1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/tests/ecma_3/String/regress-83293.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,182 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 + 1.10 +/* 1.11 + * Creation Date: 30 May 2001 1.12 + * Correction Date: 14 Aug 2001 1.13 + * 1.14 + * SUMMARY: Regression test for bugs 83293, 103351 1.15 + * See http://bugzilla.mozilla.org/show_bug.cgi?id=83293 1.16 + * http://bugzilla.mozilla.org/show_bug.cgi?id=103351 1.17 + * http://bugzilla.mozilla.org/show_bug.cgi?id=92942 1.18 + * 1.19 + * 1.20 + * ******************** CORRECTION !!! ***************************** 1.21 + * 1.22 + * When I originally wrote this test, I thought this was true: 1.23 + * str.replace(strA, strB) == str.replace(new RegExp(strA),strB). 1.24 + * See ECMA-262 Final Draft, 15.5.4.11 String.prototype.replace 1.25 + * 1.26 + * However, in http://bugzilla.mozilla.org/show_bug.cgi?id=83293 1.27 + * Jim Ley points out the ECMA-262 Final Edition changed on this. 1.28 + * String.prototype.replace (searchValue, replaceValue), if provided 1.29 + * a searchValue that is not a RegExp, is NO LONGER to replace it with 1.30 + * 1.31 + * new RegExp(searchValue) 1.32 + * but rather: 1.33 + * String(searchValue) 1.34 + * 1.35 + * This puts the replace() method at variance with search() and match(), 1.36 + * which continue to follow the RegExp conversion of the Final Draft. 1.37 + * It also makes most of this testcase, as originally written, invalid. 1.38 + ********************************************************************** 1.39 + */ 1.40 + 1.41 +//----------------------------------------------------------------------------- 1.42 +var BUGNUMBER = 103351; // <--- (Outgrowth of original bug 83293) 1.43 +var summ_OLD = 'Testing str.replace(strA, strB) == str.replace(new RegExp(strA),strB)'; 1.44 +var summ_NEW = 'Testing String.prototype.replace(x,y) when x is a string'; 1.45 +var summary = summ_NEW; 1.46 +var status = ''; 1.47 +var actual = ''; 1.48 +var expect= ''; 1.49 +var cnEmptyString = ''; 1.50 +var str = 'abc'; 1.51 +var strA = cnEmptyString; 1.52 +var strB = 'Z'; 1.53 + 1.54 + 1.55 +//----------------------------------------------------------------------------- 1.56 +test(); 1.57 +//----------------------------------------------------------------------------- 1.58 + 1.59 + 1.60 +/* 1.61 + * In this test, it's important to reportCompare() each other case 1.62 + * BEFORE the last two cases are attempted. Don't store all results 1.63 + * in an array and reportCompare() them at the end, as we usually do. 1.64 + * 1.65 + * When this bug was filed, str.replace(strA, strB) would return no value 1.66 + * whatsoever if strA == cnEmptyString, and no error, either - 1.67 + */ 1.68 +function test() 1.69 +{ 1.70 + enterFunc ('test'); 1.71 + printBugNumber(BUGNUMBER); 1.72 + printStatus (summary); 1.73 + 1.74 +/******************* THESE WERE INCORRECT; SEE ABOVE ************************ 1.75 + status = 'Section A of test'; 1.76 + strA = 'a'; 1.77 + actual = str.replace(strA, strB); 1.78 + expect = str.replace(new RegExp(strA), strB); 1.79 + reportCompare(expect, actual, status); 1.80 + 1.81 + status = 'Section B of test'; 1.82 + strA = 'x'; 1.83 + actual = str.replace(strA, strB); 1.84 + expect = str.replace(new RegExp(strA), strB); 1.85 + reportCompare(expect, actual, status); 1.86 + 1.87 + status = 'Section C of test'; 1.88 + strA = undefined; 1.89 + actual = str.replace(strA, strB); 1.90 + expect = str.replace(new RegExp(strA), strB); 1.91 + reportCompare(expect, actual, status); 1.92 + 1.93 + status = 'Section D of test'; 1.94 + strA = null; 1.95 + actual = str.replace(strA, strB); 1.96 + expect = str.replace(new RegExp(strA), strB); 1.97 + reportCompare(expect, actual, status); 1.98 + 1.99 + 1.100 + * This example is from jim@jibbering.com (see Bugzilla bug 92942) 1.101 + * It is a variation on the example below. 1.102 + * 1.103 + * Namely, we are using the regexp /$/ instead of the regexp //. 1.104 + * The regexp /$/ means we should match the "empty string" at the 1.105 + * end-boundary of the word, instead of the one at the beginning. 1.106 + * 1.107 + status = 'Section E of test'; 1.108 + var strJim = 'aa$aa'; 1.109 + strA = '$'; 1.110 + actual = strJim.replace(strA, strB); // bug -> 'aaZaa' 1.111 + expect = strJim.replace(new RegExp(strA), strB); // expect 'aa$aaZ' 1.112 + reportCompare(expect, actual, status); 1.113 + 1.114 + 1.115 + * 1.116 + * Note: 'Zabc' is the result we expect for 'abc'.replace('', 'Z'). 1.117 + * 1.118 + * The string '' is supposed to be equivalent to new RegExp('') = //. 1.119 + * The regexp // means we should match the "empty string" conceived of 1.120 + * at the beginning boundary of the word, before the first character. 1.121 + * 1.122 + status = 'Section F of test'; 1.123 + strA = cnEmptyString; 1.124 + actual = str.replace(strA, strB); 1.125 + expect = 'Zabc'; 1.126 + reportCompare(expect, actual, status); 1.127 + 1.128 + status = 'Section G of test'; 1.129 + strA = cnEmptyString; 1.130 + actual = str.replace(strA, strB); 1.131 + expect = str.replace(new RegExp(strA), strB); 1.132 + reportCompare(expect, actual, status); 1.133 + 1.134 + ************************* END OF INCORRECT CASES ****************************/ 1.135 + 1.136 + 1.137 +////////////////////////// OK, LET'S START OVER ////////////////////////////// 1.138 + 1.139 + status = 'Section 1 of test'; 1.140 + actual = 'abc'.replace('a', 'Z'); 1.141 + expect = 'Zbc'; 1.142 + reportCompare(expect, actual, status); 1.143 + 1.144 + status = 'Section 2 of test'; 1.145 + actual = 'abc'.replace('b', 'Z'); 1.146 + expect = 'aZc'; 1.147 + reportCompare(expect, actual, status); 1.148 + 1.149 + status = 'Section 3 of test'; 1.150 + actual = 'abc'.replace(undefined, 'Z'); 1.151 + expect = 'abc'; // String(undefined) == 'undefined'; no replacement possible 1.152 + reportCompare(expect, actual, status); 1.153 + 1.154 + status = 'Section 4 of test'; 1.155 + actual = 'abc'.replace(null, 'Z'); 1.156 + expect = 'abc'; // String(null) == 'null'; no replacement possible 1.157 + reportCompare(expect, actual, status); 1.158 + 1.159 + status = 'Section 5 of test'; 1.160 + actual = 'abc'.replace(true, 'Z'); 1.161 + expect = 'abc'; // String(true) == 'true'; no replacement possible 1.162 + reportCompare(expect, actual, status); 1.163 + 1.164 + status = 'Section 6 of test'; 1.165 + actual = 'abc'.replace(false, 'Z'); 1.166 + expect = 'abc'; // String(false) == 'false'; no replacement possible 1.167 + reportCompare(expect, actual, status); 1.168 + 1.169 + status = 'Section 7 of test'; 1.170 + actual = 'aa$aa'.replace('$', 'Z'); 1.171 + expect = 'aaZaa'; // NOT 'aa$aaZ' as in ECMA Final Draft; see above 1.172 + reportCompare(expect, actual, status); 1.173 + 1.174 + status = 'Section 8 of test'; 1.175 + actual = 'abc'.replace('.*', 'Z'); 1.176 + expect = 'abc'; // not 'Z' as in EMCA Final Draft 1.177 + reportCompare(expect, actual, status); 1.178 + 1.179 + status = 'Section 9 of test'; 1.180 + actual = 'abc'.replace('', 'Z'); 1.181 + expect = 'Zabc'; // Still expect 'Zabc' for this 1.182 + reportCompare(expect, actual, status); 1.183 + 1.184 + exitFunc ('test'); 1.185 +}