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