js/src/tests/ecma_5/RegExp/exec.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 * Any copyright is dedicated to the Public Domain.
michael@0 3 * http://creativecommons.org/licenses/publicdomain/
michael@0 4 */
michael@0 5
michael@0 6 var BUGNUMBER = 646490;
michael@0 7 var summary =
michael@0 8 "RegExp.prototype.exec doesn't get the lastIndex and ToInteger() it for " +
michael@0 9 "non-global regular expressions when it should";
michael@0 10
michael@0 11 print(BUGNUMBER + ": " + summary);
michael@0 12
michael@0 13 /**************
michael@0 14 * BEGIN TEST *
michael@0 15 **************/
michael@0 16
michael@0 17 function expectThrowTypeError(fun)
michael@0 18 {
michael@0 19 try
michael@0 20 {
michael@0 21 var r = fun();
michael@0 22 throw new Error("didn't throw TypeError, returned " + r);
michael@0 23 }
michael@0 24 catch (e)
michael@0 25 {
michael@0 26 assertEq(e instanceof TypeError, true,
michael@0 27 "didn't throw TypeError, got: " + e);
michael@0 28 }
michael@0 29 }
michael@0 30
michael@0 31 function checkExec(description, regex, args, obj)
michael@0 32 {
michael@0 33 var lastIndex = obj.lastIndex;
michael@0 34 var index = obj.index;
michael@0 35 var input = obj.input;
michael@0 36 var indexArray = obj.indexArray;
michael@0 37
michael@0 38 var res = regex.exec.apply(regex, args);
michael@0 39
michael@0 40 assertEq(Array.isArray(res), true, description + ": not an array");
michael@0 41 assertEq(regex.lastIndex, lastIndex, description + ": wrong lastIndex");
michael@0 42 assertEq(res.index, index, description + ": wrong index");
michael@0 43 assertEq(res.input, input, description + ": wrong input");
michael@0 44 assertEq(res.length, indexArray.length, description + ": wrong length");
michael@0 45 for (var i = 0, sz = indexArray.length; i < sz; i++)
michael@0 46 assertEq(res[i], indexArray[i], description + " " + i + ": wrong index value");
michael@0 47 }
michael@0 48
michael@0 49 var exec = RegExp.prototype.exec;
michael@0 50 var r, res, called, obj;
michael@0 51
michael@0 52 /* 1. Let R be this RegExp object. */
michael@0 53 expectThrowTypeError(function() { exec.call(null); });
michael@0 54 expectThrowTypeError(function() { exec.call(""); });
michael@0 55 expectThrowTypeError(function() { exec.call(5); });
michael@0 56 expectThrowTypeError(function() { exec.call({}); });
michael@0 57 expectThrowTypeError(function() { exec.call([]); });
michael@0 58 expectThrowTypeError(function() { exec.call(); });
michael@0 59 expectThrowTypeError(function() { exec.call(true); });
michael@0 60 expectThrowTypeError(function() { exec.call(Object.create(RegExp.prototype)); });
michael@0 61 expectThrowTypeError(function() { exec.call(Object.create(/a/)); });
michael@0 62
michael@0 63
michael@0 64 /* 2. Let S be the value of ToString(string). */
michael@0 65 called = false;
michael@0 66 r = /a/;
michael@0 67 assertEq(r.lastIndex, 0);
michael@0 68
michael@0 69 checkExec("/a/", r, [{ toString: function() { called = true; return 'ba'; } }],
michael@0 70 { lastIndex: 0,
michael@0 71 index: 1,
michael@0 72 input: "ba",
michael@0 73 indexArray: ["a"] });
michael@0 74 assertEq(called, true);
michael@0 75
michael@0 76 called = false;
michael@0 77 try
michael@0 78 {
michael@0 79 res = r.exec({ toString: null, valueOf: function() { called = true; throw 17; } });
michael@0 80 throw new Error("didn't throw");
michael@0 81 }
michael@0 82 catch (e)
michael@0 83 {
michael@0 84 assertEq(e, 17);
michael@0 85 }
michael@0 86
michael@0 87 assertEq(called, true);
michael@0 88
michael@0 89 called = false;
michael@0 90 obj = r.lastIndex = { valueOf: function() { assertEq(true, false, "shouldn't have been called"); } };
michael@0 91 try
michael@0 92 {
michael@0 93 res = r.exec({ toString: null, valueOf: function() { assertEq(called, false); called = true; throw 17; } });
michael@0 94 throw new Error("didn't throw");
michael@0 95 }
michael@0 96 catch (e)
michael@0 97 {
michael@0 98 assertEq(e, 17);
michael@0 99 }
michael@0 100
michael@0 101 assertEq(called, true);
michael@0 102 assertEq(r.lastIndex, obj);
michael@0 103
michael@0 104 // We don't test lack of an argument because of RegExp statics non-standard
michael@0 105 // behaviors overriding what really should happen for lack of an argument, sigh.
michael@0 106
michael@0 107
michael@0 108 /*
michael@0 109 * 3. Let length be the length of S.
michael@0 110 * 4. Let lastIndex be the result of calling the [[Get]] internal method of R with argument "lastIndex".
michael@0 111 * 5. Let i be the value of ToInteger(lastIndex).
michael@0 112 */
michael@0 113 r = /b/;
michael@0 114 r.lastIndex = { valueOf: {}, toString: {} };
michael@0 115 expectThrowTypeError(function() { r.exec("foopy"); });
michael@0 116 r.lastIndex = { valueOf: function() { throw new TypeError(); } };
michael@0 117 expectThrowTypeError(function() { r.exec("foopy"); });
michael@0 118
michael@0 119
michael@0 120 /*
michael@0 121 * 6. Let global be the result of calling the [[Get]] internal method of R with argument "global".
michael@0 122 * 7. If global is false, then let i = 0.
michael@0 123 */
michael@0 124 obj = { valueOf: function() { return 5; } };
michael@0 125 r = /abc/;
michael@0 126 r.lastIndex = obj;
michael@0 127
michael@0 128 checkExec("/abc/ take one", r, ["abc-------abc"],
michael@0 129 { lastIndex: obj,
michael@0 130 index: 0,
michael@0 131 input: "abc-------abc",
michael@0 132 indexArray: ["abc"] });
michael@0 133
michael@0 134 checkExec("/abc/ take two", r, ["abc-------abc"],
michael@0 135 { lastIndex: obj,
michael@0 136 index: 0,
michael@0 137 input: "abc-------abc",
michael@0 138 indexArray: ["abc"] });
michael@0 139
michael@0 140
michael@0 141 /*
michael@0 142 * 8. Let matchSucceeded be false.
michael@0 143 * 9. Repeat, while matchSucceeded is false
michael@0 144 * a. If i < 0 or i > length, then
michael@0 145 * i. Call the [[Put]] internal method of R with arguments "lastIndex", 0, and true.
michael@0 146 * ii. Return null.
michael@0 147 * b. Call the [[Match]] internal method of R with arguments S and i.
michael@0 148 * c. If [[Match]] returned failure, then
michael@0 149 * i. Let i = i+1.
michael@0 150 * d. else
michael@0 151 * i. Let r be the State result of the call to [[Match]].
michael@0 152 * ii. Set matchSucceeded to true.
michael@0 153 * e. Let i = i+1.
michael@0 154 */
michael@0 155 r = /abc()?/;
michael@0 156 r.lastIndex = -5;
michael@0 157 checkExec("/abc()?/ with lastIndex -5", r, ["abc-------abc"],
michael@0 158 { lastIndex: -5,
michael@0 159 index: 0,
michael@0 160 input: "abc-------abc",
michael@0 161 indexArray: ["abc", undefined] });
michael@0 162
michael@0 163
michael@0 164 r = /abc/;
michael@0 165 r.lastIndex = -17;
michael@0 166 res = r.exec("cdefg");
michael@0 167 assertEq(res, null);
michael@0 168 assertEq(r.lastIndex, 0);
michael@0 169
michael@0 170 r = /abc/g;
michael@0 171 r.lastIndex = -42;
michael@0 172 res = r.exec("cdefg");
michael@0 173 assertEq(res, null);
michael@0 174 assertEq(r.lastIndex, 0);
michael@0 175
michael@0 176
michael@0 177 /*
michael@0 178 * 10. Let e be r's endIndex value.
michael@0 179 * 11. If global is true,
michael@0 180 * a. Call the [[Put]] internal method of R with arguments "lastIndex", e, and true.
michael@0 181 */
michael@0 182 r = /abc/g;
michael@0 183 r.lastIndex = 17;
michael@0 184 assertEq(r.exec("sdfs"), null);
michael@0 185 assertEq(r.lastIndex, 0);
michael@0 186
michael@0 187 r = /abc/g;
michael@0 188 r.lastIndex = 2;
michael@0 189 checkExec("/abc/g", r, ["00abc"],
michael@0 190 { lastIndex: 5,
michael@0 191 index: 2,
michael@0 192 input: "00abc",
michael@0 193 indexArray: ["abc"] });
michael@0 194
michael@0 195
michael@0 196
michael@0 197 r = /a(b)c/g;
michael@0 198 r.lastIndex = 2;
michael@0 199 checkExec("/a(b)c/g take two", r, ["00abcd"],
michael@0 200 { lastIndex: 5,
michael@0 201 index: 2,
michael@0 202 input: "00abcd",
michael@0 203 indexArray: ["abc", "b"] });
michael@0 204
michael@0 205
michael@0 206 /*
michael@0 207 * 12. Let n be the length of r's captures array. (This is the same value as
michael@0 208 * 15.10.2.1's NCapturingParens.)
michael@0 209 * 13. Let A be a new array created as if by the expression new Array() where
michael@0 210 * Array is the standard built-in constructor with that name.
michael@0 211 * 14. Let matchIndex be the position of the matched substring within the
michael@0 212 * complete String S.
michael@0 213 * 15. Call the [[DefineOwnProperty]] internal method of A with arguments
michael@0 214 * "index", Property Descriptor {[[Value]]: matchIndex, [[Writable]: true,
michael@0 215 * [[Enumerable]]: true, [[Configurable]]: true}, and true.
michael@0 216 * 16. Call the [[DefineOwnProperty]] internal method of A with arguments
michael@0 217 * "input", Property Descriptor {[[Value]]: S, [[Writable]: true,
michael@0 218 * [[Enumerable]]: true, [[Configurable]]: true}, and true.
michael@0 219 * 17. Call the [[DefineOwnProperty]] internal method of A with arguments
michael@0 220 * "length", Property Descriptor {[[Value]]: n + 1}, and true.
michael@0 221 * 18. Let matchedSubstr be the matched substring (i.e. the portion of S
michael@0 222 * between offset i inclusive and offset e exclusive).
michael@0 223 * 19. Call the [[DefineOwnProperty]] internal method of A with arguments "0",
michael@0 224 * Property Descriptor {[[Value]]: matchedSubstr, [[Writable]: true,
michael@0 225 * [[Enumerable]]: true, [[Configurable]]: true}, and true.
michael@0 226 * 20. For each integer i such that I > 0 and I ≤ n
michael@0 227 * a. Let captureI be i th element of r's captures array.
michael@0 228 * b. Call the [[DefineOwnProperty]] internal method of A with arguments
michael@0 229 * ToString(i), Property Descriptor {[[Value]]: captureI, [[Writable]:
michael@0 230 * true, [[Enumerable]]: true, [[Configurable]]: true}, and true.
michael@0 231 * 21. Return A.
michael@0 232 */
michael@0 233 // throughout, above (and in other tests)
michael@0 234
michael@0 235 /******************************************************************************/
michael@0 236
michael@0 237 if (typeof reportCompare === "function")
michael@0 238 reportCompare(true, true);
michael@0 239
michael@0 240 print("All tests passed!");

mercurial