|
5 | 5 |
|
6 | 6 | WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
|
7 | 7 |
|
| 8 | +let forbidden = [ |
| 9 | + '\\p', |
| 10 | + '\\P', |
| 11 | + '\\a', |
| 12 | + '\\A', |
| 13 | + '\\e', |
| 14 | + '\\E', |
| 15 | + '\\y', |
| 16 | + '\\Y', |
| 17 | + '\\z', |
| 18 | + '\\Z', |
| 19 | +]; |
| 20 | + |
| 21 | +let identity = [ |
| 22 | + "^", |
| 23 | + "$", |
| 24 | + "\\", |
| 25 | + ".", |
| 26 | + "*", |
| 27 | + "+", |
| 28 | + "?", |
| 29 | + "(", |
| 30 | + ")", |
| 31 | + "[", |
| 32 | + "]", |
| 33 | + "{", |
| 34 | + "}", |
| 35 | + "|", |
| 36 | + "/", |
| 37 | +]; |
| 38 | + |
| 39 | +let pseudoIdentity = [ |
| 40 | + ["f", "\f"], |
| 41 | + ["n", "\n"], |
| 42 | + ["r", "\r"], |
| 43 | + ["t", "\t"], |
| 44 | + ["v", "\v"], |
| 45 | +] |
| 46 | + |
8 | 47 | var tests = [
|
9 | 48 | {
|
10 |
| - name: "Unicode-mode RegExp Forbidden Escapes", |
| 49 | + name: "Unicode-mode RegExp Forbidden Escapes (RegExp constructor)", |
11 | 50 | body: function () {
|
12 |
| - let forbidden = [ |
13 |
| - '\\p', |
14 |
| - '\\P', |
15 |
| - '\\a', |
16 |
| - '\\A', |
17 |
| - '\\e', |
18 |
| - '\\E', |
19 |
| - '\\y', |
20 |
| - '\\Y', |
21 |
| - '\\z', |
22 |
| - '\\Z', |
23 |
| - ]; |
24 |
| - |
25 | 51 | for (re of forbidden) {
|
26 | 52 | assert.throws(function () { new RegExp(re, 'u') }, SyntaxError, 'Invalid regular expression: invalid escape in unicode pattern');
|
27 | 53 | assert.doesNotThrow(function () { new RegExp(re) });
|
28 | 54 | }
|
29 | 55 | }
|
30 |
| - } |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "Unicode-mode RegExp Forbidden Escapes (literal)", |
| 59 | + body: function () { |
| 60 | + for (re of forbidden) { |
| 61 | + assert.throws(function () { eval(`/${re}/u`); }, SyntaxError, 'Invalid regular expression: invalid escape in unicode pattern'); |
| 62 | + assert.doesNotThrow(function () { eval(`/${re}/`); }, SyntaxError, 'Invalid regular expression: invalid escape in unicode pattern'); |
| 63 | + } |
| 64 | + } |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "Allow IdentityEscapes (RegExp constructor)", |
| 68 | + body: function () { |
| 69 | + for (c of identity) { |
| 70 | + assert.doesNotThrow(function () { new RegExp(`\\${c}`, 'u') }); |
| 71 | + assert.doesNotThrow(function () { new RegExp(`\\${c}`) }); |
| 72 | + } |
| 73 | + } |
| 74 | + }, |
| 75 | + { |
| 76 | + name: "Allow IdentityEscapes (literal)", |
| 77 | + body: function () { |
| 78 | + for (c of identity) { |
| 79 | + assert.doesNotThrow(function () { eval(`/\\${c}/u`); }); |
| 80 | + assert.doesNotThrow(function () { eval(`/\\${c}/`); }); |
| 81 | + } |
| 82 | + } |
| 83 | + }, |
| 84 | + { |
| 85 | + name: "Allow Pseudo-Identity Escapes (RegExpconstructor)", |
| 86 | + body: function () { |
| 87 | + for (test of pseudoIdentity) { |
| 88 | + let c = test[0]; |
| 89 | + let rendered = test[1]; |
| 90 | + let re; |
| 91 | + assert.doesNotThrow(function () { re = new RegExp(`\\${c}`, 'u') }); |
| 92 | + assert.isTrue(re.test(rendered)); |
| 93 | + assert.doesNotThrow(function () { re = new RegExp(`\\${c}`) }); |
| 94 | + assert.isTrue(re.test(rendered)); |
| 95 | + } |
| 96 | + } |
| 97 | + }, |
| 98 | + { |
| 99 | + name: "Allow Pseudo-Identity Escapes (literal)", |
| 100 | + body: function () { |
| 101 | + for (test of pseudoIdentity) { |
| 102 | + let c = test[0]; |
| 103 | + let rendered = test[1]; |
| 104 | + let re; |
| 105 | + assert.doesNotThrow(function () { re = eval(`/\\${c}/u`) }); |
| 106 | + assert.isTrue(re.test(rendered)); |
| 107 | + assert.doesNotThrow(function () { re = eval(`/\\${c}/`) }); |
| 108 | + assert.isTrue(re.test(rendered)); |
| 109 | + } |
| 110 | + } |
| 111 | + }, |
31 | 112 | ];
|
32 | 113 |
|
33 | 114 | testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });
|
0 commit comments