forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegularExpressionAttributeTests.cs
More file actions
96 lines (84 loc) · 4.9 KB
/
RegularExpressionAttributeTests.cs
File metadata and controls
96 lines (84 loc) · 4.9 KB
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Xunit;
namespace System.ComponentModel.DataAnnotations.Tests
{
public sealed partial class RegularExpressionAttributeTests : ValidationAttributeTestBase
{
protected override IEnumerable<TestCase> ValidValues()
{
yield return new TestCase(new RegularExpressionAttribute("SomePattern"), null);
yield return new TestCase(new RegularExpressionAttribute("SomePattern") { MatchTimeoutInMilliseconds = -1 }, string.Empty);
yield return new TestCase(new RegularExpressionAttribute("defghi") { MatchTimeoutInMilliseconds = 5000 }, "defghi");
yield return new TestCase(new RegularExpressionAttribute("[^a]+\\.[^z]+") { MatchTimeoutInMilliseconds = 10000 }, "bcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxy");
yield return new TestCase(new RegularExpressionAttribute("abc"), new ClassWithValidToString());
yield return new TestCase(new RegularExpressionAttribute("1"), 1);
yield return new TestCase(new RegularExpressionAttribute("abc"), new IFormattableImplementor());
}
protected override IEnumerable<TestCase> InvalidValues()
{
yield return new TestCase(new RegularExpressionAttribute("defghi"), "zyxwvu");
yield return new TestCase(new RegularExpressionAttribute("defghi"), "defghijkl");
yield return new TestCase(new RegularExpressionAttribute("defghi"), "abcdefghi");
yield return new TestCase(new RegularExpressionAttribute("defghi"), "abcdefghijkl");
yield return new TestCase(new RegularExpressionAttribute("[^a]+\\.[^z]+") { MatchTimeoutInMilliseconds = 10000 } , "aaaaa");
yield return new TestCase(new RegularExpressionAttribute("[^a]+\\.[^z]+") { MatchTimeoutInMilliseconds = 10000 }, "zzzzz");
yield return new TestCase(new RegularExpressionAttribute("[^a]+\\.[^z]+") { MatchTimeoutInMilliseconds = 10000 }, "b.z");
yield return new TestCase(new RegularExpressionAttribute("[^a]+\\.[^z]+") { MatchTimeoutInMilliseconds = 10000 }, "a.y");
yield return new TestCase(new RegularExpressionAttribute("def"), new ClassWithValidToString());
yield return new TestCase(new RegularExpressionAttribute("2"), 1);
yield return new TestCase(new RegularExpressionAttribute("def"), new IFormattableImplementor());
}
[Theory]
[InlineData("SomePattern")]
[InlineData("foo(?<1bar)")]
public static void Ctor_String(string pattern)
{
var attribute = new RegularExpressionAttribute(pattern);
Assert.Equal(pattern, attribute.Pattern);
}
[Theory]
[InlineData(12345)]
[InlineData(-1)]
public static void MatchTimeoutInMilliseconds_GetSet_ReturnsExpected(int newValue)
{
var attribute = new RegularExpressionAttribute("SomePattern");
attribute.MatchTimeoutInMilliseconds = newValue;
Assert.Equal(newValue, attribute.MatchTimeoutInMilliseconds);
}
[Theory]
[InlineData(null)]
[InlineData("")]
public static void Validate_InvalidPattern_ThrowsInvalidOperationException(string pattern)
{
var attribute = new RegularExpressionAttribute(pattern);
Assert.Throws<InvalidOperationException>(() => attribute.Validate("Any", new ValidationContext(new object())));
}
[Theory]
[InlineData(0)]
[InlineData(-2)]
public static void Validate_InvalidMatchTimeoutInMilliseconds_ThrowsArgumentOutOfRangeException(int timeout)
{
RegularExpressionAttribute attribute = new RegularExpressionAttribute("[^a]+\\.[^z]+") { MatchTimeoutInMilliseconds = timeout };
AssertExtensions.Throws<ArgumentOutOfRangeException>("matchTimeout", () => attribute.Validate("a", new ValidationContext(new object())));
}
[Fact]
public static void Validate_MatchingTimesOut_ThrowsRegexMatchTimeoutException()
{
RegularExpressionAttribute attribute = new RegularExpressionAttribute("(a[ab]+)+$") { MatchTimeoutInMilliseconds = 1 };
Assert.Throws<RegexMatchTimeoutException>(() => attribute.Validate("aaaaaaaaaaaaaaaaaaaaaaaaaaaa>", new ValidationContext(new object())));
}
[Fact]
public static void Validate_InvalidPattern_ThrowsArgumentException()
{
RegularExpressionAttribute attribute = new RegularExpressionAttribute("foo(?<1bar)");
Assert.ThrowsAny<ArgumentException>(() => attribute.Validate("Any", new ValidationContext(new object())));
}
public class ClassWithValidToString
{
public override string ToString() => "abc";
}
}
}