Added and renamed tests
This commit is contained in:
Yishai Galatzer 2014-03-29 12:24:33 -07:00
Родитель ff5d919e0d
Коммит 3e7d8d54f9
6 изменённых файлов: 147 добавлений и 110 удалений

Просмотреть файл

@ -1,19 +0,0 @@
using Microsoft.AspNet.Abstractions;
using Moq;
namespace Microsoft.AspNet.Routing.Tests
{
public static class RouteConstraintExtensions
{
public static bool EasyMatch(this IRouteConstraint constraint,
string routeKey,
RouteValueDictionary values)
{
return constraint.Match(httpContext: new Mock<HttpContext>().Object,
route: new Mock<IRouter>().Object,
routeKey: routeKey,
values: values,
routeDirection: RouteDirection.IncomingRequest);
}
}
}

Просмотреть файл

@ -1,4 +1,5 @@
using System.Collections.Generic; #if NET45
using System.Collections.Generic;
using Microsoft.AspNet.Abstractions; using Microsoft.AspNet.Abstractions;
using Moq; using Moq;
using Xunit; using Xunit;
@ -7,30 +8,6 @@ namespace Microsoft.AspNet.Routing.Tests
{ {
public class ConstraintMatcherTests public class ConstraintMatcherTests
{ {
private class PassConstraint : IRouteConstraint
{
public bool Match(HttpContext httpContext,
IRouter route,
string routeKey,
IDictionary<string, object> values,
RouteDirection routeDirection)
{
return true;
}
}
private class FailConstraint : IRouteConstraint
{
public bool Match(HttpContext httpContext,
IRouter route,
string routeKey,
IDictionary<string, object> values,
RouteDirection routeDirection)
{
return false;
}
}
[Fact] [Fact]
public void ReturnsTrueOnValidConstraints() public void ReturnsTrueOnValidConstraints()
{ {
@ -50,6 +27,25 @@ namespace Microsoft.AspNet.Routing.Tests
routeDirection: RouteDirection.IncomingRequest)); routeDirection: RouteDirection.IncomingRequest));
} }
[Fact]
public void ConstraintsGetTheRightKey()
{
var constraints = new Dictionary<string, IRouteConstraint>
{
{"a", new PassConstraint("a")},
{"b", new PassConstraint("b")}
};
var routeValueDictionary = new RouteValueDictionary(new { a = "value", b = "value" });
Assert.True(RouteConstraintMatcher.Match(
constraints: constraints,
routeValues: routeValueDictionary,
httpContext: new Mock<HttpContext>().Object,
route: new Mock<IRouter>().Object,
routeDirection: RouteDirection.IncomingRequest));
}
[Fact] [Fact]
public void ReturnsFalseOnInvalidConstraintsThatDontMatch() public void ReturnsFalseOnInvalidConstraintsThatDontMatch()
{ {
@ -117,5 +113,42 @@ namespace Microsoft.AspNet.Routing.Tests
route: new Mock<IRouter>().Object, route: new Mock<IRouter>().Object,
routeDirection: RouteDirection.IncomingRequest)); routeDirection: RouteDirection.IncomingRequest));
} }
private class PassConstraint : IRouteConstraint
{
private readonly string _expectedKey;
public PassConstraint(string expectedKey = null)
{
_expectedKey = expectedKey;
}
public bool Match(HttpContext httpContext,
IRouter route,
string routeKey,
IDictionary<string, object> values,
RouteDirection routeDirection)
{
if (_expectedKey != null)
{
Assert.Equal(_expectedKey, routeKey);
}
return true;
} }
} }
private class FailConstraint : IRouteConstraint
{
public bool Match(HttpContext httpContext,
IRouter route,
string routeKey,
IDictionary<string, object> values,
RouteDirection routeDirection)
{
return false;
}
}
}
}
#endif

Просмотреть файл

@ -1,6 +1,8 @@
using System; #if NET45
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Microsoft.AspNet.Abstractions;
using Microsoft.AspNet.Testing; using Microsoft.AspNet.Testing;
using Moq; using Moq;
using Xunit; using Xunit;
@ -9,25 +11,6 @@ namespace Microsoft.AspNet.Routing.Tests
{ {
public class ConstraintsBuilderTests public class ConstraintsBuilderTests
{ {
public static IEnumerable<object> EmptyAndNullDictionary
{
get
{
return new[]
{
new Object[]
{
null,
},
new Object[]
{
new Dictionary<string, object>(),
},
};
}
}
[Theory] [Theory]
[MemberData("EmptyAndNullDictionary")] [MemberData("EmptyAndNullDictionary")]
public void ConstraintBuilderReturnsNull_OnNullOrEmptyInput(IDictionary<string, object> input) public void ConstraintBuilderReturnsNull_OnNullOrEmptyInput(IDictionary<string, object> input)
@ -106,13 +89,13 @@ namespace Microsoft.AspNet.Routing.Tests
} }
[Theory] [Theory]
[InlineData("abc", "abc", true)] [InlineData("abc", "abc", true)] // simple case
[InlineData("abc", "bbb|abc", true)] [InlineData("abc", "bbb|abc", true)] // Regex or
[InlineData("Abc", "abc", true)] [InlineData("Abc", "abc", true)] // Case insensitive
[InlineData("Abc ", "abc", false)] [InlineData("Abc ", "abc", false)] // Matches whole (but no trimming)
[InlineData("Abcd", "abc", false)] [InlineData("Abcd", "abc", false)] // Matches whole (additional non whitespace char)
[InlineData("Abc", " abc", false)] [InlineData("Abc", " abc", false)] // Matches whole (less one char)
public void StringConstraintsMatchesWholeValueCaseInsensitively(string routeValue, public void StringConstraintsMatchingScenarios(string routeValue,
string constraintValue, string constraintValue,
bool shouldMatch) bool shouldMatch)
{ {
@ -124,7 +107,32 @@ namespace Microsoft.AspNet.Routing.Tests
var constraint = constraintDictionary["controller"]; var constraint = constraintDictionary["controller"];
Assert.Equal(shouldMatch, Assert.Equal(shouldMatch,
constraint.EasyMatch("controller", dictionary)); constraint.Match(
httpContext: new Mock<HttpContext>().Object,
route: new Mock<IRouter>().Object,
routeKey: "controller",
values: dictionary,
routeDirection: RouteDirection.IncomingRequest));
}
public static IEnumerable<object> EmptyAndNullDictionary
{
get
{
return new[]
{
new Object[]
{
null,
},
new Object[]
{
new Dictionary<string, object>(),
},
};
} }
} }
} }
}
#endif

Просмотреть файл

@ -1,21 +1,23 @@
using System; #if NET45
using Moq;
using System.Globalization; using System.Globalization;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading; using System.Threading;
using Xunit; using Xunit;
using Microsoft.AspNet.Abstractions;
namespace Microsoft.AspNet.Routing.Tests namespace Microsoft.AspNet.Routing.Tests
{ {
public class RegexConstraintTests public class RegexConstraintTests
{ {
[Theory] [Theory]
[InlineData("abc", "abc", true)] [InlineData("abc", "abc", true)] // simple match
[InlineData("Abc", "abc", true)] [InlineData("Abc", "abc", true)] // case insensitive match
[InlineData("Abc ", "abc", true)] [InlineData("Abc ", "abc", true)] // Extra space on input match (because we don't add ^({0})$
[InlineData("Abcd", "abc", true)] [InlineData("Abcd", "abc", true)] // Extra char
[InlineData("^Abcd", "abc", true)] [InlineData("^Abcd", "abc", true)] // Extra special char
[InlineData("Abc", " abc", false)] [InlineData("Abc", " abc", false)] // Missing char
public void RegexConstraintDoesNotPrepend(string routeValue, public void RegexConstraintBuildRegexVerbatimFromInput(string routeValue,
string constraintValue, string constraintValue,
bool shouldMatch) bool shouldMatch)
{ {
@ -24,48 +26,48 @@ namespace Microsoft.AspNet.Routing.Tests
var values = new RouteValueDictionary(new {controller = routeValue}); var values = new RouteValueDictionary(new {controller = routeValue});
// Assert // Assert
Assert.Equal(shouldMatch, constraint.EasyMatch("controller", values)); Assert.Equal(shouldMatch, EasyMatch(constraint, "controller", values));
} }
[Fact] [Fact]
public void RegexConstraintCanTakeARegex_SuccessulMatch() public void RegexConstraint_TakesRegexAsInput_SimpleMatch()
{ {
// Arrange // Arrange
var constraint = new RegexConstraint(new Regex("^abc$")); var constraint = new RegexConstraint(new Regex("^abc$"));
var values = new RouteValueDictionary(new { controller = "abc"}); var values = new RouteValueDictionary(new { controller = "abc"});
// Assert // Assert
Assert.True(constraint.EasyMatch("controller", values)); Assert.True(EasyMatch(constraint, "controller", values));
} }
[Fact] [Fact]
public void RegexConstraintFailsIfKeyIsNotFound() public void RegexConstraintConstructedWithRegex_SimpleFailedMatch()
{
// Arrange
var constraint = new RegexConstraint(new Regex("^abc$"));
var values = new RouteValueDictionary(new { action = "abc" });
// Assert
Assert.False(constraint.EasyMatch("controller", values));
}
[Fact]
public void RegexConstraintCanTakeARegex_FailedMatch()
{ {
// Arrange // Arrange
var constraint = new RegexConstraint(new Regex("^abc$")); var constraint = new RegexConstraint(new Regex("^abc$"));
var values = new RouteValueDictionary(new { controller = "Abc" }); var values = new RouteValueDictionary(new { controller = "Abc" });
// Assert // Assert
Assert.False(constraint.EasyMatch("controller", values)); Assert.False(EasyMatch(constraint, "controller", values));
} }
[Fact] [Fact]
public void RegexConstraintIsCultureInsensitive() public void RegexConstraintFailsIfKeyIsNotFoundInRouteValues()
{
// Arrange
var constraint = new RegexConstraint(new Regex("^abc$"));
var values = new RouteValueDictionary(new { action = "abc" });
// Assert
Assert.False(EasyMatch(constraint, "controller", values));
}
[Fact]
public void RegexConstraintIsCultureInsensitiveWhenConstructredWithString()
{ {
// Arrange // Arrange
var constraint = new RegexConstraint("^([a-z]+)$"); var constraint = new RegexConstraint("^([a-z]+)$");
var values = new RouteValueDictionary(new { controller = "\u0130" }); var values = new RouteValueDictionary(new { controller = "\u0130" }); // Turkish upper-case dotted I
var currentThread = Thread.CurrentThread; var currentThread = Thread.CurrentThread;
var backupCulture = currentThread.CurrentCulture; var backupCulture = currentThread.CurrentCulture;
@ -77,10 +79,10 @@ namespace Microsoft.AspNet.Routing.Tests
try try
{ {
currentThread.CurrentCulture = new CultureInfo("tr-TR"); // Turkish culture currentThread.CurrentCulture = new CultureInfo("tr-TR"); // Turkish culture
matchInTurkish = constraint.EasyMatch("controller", values); matchInTurkish = EasyMatch(constraint, "controller", values);
currentThread.CurrentCulture = new CultureInfo("en-US"); currentThread.CurrentCulture = new CultureInfo("en-US");
matchInUsEnglish = constraint.EasyMatch("controller", values); matchInUsEnglish = EasyMatch(constraint, "controller", values);
} }
finally finally
{ {
@ -91,5 +93,17 @@ namespace Microsoft.AspNet.Routing.Tests
Assert.False(matchInUsEnglish); // this just verifies the test Assert.False(matchInUsEnglish); // this just verifies the test
Assert.False(matchInTurkish); Assert.False(matchInTurkish);
} }
private static bool EasyMatch(IRouteConstraint constraint,
string routeKey,
RouteValueDictionary values)
{
return constraint.Match(httpContext: new Mock<HttpContext>().Object,
route: new Mock<IRouter>().Object,
routeKey: routeKey,
values: values,
routeDirection: RouteDirection.IncomingRequest);
} }
} }
}
#endif

Просмотреть файл

@ -179,7 +179,7 @@ namespace Microsoft.AspNet.Routing.Template.Tests
TemplateRoute r = CreateRoute( TemplateRoute r = CreateRoute(
"{p1}/{p2}", "{p1}/{p2}",
new RouteValueDictionary(new { p2 = "catchall" }), new { p2 = "catchall" },
true, true,
new RouteValueDictionary(new { p2 = "\\d{4}" })); new RouteValueDictionary(new { p2 = "\\d{4}" }));
@ -199,7 +199,7 @@ namespace Microsoft.AspNet.Routing.Template.Tests
TemplateRoute r = CreateRoute( TemplateRoute r = CreateRoute(
"{p1}/{p2}", "{p1}/{p2}",
new RouteValueDictionary(new { p2 = "catchall" }), new { p2 = "catchall" },
true, true,
new RouteValueDictionary(new { p2 = "\\d{4}" })); new RouteValueDictionary(new { p2 = "\\d{4}" }));
@ -220,7 +220,7 @@ namespace Microsoft.AspNet.Routing.Template.Tests
TemplateRoute r = CreateRoute( TemplateRoute r = CreateRoute(
"{p1}/{*p2}", "{p1}/{*p2}",
new RouteValueDictionary(new { p2 = "catchall" }), new { p2 = "catchall" },
true, true,
new RouteValueDictionary(new { p2 = "\\d{4}" })); new RouteValueDictionary(new { p2 = "\\d{4}" }));
@ -241,7 +241,7 @@ namespace Microsoft.AspNet.Routing.Template.Tests
TemplateRoute r = CreateRoute( TemplateRoute r = CreateRoute(
"{p1}/{*p2}", "{p1}/{*p2}",
new RouteValueDictionary(new { p2 = "catchall" }), new { p2 = "catchall" },
true, true,
new RouteValueDictionary(new { p2 = "\\d{4}" })); new RouteValueDictionary(new { p2 = "\\d{4}" }));
@ -271,7 +271,7 @@ namespace Microsoft.AspNet.Routing.Template.Tests
TemplateRoute r = CreateRoute( TemplateRoute r = CreateRoute(
"{p1}/{p2}", "{p1}/{p2}",
new RouteValueDictionary(new { p2 = "catchall" }), new { p2 = "catchall" },
true, true,
new RouteValueDictionary(new { p2 = target.Object })); new RouteValueDictionary(new { p2 = target.Object }));

Просмотреть файл

@ -19,6 +19,7 @@
"System.Runtime.Extensions": "4.0.10.0", "System.Runtime.Extensions": "4.0.10.0",
"System.Threading": "4.0.0.0", "System.Threading": "4.0.0.0",
"System.Threading.Tasks": "4.0.10.0" "System.Threading.Tasks": "4.0.10.0"
"System.Text.RegularExpressions": "4.0.0.0"
} }
}, },
"net45": { "net45": {