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