michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * michael@0: * Date: 11 Nov 2002 michael@0: * SUMMARY: JS shouldn't crash on extraneous args to str.match(), etc. michael@0: * See http://bugzilla.mozilla.org/show_bug.cgi?id=179524 michael@0: * michael@0: * Note that when testing str.replace(), we have to be careful if the first michael@0: * argument provided to str.replace() is not a regexp object. ECMA-262 says michael@0: * it is NOT converted to one, unlike the case for str.match(), str.search(). michael@0: * michael@0: * See http://bugzilla.mozilla.org/show_bug.cgi?id=83293#c21. This means michael@0: * we have to be careful how we test meta-characters in the first argument michael@0: * to str.replace(), if that argument is a string - michael@0: */ michael@0: //----------------------------------------------------------------------------- michael@0: var UBound = 0; michael@0: var BUGNUMBER = 179524; michael@0: var summary = "Don't crash on extraneous arguments to str.match(), etc."; michael@0: var status = ''; michael@0: var statusitems = []; michael@0: var actual = ''; michael@0: var actualvalues = []; michael@0: var expect= ''; michael@0: var expectedvalues = []; michael@0: michael@0: michael@0: str = 'ABC abc'; michael@0: var re = /z/ig; michael@0: michael@0: status = inSection(1); michael@0: actual = str.match(re); michael@0: expect = null; michael@0: addThis(); michael@0: michael@0: status = inSection(2); michael@0: actual = str.match(re, 'i'); michael@0: expect = null; michael@0: addThis(); michael@0: michael@0: status = inSection(3); michael@0: actual = str.match(re, 'g', ''); michael@0: expect = null; michael@0: addThis(); michael@0: michael@0: status = inSection(4); michael@0: actual = str.match(re, 'z', new Object(), new Date()); michael@0: expect = null; michael@0: addThis(); michael@0: michael@0: michael@0: /* michael@0: * Now try the same thing with str.search() michael@0: */ michael@0: status = inSection(5); michael@0: actual = str.search(re); michael@0: expect = -1; michael@0: addThis(); michael@0: michael@0: status = inSection(6); michael@0: actual = str.search(re, 'i'); michael@0: expect = -1; michael@0: addThis(); michael@0: michael@0: status = inSection(7); michael@0: actual = str.search(re, 'g', ''); michael@0: expect = -1; michael@0: addThis(); michael@0: michael@0: status = inSection(8); michael@0: actual = str.search(re, 'z', new Object(), new Date()); michael@0: expect = -1; michael@0: addThis(); michael@0: michael@0: michael@0: /* michael@0: * Now try the same thing with str.replace() michael@0: */ michael@0: status = inSection(9); michael@0: actual = str.replace(re, 'Z'); michael@0: expect = str; michael@0: addThis(); michael@0: michael@0: status = inSection(10); michael@0: actual = str.replace(re, 'Z', 'i'); michael@0: expect = str; michael@0: addThis(); michael@0: michael@0: status = inSection(11); michael@0: actual = str.replace(re, 'Z', 'g', ''); michael@0: expect = str; michael@0: addThis(); michael@0: michael@0: status = inSection(12); michael@0: actual = str.replace(re, 'Z', 'z', new Object(), new Date()); michael@0: expect = str; michael@0: addThis(); michael@0: michael@0: michael@0: michael@0: /* michael@0: * Now test the case where str.match()'s first argument is not a regexp object. michael@0: * In that case, JS follows ECMA-262 Ed.3 by converting the 1st argument to a michael@0: * regexp object using the argument as a regexp pattern, but then extends ECMA michael@0: * by taking any optional 2nd argument to be a regexp flag string (e.g.'ig'). michael@0: * michael@0: * Reference: http://bugzilla.mozilla.org/show_bug.cgi?id=179524#c10 michael@0: */ michael@0: status = inSection(13); michael@0: actual = str.match('a').toString(); michael@0: expect = str.match(/a/).toString(); michael@0: addThis(); michael@0: michael@0: status = inSection(14); michael@0: actual = str.match('a', 'i').toString(); michael@0: expect = str.match(/a/i).toString(); michael@0: addThis(); michael@0: michael@0: status = inSection(15); michael@0: actual = str.match('a', 'ig').toString(); michael@0: expect = str.match(/a/ig).toString(); michael@0: addThis(); michael@0: michael@0: status = inSection(16); michael@0: actual = str.match('\\s', 'm').toString(); michael@0: expect = str.match(/\s/m).toString(); michael@0: addThis(); michael@0: michael@0: michael@0: /* michael@0: * Now try the previous three cases with extraneous parameters michael@0: */ michael@0: status = inSection(17); michael@0: actual = str.match('a', 'i', 'g').toString(); michael@0: expect = str.match(/a/i).toString(); michael@0: addThis(); michael@0: michael@0: status = inSection(18); michael@0: actual = str.match('a', 'ig', new Object()).toString(); michael@0: expect = str.match(/a/ig).toString(); michael@0: addThis(); michael@0: michael@0: status = inSection(19); michael@0: actual = str.match('\\s', 'm', 999).toString(); michael@0: expect = str.match(/\s/m).toString(); michael@0: addThis(); michael@0: michael@0: michael@0: /* michael@0: * Try an invalid second parameter (i.e. an invalid regexp flag) michael@0: */ michael@0: status = inSection(20); michael@0: try michael@0: { michael@0: actual = str.match('a', 'z').toString(); michael@0: expect = 'SHOULD HAVE FALLEN INTO CATCH-BLOCK!'; michael@0: addThis(); michael@0: } michael@0: catch (e) michael@0: { michael@0: actual = e instanceof SyntaxError; michael@0: expect = true; michael@0: addThis(); michael@0: } michael@0: michael@0: michael@0: michael@0: /* michael@0: * Now test str.search() where the first argument is not a regexp object. michael@0: * The same considerations as above apply - michael@0: * michael@0: * Reference: http://bugzilla.mozilla.org/show_bug.cgi?id=179524#c16 michael@0: */ michael@0: status = inSection(21); michael@0: actual = str.search('a'); michael@0: expect = str.search(/a/); michael@0: addThis(); michael@0: michael@0: status = inSection(22); michael@0: actual = str.search('a', 'i'); michael@0: expect = str.search(/a/i); michael@0: addThis(); michael@0: michael@0: status = inSection(23); michael@0: actual = str.search('a', 'ig'); michael@0: expect = str.search(/a/ig); michael@0: addThis(); michael@0: michael@0: status = inSection(24); michael@0: actual = str.search('\\s', 'm'); michael@0: expect = str.search(/\s/m); michael@0: addThis(); michael@0: michael@0: michael@0: /* michael@0: * Now try the previous three cases with extraneous parameters michael@0: */ michael@0: status = inSection(25); michael@0: actual = str.search('a', 'i', 'g'); michael@0: expect = str.search(/a/i); michael@0: addThis(); michael@0: michael@0: status = inSection(26); michael@0: actual = str.search('a', 'ig', new Object()); michael@0: expect = str.search(/a/ig); michael@0: addThis(); michael@0: michael@0: status = inSection(27); michael@0: actual = str.search('\\s', 'm', 999); michael@0: expect = str.search(/\s/m); michael@0: addThis(); michael@0: michael@0: michael@0: /* michael@0: * Try an invalid second parameter (i.e. an invalid regexp flag) michael@0: */ michael@0: status = inSection(28); michael@0: try michael@0: { michael@0: actual = str.search('a', 'z'); michael@0: expect = 'SHOULD HAVE FALLEN INTO CATCH-BLOCK!'; michael@0: addThis(); michael@0: } michael@0: catch (e) michael@0: { michael@0: actual = e instanceof SyntaxError; michael@0: expect = true; michael@0: addThis(); michael@0: } michael@0: michael@0: michael@0: michael@0: /* michael@0: * Now test str.replace() where the first argument is not a regexp object. michael@0: * The same considerations as above apply, EXCEPT for meta-characters. michael@0: * See introduction to testcase above. References: michael@0: * michael@0: * http://bugzilla.mozilla.org/show_bug.cgi?id=179524#c16 michael@0: * http://bugzilla.mozilla.org/show_bug.cgi?id=83293#c21 michael@0: */ michael@0: status = inSection(29); michael@0: actual = str.replace('a', 'Z'); michael@0: expect = str.replace(/a/, 'Z'); michael@0: addThis(); michael@0: michael@0: status = inSection(30); michael@0: actual = str.replace('a', 'Z', 'i'); michael@0: expect = str.replace(/a/i, 'Z'); michael@0: addThis(); michael@0: michael@0: status = inSection(31); michael@0: actual = str.replace('a', 'Z', 'ig'); michael@0: expect = str.replace(/a/ig, 'Z'); michael@0: addThis(); michael@0: michael@0: status = inSection(32); michael@0: actual = str.replace('\\s', 'Z', 'm'); //<--- NO!!! No meta-characters 1st arg! michael@0: actual = str.replace(' ', 'Z', 'm'); //<--- Have to do this instead michael@0: expect = str.replace(/\s/m, 'Z'); michael@0: addThis(); michael@0: michael@0: michael@0: /* michael@0: * Now try the previous three cases with extraneous parameters michael@0: */ michael@0: status = inSection(33); michael@0: actual = str.replace('a', 'Z', 'i', 'g'); michael@0: expect = str.replace(/a/i, 'Z'); michael@0: addThis(); michael@0: michael@0: status = inSection(34); michael@0: actual = str.replace('a', 'Z', 'ig', new Object()); michael@0: expect = str.replace(/a/ig, 'Z'); michael@0: addThis(); michael@0: michael@0: status = inSection(35); michael@0: actual = str.replace('\\s', 'Z', 'm', 999); //<--- NO meta-characters 1st arg! michael@0: actual = str.replace(' ', 'Z', 'm', 999); //<--- Have to do this instead michael@0: expect = str.replace(/\s/m, 'Z'); michael@0: addThis(); michael@0: michael@0: michael@0: /* michael@0: * Try an invalid third parameter (i.e. an invalid regexp flag) michael@0: */ michael@0: status = inSection(36); michael@0: try michael@0: { michael@0: actual = str.replace('a', 'Z', 'z'); michael@0: expect = 'SHOULD HAVE FALLEN INTO CATCH-BLOCK!'; michael@0: addThis(); michael@0: } michael@0: catch (e) michael@0: { michael@0: actual = e instanceof SyntaxError; michael@0: expect = true; michael@0: addThis(); michael@0: } michael@0: michael@0: michael@0: michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: test(); michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: michael@0: michael@0: function addThis() michael@0: { michael@0: statusitems[UBound] = status; michael@0: actualvalues[UBound] = actual; michael@0: expectedvalues[UBound] = expect; michael@0: UBound++; michael@0: } michael@0: michael@0: michael@0: function test() michael@0: { michael@0: enterFunc('test'); michael@0: printBugNumber(BUGNUMBER); michael@0: printStatus(summary); michael@0: michael@0: for (var i=0; i