5
5
6
6
WScript . LoadScriptFile ( "..\\UnitTestFramework\\UnitTestFramework.js" ) ;
7
7
8
- function testRegExp ( str , regexp , expectedResult )
8
+ function matchRegExp ( str , regexp , expectedResult )
9
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 ;
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 ] ;
13
15
assert . areEqual ( expectedResult , actualResult , errorMsg ) ;
14
16
}
15
17
@@ -19,43 +21,43 @@ var tests = [
19
21
body : function ( )
20
22
{
21
23
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" ) ;
31
33
}
32
34
} ,
33
35
{
34
36
name : "RegExp tests with IgnoreCase flag set" ,
35
37
body : function ( )
36
38
{
37
39
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" ) ;
45
47
}
46
48
} ,
47
49
{
48
50
name : "RegExp tests with Unicode flag set" ,
49
51
body : function ( )
50
52
{
51
53
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 ) ;
59
61
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" ) ;
60
62
}
61
63
} ,
@@ -64,65 +66,68 @@ var tests = [
64
66
body : function ( )
65
67
{
66
68
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 ) ;
71
73
}
72
74
} ,
73
75
{
74
76
name : "Regression tests from bugFixRegression" ,
75
77
body : function ( )
76
78
{
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" ) ;
81
84
}
82
85
} ,
83
86
{
84
87
name : "Special character tests" ,
85
88
body : function ( )
86
89
{
87
90
let re = / ^ [ \s ] [ a \s b ] [ \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 ) ;
91
94
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 ) ;
95
98
}
96
99
} ,
97
100
{
98
101
name : "Negation character set tests" ,
99
102
body : function ( )
100
103
{
101
104
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-' ) ;
105
108
}
106
109
} ,
107
110
{
108
111
name : "Non-range tests" ,
109
112
body : function ( )
110
113
{
111
114
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 ) ;
118
122
119
123
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 ) ;
126
131
}
127
132
}
128
133
] ;
0 commit comments