-
Notifications
You must be signed in to change notification settings - Fork 3
/
CharGroupPatternTests.cs
97 lines (78 loc) · 4.19 KB
/
CharGroupPatternTests.cs
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
using NUnit.Framework;
using RegexParser.Patterns;
namespace RegexParser.Tests.Patterns
{
[TestFixture]
public class CharGroupPatternTests
{
[Test]
public void Ranges()
{
BasePattern actual = BasePattern.CreatePattern("[A-Z]");
BasePattern expected = new GroupPattern(true, new BasePattern[] { new CharGroupPattern(true, new[] { uppercase }) });
Assert.AreEqual(expected, actual, "One Range");
actual = BasePattern.CreatePattern("[A-Z][a-z][0-9][a-zA-Z][A-Za-z0-9][a-zm-wA-M]");
expected = new GroupPattern(true, new BasePattern[] {
new CharGroupPattern(true, new[] { uppercase }),
new CharGroupPattern(true, new[] { lowercase }),
new CharGroupPattern(true, new[] { digits }),
new CharGroupPattern(true, new[] { lowercase, uppercase }),
new CharGroupPattern(true, new[] { uppercase, lowercase, digits }),
new CharGroupPattern(true, new[] { lowercase,
new CharRangePattern('m', 'w'),
new CharRangePattern('A', 'M') })
});
Assert.AreEqual(expected, actual, "Many Ranges");
}
[Test]
public void CharSetsAndRanges()
{
BasePattern actual = BasePattern.CreatePattern("[A-Z]");
BasePattern expected = new GroupPattern(true, new BasePattern[] { new CharGroupPattern(true, new[] { uppercase }) });
Assert.AreEqual(expected, actual, "One Range");
actual = BasePattern.CreatePattern("[.,;:?!A-Za-z][xya-zm-wA-M][msabcdm ;xyz]");
expected = new GroupPattern(true, new BasePattern[] {
new CharGroupPattern(true, ".,;:?!", new[] { uppercase, lowercase }),
new CharGroupPattern(true, "xy", new[] { lowercase,
new CharRangePattern('m', 'w'),
new CharRangePattern('A', 'M') }),
new CharGroupPattern(true, "msabcd ;xyz")
});
Assert.AreEqual(expected, actual);
}
[Test]
public void NegativeRanges()
{
BasePattern actual = BasePattern.CreatePattern("[A-Z][^x][^a-z][0-9]");
BasePattern expected = new GroupPattern(true, new BasePattern[] {
new CharGroupPattern(true, new[] { uppercase }),
new CharGroupPattern(false, "x"),
new CharGroupPattern(false, new[] { lowercase }),
new CharGroupPattern(true, new[] { digits })
});
Assert.AreEqual(expected, actual);
}
[Test]
public void Equality()
{
CharGroupPattern pattern1 = new CharGroupPattern(true, "xy");
CharGroupPattern pattern2 = new CharGroupPattern(true, "xyxyy");
Assert.AreEqual(pattern1, pattern2, "CharSet");
pattern1 = new CharGroupPattern(true, new[] { uppercase, lowercase });
pattern2 = new CharGroupPattern(true, new[] { uppercase, lowercase });
Assert.AreEqual(pattern1, pattern2, "CharRange");
pattern1 = new CharGroupPattern(true, "xyyx", new[] { uppercase, lowercase });
pattern2 = new CharGroupPattern(true, "xxy", new[] { uppercase, lowercase });
Assert.AreEqual(pattern1, pattern2, "CharSet/CharRange");
Assert.AreEqual(pattern1.CharSet, pattern2.CharSet, "CharRange");
Assert.AreEqual(pattern1.ChildPatterns[0], pattern2.ChildPatterns[0], "CharRange");
pattern1 = new CharGroupPattern(true, new[] { uppercase, lowercase });
pattern2 = new CharGroupPattern(true, new[] { uppercase, lowercase, digits });
Assert.AreNotEqual(pattern1, pattern2, "NotEqual");
Assert.IsTrue(pattern1 != pattern2, "!=");
}
private static CharRangePattern uppercase = new CharRangePattern('A', 'Z'),
lowercase = new CharRangePattern('a', 'z'),
digits = new CharRangePattern('0', '9');
}
}