#18533: Fixing RouteValueDictionary comparisons

Work Item: 18533

--HG--
branch : 1.x
This commit is contained in:
Sebastien Ros 2012-03-12 16:38:28 -07:00
parent 2c25bb243d
commit 43ce029cab
3 changed files with 50 additions and 2 deletions

View File

@ -309,6 +309,7 @@
<Compile Include="UI\Navigation\MenuItemComparerTests.cs" />
<Compile Include="UI\Navigation\NavigationManagerTests.cs" />
<Compile Include="UI\Navigation\PositionComparerTests.cs" />
<Compile Include="Utility\Extensions\RouteValueDictionaryExtensionsTests.cs" />
<Compile Include="Utility\Extensions\StringExtensionsTests.cs" />
<Compile Include="Utility\ReflectOnTests.cs" />
<Compile Include="Utility\ReflectTests.cs" />

View File

@ -0,0 +1,34 @@
using System.Web.Routing;
using NUnit.Framework;
using Orchard.Utility.Extensions;
namespace Orchard.Tests.Utility.Extensions {
[TestFixture]
class RouteValueDictionaryExtensionsTests {
[Test]
public void IdenticalRouteValueDictionariesShouldMatch() {
Assert.IsTrue(new RouteValueDictionary { { "controller", "foo" }, { "action", "bar" } }
.Match(new RouteValueDictionary { { "controller", "foo" }, { "action", "bar" } }));
}
[Test]
public void CasedRouteValueDictionariesShouldMatch() {
Assert.IsTrue(new RouteValueDictionary { { "controller", "foo" }, { "action", "BAR" } }
.Match(new RouteValueDictionary { { "controller", "foo" }, { "action", "bar" } }));
}
[Test]
public void RouteValueDictionariesWithDifferentNumbersOfValuesShouldNotMatch() {
Assert.IsFalse(new RouteValueDictionary { { "controller", "foo" }, { "action", "bar" } }
.Match(new RouteValueDictionary { { "controller", "foo" }, { "action", "bar" }, { "area", "baz" } }));
}
[Test]
public void RouteValueDictionariesWithDifferentValuesShouldMatch() {
Assert.IsFalse(new RouteValueDictionary { { "controller", "foo" }, { "action", "bar" } }
.Match(new RouteValueDictionary { { "controller", "foo" }, { "action", "baz" } }));
}
}
}

View File

@ -35,8 +35,21 @@ namespace Orchard.Utility.Extensions {
return false;
}
// keys can be different in case
return x.Keys.All(key => x[key].ToString().Equals(y[key].ToString(), StringComparison.OrdinalIgnoreCase));
var bools = x.Join(y,
kv1 => kv1.Key.ToLowerInvariant(),
kv2 => kv2.Key.ToLowerInvariant(),
(kv1, kv2) => StringMatch(kv1.Value, kv2.Value)
).ToArray();
return bools.All(b => b) && bools.Count() == x.Count;
}
private static bool StringMatch(object value1, object value2) {
return string.Equals(
Convert.ToString(value1, CultureInfo.InvariantCulture),
Convert.ToString(value2, CultureInfo.InvariantCulture),
StringComparison.InvariantCultureIgnoreCase
);
}
public static RouteValueDictionary ToRouteValueDictionary(this IEnumerable<KeyValuePair<string, string>> routeValues) {