regress-83293.js (6210B)
1 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ 2 /* This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 7 /* 8 * Creation Date: 30 May 2001 9 * Correction Date: 14 Aug 2001 10 * 11 * SUMMARY: Regression test for bugs 83293, 103351 12 * See http://bugzilla.mozilla.org/show_bug.cgi?id=83293 13 * http://bugzilla.mozilla.org/show_bug.cgi?id=103351 14 * http://bugzilla.mozilla.org/show_bug.cgi?id=92942 15 * 16 * 17 * ******************** CORRECTION !!! ***************************** 18 * 19 * When I originally wrote this test, I thought this was true: 20 * str.replace(strA, strB) == str.replace(new RegExp(strA),strB). 21 * See ECMA-262 Final Draft, 15.5.4.11 String.prototype.replace 22 * 23 * However, in http://bugzilla.mozilla.org/show_bug.cgi?id=83293 24 * Jim Ley points out the ECMA-262 Final Edition changed on this. 25 * String.prototype.replace (searchValue, replaceValue), if provided 26 * a searchValue that is not a RegExp, is NO LONGER to replace it with 27 * 28 * new RegExp(searchValue) 29 * but rather: 30 * String(searchValue) 31 * 32 * This puts the replace() method at variance with search() and match(), 33 * which continue to follow the RegExp conversion of the Final Draft. 34 * It also makes most of this testcase, as originally written, invalid. 35 ********************************************************************** 36 */ 37 38 //----------------------------------------------------------------------------- 39 var BUGNUMBER = 103351; // <--- (Outgrowth of original bug 83293) 40 var summ_OLD = 'Testing str.replace(strA, strB) == str.replace(new RegExp(strA),strB)'; 41 var summ_NEW = 'Testing String.prototype.replace(x,y) when x is a string'; 42 var summary = summ_NEW; 43 var status = ''; 44 var actual = ''; 45 var expect= ''; 46 var cnEmptyString = ''; 47 var str = 'abc'; 48 var strA = cnEmptyString; 49 var strB = 'Z'; 50 51 52 //----------------------------------------------------------------------------- 53 test(); 54 //----------------------------------------------------------------------------- 55 56 57 /* 58 * In this test, it's important to reportCompare() each other case 59 * BEFORE the last two cases are attempted. Don't store all results 60 * in an array and reportCompare() them at the end, as we usually do. 61 * 62 * When this bug was filed, str.replace(strA, strB) would return no value 63 * whatsoever if strA == cnEmptyString, and no error, either - 64 */ 65 function test() 66 { 67 printBugNumber(BUGNUMBER); 68 printStatus (summary); 69 70 /******************* THESE WERE INCORRECT; SEE ABOVE ************************ 71 status = 'Section A of test'; 72 strA = 'a'; 73 actual = str.replace(strA, strB); 74 expect = str.replace(new RegExp(strA), strB); 75 reportCompare(expect, actual, status); 76 77 status = 'Section B of test'; 78 strA = 'x'; 79 actual = str.replace(strA, strB); 80 expect = str.replace(new RegExp(strA), strB); 81 reportCompare(expect, actual, status); 82 83 status = 'Section C of test'; 84 strA = undefined; 85 actual = str.replace(strA, strB); 86 expect = str.replace(new RegExp(strA), strB); 87 reportCompare(expect, actual, status); 88 89 status = 'Section D of test'; 90 strA = null; 91 actual = str.replace(strA, strB); 92 expect = str.replace(new RegExp(strA), strB); 93 reportCompare(expect, actual, status); 94 95 96 * This example is from jim@jibbering.com (see Bugzilla bug 92942) 97 * It is a variation on the example below. 98 * 99 * Namely, we are using the regexp /$/ instead of the regexp //. 100 * The regexp /$/ means we should match the "empty string" at the 101 * end-boundary of the word, instead of the one at the beginning. 102 * 103 status = 'Section E of test'; 104 var strJim = 'aa$aa'; 105 strA = '$'; 106 actual = strJim.replace(strA, strB); // bug -> 'aaZaa' 107 expect = strJim.replace(new RegExp(strA), strB); // expect 'aa$aaZ' 108 reportCompare(expect, actual, status); 109 110 111 * 112 * Note: 'Zabc' is the result we expect for 'abc'.replace('', 'Z'). 113 * 114 * The string '' is supposed to be equivalent to new RegExp('') = //. 115 * The regexp // means we should match the "empty string" conceived of 116 * at the beginning boundary of the word, before the first character. 117 * 118 status = 'Section F of test'; 119 strA = cnEmptyString; 120 actual = str.replace(strA, strB); 121 expect = 'Zabc'; 122 reportCompare(expect, actual, status); 123 124 status = 'Section G of test'; 125 strA = cnEmptyString; 126 actual = str.replace(strA, strB); 127 expect = str.replace(new RegExp(strA), strB); 128 reportCompare(expect, actual, status); 129 130 ************************* END OF INCORRECT CASES ****************************/ 131 132 133 ////////////////////////// OK, LET'S START OVER ////////////////////////////// 134 135 status = 'Section 1 of test'; 136 actual = 'abc'.replace('a', 'Z'); 137 expect = 'Zbc'; 138 reportCompare(expect, actual, status); 139 140 status = 'Section 2 of test'; 141 actual = 'abc'.replace('b', 'Z'); 142 expect = 'aZc'; 143 reportCompare(expect, actual, status); 144 145 status = 'Section 3 of test'; 146 actual = 'abc'.replace(undefined, 'Z'); 147 expect = 'abc'; // String(undefined) == 'undefined'; no replacement possible 148 reportCompare(expect, actual, status); 149 150 status = 'Section 4 of test'; 151 actual = 'abc'.replace(null, 'Z'); 152 expect = 'abc'; // String(null) == 'null'; no replacement possible 153 reportCompare(expect, actual, status); 154 155 status = 'Section 5 of test'; 156 actual = 'abc'.replace(true, 'Z'); 157 expect = 'abc'; // String(true) == 'true'; no replacement possible 158 reportCompare(expect, actual, status); 159 160 status = 'Section 6 of test'; 161 actual = 'abc'.replace(false, 'Z'); 162 expect = 'abc'; // String(false) == 'false'; no replacement possible 163 reportCompare(expect, actual, status); 164 165 status = 'Section 7 of test'; 166 actual = 'aa$aa'.replace('$', 'Z'); 167 expect = 'aaZaa'; // NOT 'aa$aaZ' as in ECMA Final Draft; see above 168 reportCompare(expect, actual, status); 169 170 status = 'Section 8 of test'; 171 actual = 'abc'.replace('.*', 'Z'); 172 expect = 'abc'; // not 'Z' as in EMCA Final Draft 173 reportCompare(expect, actual, status); 174 175 status = 'Section 9 of test'; 176 actual = 'abc'.replace('', 'Z'); 177 expect = 'Zabc'; // Still expect 'Zabc' for this 178 reportCompare(expect, actual, status); 179 }