Skip to content

Commit c0f7bc4

Browse files
author
Kenji Fukuda
committed
Changing tests to do a match and verify the matched string.
Adding a missed regression test and a couple new tests.
1 parent 3daa80d commit c0f7bc4

File tree

1 file changed

+61
-56
lines changed

1 file changed

+61
-56
lines changed

test/Regex/characterclass_with_range.js

Lines changed: 61 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55

66
WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
77

8-
function testRegExp(str, regexp, expectedResult)
8+
function matchRegExp(str, regexp, expectedResult)
99
{
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;
10+
matchResult = str.match(regexp);//regexp.test(str);
11+
errorMsg = "Expected result of match between string: '" + str + "' and regular expression: " + regexp + " to be " +
12+
expectedResult + " but was " + matchResult;
13+
14+
actualResult = matchResult == null ? null : matchResult[0];
1315
assert.areEqual(expectedResult, actualResult, errorMsg);
1416
}
1517

@@ -19,43 +21,43 @@ var tests = [
1921
body : function ()
2022
{
2123
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);
24+
matchRegExp("b", re, null);
25+
matchRegExp("a", re, "a");
26+
matchRegExp(" ", re, " ");
27+
matchRegExp("z", re, "z");
28+
matchRegExp("\t", re, "\t");
29+
matchRegExp("q", re, null);
30+
matchRegExp("\\", re, null);
31+
matchRegExp("\u2028", re, "\u2028");
32+
matchRegExp("\u2009", re, "\u2009");
3133
}
3234
},
3335
{
3436
name : "RegExp tests with IgnoreCase flag set",
3537
body : function ()
3638
{
3739
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);
40+
matchRegExp("O", reIgnoreCase, null);
41+
matchRegExp("A", reIgnoreCase, "A");
42+
matchRegExp(" ", reIgnoreCase, " ");
43+
matchRegExp("z", reIgnoreCase, "z");
44+
matchRegExp("\t", reIgnoreCase, "\t");
45+
matchRegExp("\u2028", reIgnoreCase, "\u2028");
46+
matchRegExp("\u2009", reIgnoreCase, "\u2009");
4547
}
4648
},
4749
{
4850
name : "RegExp tests with Unicode flag set",
4951
body : function ()
5052
{
5153
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);
54+
matchRegExp("a", reUnicode, "a");
55+
matchRegExp("c", reUnicode, "c");
56+
matchRegExp("d", reUnicode, "d");
57+
matchRegExp("C", reUnicode, null);
58+
matchRegExp("g", reUnicode, null);
59+
matchRegExp("\u2028", reUnicode, null);
60+
matchRegExp("\u2009", reUnicode, null);
5961
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");
6062
}
6163
},
@@ -64,65 +66,68 @@ var tests = [
6466
body : function ()
6567
{
6668
let reNoCharClass = /^[a-c-z]$/;
67-
testRegExp("b", reNoCharClass, true);
68-
testRegExp("-", reNoCharClass, true);
69-
testRegExp("z", reNoCharClass, true);
70-
testRegExp("y", reNoCharClass, false);
69+
matchRegExp("b", reNoCharClass, "b");
70+
matchRegExp("-", reNoCharClass, "-");
71+
matchRegExp("z", reNoCharClass, "z");
72+
matchRegExp("y", reNoCharClass, null);
7173
}
7274
},
7375
{
7476
name : "Regression tests from bugFixRegression",
7577
body : function ()
7678
{
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]);
79+
matchRegExp(" -abc", /[\s-a-c]*/, " -a");
80+
matchRegExp(" -abc", /[\s\-a-c]*/, " -abc");
81+
matchRegExp(" -ab", /[a-\s-b]*/, " -ab");
82+
matchRegExp(" -ab", /[a\-\s\-b]*/, " -ab");
83+
assert.throws(() => eval("/^[\\s--c-!]$/.exec(\"-./0Abc!\")"), SyntaxError, "Expected an error due to 'c-!' being an invalid range.", "Invalid range in character set");
8184
}
8285
},
8386
{
8487
name : "Special character tests",
8588
body : function ()
8689
{
8790
let re = /^[\s][a\sb][\s--c-f]$/;
88-
testRegExp(' \\', re, false);
89-
testRegExp(' \\ ', re, false);
90-
testRegExp('\\ ', re, false);
91+
matchRegExp(' \\', re, null);
92+
matchRegExp(' \\ ', re, null);
93+
matchRegExp('\\ ', re, null);
9194
re = /[-][\d\-]/;
92-
testRegExp('--', re, true);
93-
testRegExp('-9', re, true);
94-
testRegExp(' ', re, false);
95+
matchRegExp('--', re, '--');
96+
matchRegExp('-9', re, '-9');
97+
matchRegExp(' ', re, null);
9598
}
9699
},
97100
{
98101
name : "Negation character set tests",
99102
body : function ()
100103
{
101104
let reNegationCharSet = /[\D-\s]+/;
102-
testRegExp('555686', reNegationCharSet, false);
103-
testRegExp('555-686', reNegationCharSet, true);
104-
testRegExp('alphabet-123', reNegationCharSet, true);
105+
matchRegExp('555686', reNegationCharSet, null);
106+
matchRegExp('555-686', reNegationCharSet, '-');
107+
matchRegExp('alphabet-123', reNegationCharSet, 'alphabet-');
105108
}
106109
},
107110
{
108111
name : "Non-range tests",
109112
body : function ()
110113
{
111114
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);
115+
matchRegExp('-', reNonRange, '-');
116+
matchRegExp('g', reNonRange, 'g');
117+
matchRegExp('5', reNonRange, '5');
118+
matchRegExp(' ', reNonRange, null);
119+
matchRegExp('\t', reNonRange, null);
120+
matchRegExp('\u2028', reNonRange, null);
121+
matchRegExp('\\', reNonRange, null);
118122

119123
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);
124+
matchRegExp('-', reNonRange, '-');
125+
matchRegExp('g', reNonRange, 'g');
126+
matchRegExp('5', reNonRange, '5');
127+
matchRegExp(' ', reNonRange, null);
128+
matchRegExp('\t', reNonRange, null);
129+
matchRegExp('\u2028', reNonRange, null);
130+
matchRegExp('\\', reNonRange, null);
126131
}
127132
}
128133
];

0 commit comments

Comments
 (0)