|
| 1 | +// With verbose set to false, this test is successful if there is no output. Set verbose to true to see expected matches. |
| 2 | +let verbose = false; |
| 3 | + |
| 4 | +function arrayToString(arr) |
| 5 | +{ |
| 6 | + let str = ''; |
| 7 | + arr.forEach(function(v, index) { |
| 8 | + if (typeof v == "string") |
| 9 | + str += "\"" + v + "\""; |
| 10 | + else |
| 11 | + str += v; |
| 12 | + |
| 13 | + if (index != (arr.length - 1)) |
| 14 | + str += ','; |
| 15 | + }); |
| 16 | + return str; |
| 17 | +} |
| 18 | + |
| 19 | +function objectToString(obj) |
| 20 | +{ |
| 21 | + let str = ""; |
| 22 | + |
| 23 | + firstEntry = true; |
| 24 | + |
| 25 | + for (const [key, value] of Object.entries(obj)) { |
| 26 | + if (!firstEntry) |
| 27 | + str += ", "; |
| 28 | + |
| 29 | + str += key + ": " + dumpValue(value); |
| 30 | + |
| 31 | + firstEntry = false; |
| 32 | + } |
| 33 | + |
| 34 | + return "{ " + str + " }"; |
| 35 | +} |
| 36 | + |
| 37 | +function dumpValue(v) |
| 38 | +{ |
| 39 | + if (v === null) |
| 40 | + return "<null>"; |
| 41 | + |
| 42 | + if (v === undefined) |
| 43 | + return "<undefined>"; |
| 44 | + |
| 45 | + if (typeof v == "string") |
| 46 | + return "\"" + v + "\""; |
| 47 | + |
| 48 | + let str = ""; |
| 49 | + |
| 50 | + if (v.length) |
| 51 | + str += arrayToString(v); |
| 52 | + |
| 53 | + if (v.groups) { |
| 54 | + groupStr = objectToString(v.groups); |
| 55 | + |
| 56 | + if (str.length) { |
| 57 | + if ( groupStr.length) |
| 58 | + str += ", " + groupStr; |
| 59 | + } else |
| 60 | + str = groupStr; |
| 61 | + } |
| 62 | + |
| 63 | + return "[ " + str + " ]"; |
| 64 | +} |
| 65 | + |
| 66 | +function compareArray(expected, actual) |
| 67 | +{ |
| 68 | + if (expected === null && actual === null) |
| 69 | + return true; |
| 70 | + |
| 71 | + if (expected === null) { |
| 72 | + print("### expected is null, actual is not null"); |
| 73 | + return false; |
| 74 | + } |
| 75 | + |
| 76 | + if (actual === null) { |
| 77 | + print("### expected is not null, actual is null"); |
| 78 | + return false; |
| 79 | + } |
| 80 | + |
| 81 | + if (expected.length !== actual.length) { |
| 82 | + print("### expected.length: " + expected.length + ", actual.length: " + actual.length); |
| 83 | + return false; |
| 84 | + } |
| 85 | + |
| 86 | + for (var i = 0; i < expected.length; i++) { |
| 87 | + if (expected[i] !== actual[i]) { |
| 88 | + print("### expected[" + i + "]: \"" + expected[i] + "\" !== actual[" + i + "]: \"" + actual[i] + "\""); |
| 89 | + return false; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return true; |
| 94 | +} |
| 95 | + |
| 96 | +function compareGroups(expected, actual) |
| 97 | +{ |
| 98 | + if (expected === null && actual === null) |
| 99 | + return true; |
| 100 | + |
| 101 | + if (expected === null) { |
| 102 | + print("### expected group is null, actual group is not null"); |
| 103 | + return false; |
| 104 | + } |
| 105 | + |
| 106 | + if (actual === null) { |
| 107 | + print("### expected group is not null, actual group is null"); |
| 108 | + return false; |
| 109 | + } |
| 110 | + |
| 111 | + for (const key in expected) { |
| 112 | + if (expected[key] !== actual[key]) { |
| 113 | + print("### expected." + key + ": " + dumpValue(expected[key]) + " !== actual." + key + ": " + dumpValue(actual[key])); |
| 114 | + return false; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + return true; |
| 119 | +} |
| 120 | + |
| 121 | +let testNumber = 0; |
| 122 | + |
| 123 | +function testRegExp(re, str, exp, groups) |
| 124 | +{ |
| 125 | + testNumber++; |
| 126 | + |
| 127 | + if (groups) |
| 128 | + exp.groups = groups; |
| 129 | + |
| 130 | + let actual = re.exec(str); |
| 131 | + |
| 132 | + let result = compareArray(exp, actual);; |
| 133 | + |
| 134 | + if (exp && exp.groups) { |
| 135 | + if (!compareGroups(exp.groups, actual.groups)) |
| 136 | + result = false; |
| 137 | + } |
| 138 | + |
| 139 | + if (result) { |
| 140 | + if (verbose) |
| 141 | + print(re.toString() + ".exec(" + dumpValue(str) + "), passed ", dumpValue(exp)); |
| 142 | + } else |
| 143 | + print(re.toString() + ".exec(" + dumpValue(str) + "), FAILED test #" + testNumber + ", Expected ", dumpValue(exp), " got ", dumpValue(actual)); |
| 144 | +} |
| 145 | + |
| 146 | +function testRegExpSyntaxError(reString, flags, expError) |
| 147 | +{ |
| 148 | + testNumber++; |
| 149 | + |
| 150 | + |
| 151 | + try { |
| 152 | + let re = new RegExp(reString, flags); |
| 153 | + print("FAILED test #" + testNumber + ", Expected /" + reString + "/" + flags + " to throw \"" + expError + "\", but it didn't"); |
| 154 | + } catch (e) { |
| 155 | + if (e != expError) |
| 156 | + print("FAILED test #" + testNumber + ", Expected /" + reString + "/" + flags + " to throw \"" + expError + "\" got \"" + e + "\""); |
| 157 | + else if (verbose) |
| 158 | + print("/" + reString + "/" + flags + " passed, it threw \"" + expError + "\" as expected"); |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +let re = /^(?:break|case|catch|continue|debugger|default|do|else|finally|for|function|if|return|switch|throw|try|var|while|with|null|true|false|instanceof|typeof|void|delete|new|in|this)/; |
| 163 | + |
| 164 | +for (i = 0; i < 1000000; i++) { |
| 165 | + testRegExp(re, "function", ["function"]); |
| 166 | + testRegExp(re, "return", ["return"]); |
| 167 | + testRegExp(re, "let", null); |
| 168 | +} |
| 169 | + |
| 170 | +let re1 = /^(?:break|case|catch|continue|debugger|default|do|else|finally|for|function|if|return|switch|throw|try|var|while|with|null|true|false|instanceof|typeof|void|delete|new|in|this)$/; |
| 171 | + |
| 172 | +for (i = 0; i < 1000000; i++) { |
| 173 | + testRegExp(re1, "throw", ["throw"]); |
| 174 | + testRegExp(re1, "while", ["while"]); |
| 175 | +} |
0 commit comments