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: * Creation Date: 30 May 2001 michael@0: * Correction Date: 14 Aug 2001 michael@0: * michael@0: * SUMMARY: Regression test for bugs 83293, 103351 michael@0: * See http://bugzilla.mozilla.org/show_bug.cgi?id=83293 michael@0: * http://bugzilla.mozilla.org/show_bug.cgi?id=103351 michael@0: * http://bugzilla.mozilla.org/show_bug.cgi?id=92942 michael@0: * michael@0: * michael@0: * ******************** CORRECTION !!! ***************************** michael@0: * michael@0: * When I originally wrote this test, I thought this was true: michael@0: * str.replace(strA, strB) == str.replace(new RegExp(strA),strB). michael@0: * See ECMA-262 Final Draft, 15.5.4.11 String.prototype.replace michael@0: * michael@0: * However, in http://bugzilla.mozilla.org/show_bug.cgi?id=83293 michael@0: * Jim Ley points out the ECMA-262 Final Edition changed on this. michael@0: * String.prototype.replace (searchValue, replaceValue), if provided michael@0: * a searchValue that is not a RegExp, is NO LONGER to replace it with michael@0: * michael@0: * new RegExp(searchValue) michael@0: * but rather: michael@0: * String(searchValue) michael@0: * michael@0: * This puts the replace() method at variance with search() and match(), michael@0: * which continue to follow the RegExp conversion of the Final Draft. michael@0: * It also makes most of this testcase, as originally written, invalid. michael@0: ********************************************************************** michael@0: */ michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: var BUGNUMBER = 103351; // <--- (Outgrowth of original bug 83293) michael@0: var summ_OLD = 'Testing str.replace(strA, strB) == str.replace(new RegExp(strA),strB)'; michael@0: var summ_NEW = 'Testing String.prototype.replace(x,y) when x is a string'; michael@0: var summary = summ_NEW; michael@0: var status = ''; michael@0: var actual = ''; michael@0: var expect= ''; michael@0: var cnEmptyString = ''; michael@0: var str = 'abc'; michael@0: var strA = cnEmptyString; michael@0: var strB = 'Z'; michael@0: michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: test(); michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: michael@0: /* michael@0: * In this test, it's important to reportCompare() each other case michael@0: * BEFORE the last two cases are attempted. Don't store all results michael@0: * in an array and reportCompare() them at the end, as we usually do. michael@0: * michael@0: * When this bug was filed, str.replace(strA, strB) would return no value michael@0: * whatsoever if strA == cnEmptyString, and no error, either - michael@0: */ michael@0: function test() michael@0: { michael@0: enterFunc ('test'); michael@0: printBugNumber(BUGNUMBER); michael@0: printStatus (summary); michael@0: michael@0: /******************* THESE WERE INCORRECT; SEE ABOVE ************************ michael@0: status = 'Section A of test'; michael@0: strA = 'a'; michael@0: actual = str.replace(strA, strB); michael@0: expect = str.replace(new RegExp(strA), strB); michael@0: reportCompare(expect, actual, status); michael@0: michael@0: status = 'Section B of test'; michael@0: strA = 'x'; michael@0: actual = str.replace(strA, strB); michael@0: expect = str.replace(new RegExp(strA), strB); michael@0: reportCompare(expect, actual, status); michael@0: michael@0: status = 'Section C of test'; michael@0: strA = undefined; michael@0: actual = str.replace(strA, strB); michael@0: expect = str.replace(new RegExp(strA), strB); michael@0: reportCompare(expect, actual, status); michael@0: michael@0: status = 'Section D of test'; michael@0: strA = null; michael@0: actual = str.replace(strA, strB); michael@0: expect = str.replace(new RegExp(strA), strB); michael@0: reportCompare(expect, actual, status); michael@0: michael@0: michael@0: * This example is from jim@jibbering.com (see Bugzilla bug 92942) michael@0: * It is a variation on the example below. michael@0: * michael@0: * Namely, we are using the regexp /$/ instead of the regexp //. michael@0: * The regexp /$/ means we should match the "empty string" at the michael@0: * end-boundary of the word, instead of the one at the beginning. michael@0: * michael@0: status = 'Section E of test'; michael@0: var strJim = 'aa$aa'; michael@0: strA = '$'; michael@0: actual = strJim.replace(strA, strB); // bug -> 'aaZaa' michael@0: expect = strJim.replace(new RegExp(strA), strB); // expect 'aa$aaZ' michael@0: reportCompare(expect, actual, status); michael@0: michael@0: michael@0: * michael@0: * Note: 'Zabc' is the result we expect for 'abc'.replace('', 'Z'). michael@0: * michael@0: * The string '' is supposed to be equivalent to new RegExp('') = //. michael@0: * The regexp // means we should match the "empty string" conceived of michael@0: * at the beginning boundary of the word, before the first character. michael@0: * michael@0: status = 'Section F of test'; michael@0: strA = cnEmptyString; michael@0: actual = str.replace(strA, strB); michael@0: expect = 'Zabc'; michael@0: reportCompare(expect, actual, status); michael@0: michael@0: status = 'Section G of test'; michael@0: strA = cnEmptyString; michael@0: actual = str.replace(strA, strB); michael@0: expect = str.replace(new RegExp(strA), strB); michael@0: reportCompare(expect, actual, status); michael@0: michael@0: ************************* END OF INCORRECT CASES ****************************/ michael@0: michael@0: michael@0: ////////////////////////// OK, LET'S START OVER ////////////////////////////// michael@0: michael@0: status = 'Section 1 of test'; michael@0: actual = 'abc'.replace('a', 'Z'); michael@0: expect = 'Zbc'; michael@0: reportCompare(expect, actual, status); michael@0: michael@0: status = 'Section 2 of test'; michael@0: actual = 'abc'.replace('b', 'Z'); michael@0: expect = 'aZc'; michael@0: reportCompare(expect, actual, status); michael@0: michael@0: status = 'Section 3 of test'; michael@0: actual = 'abc'.replace(undefined, 'Z'); michael@0: expect = 'abc'; // String(undefined) == 'undefined'; no replacement possible michael@0: reportCompare(expect, actual, status); michael@0: michael@0: status = 'Section 4 of test'; michael@0: actual = 'abc'.replace(null, 'Z'); michael@0: expect = 'abc'; // String(null) == 'null'; no replacement possible michael@0: reportCompare(expect, actual, status); michael@0: michael@0: status = 'Section 5 of test'; michael@0: actual = 'abc'.replace(true, 'Z'); michael@0: expect = 'abc'; // String(true) == 'true'; no replacement possible michael@0: reportCompare(expect, actual, status); michael@0: michael@0: status = 'Section 6 of test'; michael@0: actual = 'abc'.replace(false, 'Z'); michael@0: expect = 'abc'; // String(false) == 'false'; no replacement possible michael@0: reportCompare(expect, actual, status); michael@0: michael@0: status = 'Section 7 of test'; michael@0: actual = 'aa$aa'.replace('$', 'Z'); michael@0: expect = 'aaZaa'; // NOT 'aa$aaZ' as in ECMA Final Draft; see above michael@0: reportCompare(expect, actual, status); michael@0: michael@0: status = 'Section 8 of test'; michael@0: actual = 'abc'.replace('.*', 'Z'); michael@0: expect = 'abc'; // not 'Z' as in EMCA Final Draft michael@0: reportCompare(expect, actual, status); michael@0: michael@0: status = 'Section 9 of test'; michael@0: actual = 'abc'.replace('', 'Z'); michael@0: expect = 'Zabc'; // Still expect 'Zabc' for this michael@0: reportCompare(expect, actual, status); michael@0: michael@0: exitFunc ('test'); michael@0: }