1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.AspNetCore.Routing.Constraints;
using Microsoft.AspNetCore.Testing;
using Xunit;
namespace Microsoft.AspNetCore.Routing.Tests
{
public class LengthRouteConstraintTests
{
[Theory]
[InlineData(3, "123", true)]
[InlineData(3, "1234", false)]
[InlineData(0, "", true)]
public void LengthRouteConstraint_ExactLength_Tests(int length, string parameterValue, bool expected)
{
// Arrange
var constraint = new LengthRouteConstraint(length);
// Act
var actual = ConstraintsTestHelper.TestConstraint(constraint, parameterValue);
// Assert
Assert.Equal(expected, actual);
}
[Theory]
[InlineData(3, 5, "12", false)]
[InlineData(3, 5, "123", true)]
[InlineData(3, 5, "1234", true)]
[InlineData(3, 5, "12345", true)]
[InlineData(3, 5, "123456", false)]
public void LengthRouteConstraint_Range_Tests(int min, int max, string parameterValue, bool expected)
{
// Arrange
var constraint = new LengthRouteConstraint(min, max);
// Act
var actual = ConstraintsTestHelper.TestConstraint(constraint, parameterValue);
// Assert
Assert.Equal(expected, actual);
}
[Fact]
public void LengthRouteConstraint_SettingLengthLessThanZero_Throws()
{
// Arrange
var expectedMessage = "Value must be greater than or equal to 0.";
// Act & Assert
ExceptionAssert.ThrowsArgumentOutOfRange(
() => new LengthRouteConstraint(-1),
"length",
expectedMessage,
-1);
}
[Fact]
public void LengthRouteConstraint_SettingMinLengthLessThanZero_Throws()
{
// Arrange
var expectedMessage = "Value must be greater than or equal to 0.";
// Act & Assert
ExceptionAssert.ThrowsArgumentOutOfRange(
() => new LengthRouteConstraint(-1, 3),
"minLength",
expectedMessage,
-1);
}
[Fact]
public void LengthRouteConstraint_SettingMaxLengthLessThanZero_Throws()
{
// Arrange
var expectedMessage = "Value must be greater than or equal to 0.";
// Act & Assert
ExceptionAssert.ThrowsArgumentOutOfRange(
() => new LengthRouteConstraint(0, -1),
"maxLength",
expectedMessage,
-1);
}
[Fact]
public void LengthRouteConstraint_MinGreaterThanMax_Throws()
{
// Arrange
var expectedMessage = "The value for argument 'minLength' should be less than or equal to the " +
"value for the argument 'maxLength'.";
// Arrange Act & Assert
ExceptionAssert.ThrowsArgumentOutOfRange(
() => new LengthRouteConstraint(3, 2),
"minLength",
expectedMessage,
3);
}
}
}
|