|
5 | 5 |
|
6 | 6 | WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
|
7 | 7 |
|
8 |
| -let re = /^[\s-a-z]$/; |
9 |
| -let reIgnoreCase = /^[\s-a-z]$/i; |
10 |
| -let reUnicode = /^[\s-z]$/u; |
11 |
| -let reNoCharClass = /^[a-c-z]$/; |
| 8 | +function testRegExp(str, regexp, expectedResult) |
| 9 | +{ |
| 10 | + actualResult = regexp.test(str); |
| 11 | + errorMsg = "Expected result of test for match between string: '" + str + "' and regular expression: " + regexp + " to be " + |
| 12 | + expectedResult + " but was " + actualResult; |
| 13 | + assert.areEqual(expectedResult, actualResult, errorMsg); |
| 14 | +} |
12 | 15 |
|
13 | 16 | var tests = [
|
14 |
| - /*No Flag RegExp Tests begin*/ |
15 |
| - { |
16 |
| - name : "Ensure 'a-z' not counted as range", |
17 |
| - body : function () |
18 |
| - { |
19 |
| - assert.isFalse(re.test("b")); |
20 |
| - } |
21 |
| - }, |
22 |
| - { |
23 |
| - name : "Ensure 'a' included in set", |
24 |
| - body : function () |
25 |
| - { |
26 |
| - assert.isTrue(re.test("a")); |
27 |
| - } |
28 |
| - }, |
29 |
| - { |
30 |
| - name : "Ensure ' ' included in set", |
31 |
| - body : function () |
32 |
| - { |
33 |
| - assert.isTrue(re.test(" ")); |
34 |
| - } |
35 |
| - }, |
36 |
| - { |
37 |
| - name : "Ensure 'z' included in set", |
38 |
| - body : function () |
39 |
| - { |
40 |
| - assert.isTrue(re.test("z")); |
41 |
| - } |
42 |
| - }, |
43 |
| - { |
44 |
| - name : "Ensure '\t' included in set", |
45 |
| - body : function () |
46 |
| - { |
47 |
| - assert.isTrue(re.test("\t")); |
48 |
| - } |
49 |
| - }, |
50 |
| - { |
51 |
| - name : "Ensure 'a-z' not counted as range", |
52 |
| - body : function () |
53 |
| - { |
54 |
| - assert.isFalse(re.test("q")); |
55 |
| - } |
56 |
| - }, |
57 |
| - { |
58 |
| - name : "Ensure '\' not counted in set", |
59 |
| - body : function () |
60 |
| - { |
61 |
| - assert.isFalse(re.test("\\")); |
62 |
| - } |
63 |
| - }, |
64 |
| - /*No Flag RegExp Tests End*/ |
65 |
| - /*IgnoreCase Flag RegExp Tests Begin*/ |
66 | 17 | {
|
67 |
| - name : "Ensure 'O' not included in set", |
| 18 | + name : "RegExp tests with no flags", |
68 | 19 | body : function ()
|
69 | 20 | {
|
70 |
| - assert.isFalse(reIgnoreCase.test("O")); |
| 21 | + let re = /[\s-a-z]/; |
| 22 | + testRegExp("b", re, false); |
| 23 | + testRegExp("a", re, true); |
| 24 | + testRegExp(" ", re, true); |
| 25 | + testRegExp("z", re, true); |
| 26 | + testRegExp("\t", re, true); |
| 27 | + testRegExp("q", re, false); |
| 28 | + testRegExp("\\", re, false); |
| 29 | + testRegExp("\u2028", re, true); |
| 30 | + testRegExp("\u2009", re, true); |
71 | 31 | }
|
72 | 32 | },
|
73 | 33 | {
|
74 |
| - name : "Ensure 'A' included in set", |
| 34 | + name : "RegExp tests with IgnoreCase flag set", |
75 | 35 | body : function ()
|
76 | 36 | {
|
77 |
| - assert.isTrue(reIgnoreCase.test("A")); |
| 37 | + let reIgnoreCase = /^[\s-a-z]$/i; |
| 38 | + testRegExp("O", reIgnoreCase, false); |
| 39 | + testRegExp("A", reIgnoreCase, true); |
| 40 | + testRegExp(" ", reIgnoreCase, true); |
| 41 | + testRegExp("z", reIgnoreCase, true); |
| 42 | + testRegExp("\t", reIgnoreCase, true); |
| 43 | + testRegExp("\u2028", reIgnoreCase, true); |
| 44 | + testRegExp("\u2009", reIgnoreCase, true); |
78 | 45 | }
|
79 | 46 | },
|
80 | 47 | {
|
81 |
| - name : "Ensure ' ' included in set", |
| 48 | + name : "RegExp tests with Unicode flag set", |
82 | 49 | body : function ()
|
83 | 50 | {
|
84 |
| - assert.isTrue(reIgnoreCase.test(" ")); |
| 51 | + let reUnicode = /^[a-d]$/u; |
| 52 | + testRegExp("a", reUnicode, true); |
| 53 | + testRegExp("c", reUnicode, true); |
| 54 | + testRegExp("d", reUnicode, true); |
| 55 | + testRegExp("C", reUnicode, false); |
| 56 | + testRegExp("g", reUnicode, false); |
| 57 | + testRegExp("\u2028", reUnicode, false); |
| 58 | + testRegExp("\u2009", reUnicode, false); |
| 59 | + assert.throws(() => eval("/^[\\s-z]$/u.exec(\"-\")"), SyntaxError, "Expected an error due to character sets not being allowed in ranges when unicode flag is set.", "Character classes not allowed in class ranges"); |
85 | 60 | }
|
86 | 61 | },
|
87 | 62 | {
|
88 |
| - name : "Ensure 'z' included in set", |
| 63 | + name : "Non-character class tests", |
89 | 64 | body : function ()
|
90 | 65 | {
|
91 |
| - assert.isTrue(reIgnoreCase.test("z")); |
| 66 | + let reNoCharClass = /^[a-c-z]$/; |
| 67 | + testRegExp("b", reNoCharClass, true); |
| 68 | + testRegExp("-", reNoCharClass, true); |
| 69 | + testRegExp("z", reNoCharClass, true); |
| 70 | + testRegExp("y", reNoCharClass, false); |
92 | 71 | }
|
93 | 72 | },
|
94 | 73 | {
|
95 |
| - name : "Ensure '\t' included in set", |
| 74 | + name : "Regression tests from bugFixRegression", |
96 | 75 | body : function ()
|
97 | 76 | {
|
98 |
| - assert.isTrue(reIgnoreCase.test("\t")); |
| 77 | + assert.areEqual(" -a", /[\s-a-c]*/.exec(" -abc")[0]); |
| 78 | + assert.areEqual(" -abc", /[\s\-a-c]*/.exec(" -abc")[0]); |
| 79 | + assert.areEqual(" -ab", /[a-\s-b]*/.exec(" -ab")[0]); |
| 80 | + assert.areEqual(" -ab", /[a\-\s\-b]*/.exec(" -ab")[0]); |
99 | 81 | }
|
100 | 82 | },
|
101 |
| - /*IgnoreCase Flag RegExp Tests End*/ |
102 |
| - /*Unicode Flag RegExp Tests Begin*/ |
103 | 83 | {
|
104 |
| - name : "'-' included in set since \s-z treated as union of three types, not range", |
| 84 | + name : "Special character tests", |
105 | 85 | body : function ()
|
106 | 86 | {
|
107 |
| - assert.isTrue(reUnicode.test("-")); |
| 87 | + let re = /^[\s][a\sb][\s--c-f]$/; |
| 88 | + testRegExp(' \\', re, false); |
| 89 | + testRegExp(' \\ ', re, false); |
| 90 | + testRegExp('\\ ', re, false); |
| 91 | + re = /[-][\d\-]/; |
| 92 | + testRegExp('--', re, true); |
| 93 | + testRegExp('-9', re, true); |
| 94 | + testRegExp(' ', re, false); |
108 | 95 | }
|
109 | 96 | },
|
110 | 97 | {
|
111 |
| - name : "' ' in set from \s character set", |
| 98 | + name : "Negation character set tests", |
112 | 99 | body : function ()
|
113 | 100 | {
|
114 |
| - assert.isTrue(reUnicode.test(" ")); |
| 101 | + let reNegationCharSet = /[\D-\s]+/; |
| 102 | + testRegExp('555686', reNegationCharSet, false); |
| 103 | + testRegExp('555-686', reNegationCharSet, true); |
| 104 | + testRegExp('alphabet-123', reNegationCharSet, true); |
115 | 105 | }
|
116 | 106 | },
|
117 | 107 | {
|
118 |
| - name : "b not included in '\s-z'", |
| 108 | + name : "Non-range tests", |
119 | 109 | body : function ()
|
120 | 110 | {
|
121 |
| - assert.isFalse(reUnicode.test("b")); |
| 111 | + let reNonRange = /[-\w]/ |
| 112 | + testRegExp('-', reNonRange, true); |
| 113 | + testRegExp('g', reNonRange, true); |
| 114 | + testRegExp('5', reNonRange, true); |
| 115 | + testRegExp(' ', reNonRange, false); |
| 116 | + testRegExp('\t', reNonRange, false); |
| 117 | + testRegExp('\u2028', reNonRange, false); |
| 118 | + |
| 119 | + reNonRange = /[\w-]/ |
| 120 | + testRegExp('-', reNonRange, true); |
| 121 | + testRegExp('g', reNonRange, true); |
| 122 | + testRegExp('5', reNonRange, true); |
| 123 | + testRegExp(' ', reNonRange, false); |
| 124 | + testRegExp('\t', reNonRange, false); |
| 125 | + testRegExp('\u2028', reNonRange, false); |
122 | 126 | }
|
123 |
| - }, |
124 |
| - /*Unicode Flag RegExp Tests End*/ |
125 |
| - /*Non-character class tests Begin*/ |
126 |
| - { |
127 |
| - name : "First range is used", |
128 |
| - body : function () |
129 |
| - { |
130 |
| - assert.isTrue(reNoCharClass.test("b")); |
131 |
| - } |
132 |
| - }, |
133 |
| - { |
134 |
| - name : "'-' included in set from 2nd dash", |
135 |
| - body : function () |
136 |
| - { |
137 |
| - assert.isTrue(reNoCharClass.test("-")); |
138 |
| - } |
139 |
| - }, |
140 |
| - { |
141 |
| - name : "z included in set", |
142 |
| - body : function () |
143 |
| - { |
144 |
| - assert.isTrue(reNoCharClass.test("z")); |
145 |
| - } |
146 |
| - }, |
147 |
| - { |
148 |
| - name : "'c-z' not viewed as range", |
149 |
| - body : function () |
150 |
| - { |
151 |
| - assert.isFalse(reNoCharClass.test("y")); |
152 |
| - } |
153 |
| - } |
154 |
| - /*Non-character class tests End*/ |
| 127 | + } |
155 | 128 | ];
|
156 | 129 |
|
157 |
| -if (typeof modifyTests !== "undefined") { |
158 |
| - tests = modifyTests(tests); |
159 |
| -} |
160 |
| - |
161 | 130 | testRunner.runTests(tests, {
|
162 | 131 | verbose : WScript.Arguments[0] != "summary"
|
163 | 132 | });
|
0 commit comments