1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is mozilla.org code.
16 * The Initial Developer of the Original Code is
17 * Netscape Communications Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 1998
19 * the Initial Developer. All Rights Reserved.
22 * pschwartau@netscape.com, jim@jibbering.com
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
39 * Creation Date: 30 May 2001
40 * Correction Date: 14 Aug 2001
42 * SUMMARY: Regression test for bugs 83293, 103351
43 * See http://bugzilla.mozilla.org/show_bug.cgi?id=83293
44 * http://bugzilla.mozilla.org/show_bug.cgi?id=103351
45 * http://bugzilla.mozilla.org/show_bug.cgi?id=92942
48 * ******************** CORRECTION !!! *****************************
50 * When I originally wrote this test, I thought this was true:
51 * str.replace(strA, strB) == str.replace(new RegExp(strA),strB).
52 * See ECMA-262 Final Draft, 15.5.4.11 String.prototype.replace
54 * However, in http://bugzilla.mozilla.org/show_bug.cgi?id=83293
55 * Jim Ley points out the ECMA-262 Final Edition changed on this.
56 * String.prototype.replace (searchValue, replaceValue), if provided
57 * a searchValue that is not a RegExp, is NO LONGER to replace it with
59 * new RegExp(searchValue)
63 * This puts the replace() method at variance with search() and match(),
64 * which continue to follow the RegExp conversion of the Final Draft.
65 * It also makes most of this testcase, as originally written, invalid.
66 **********************************************************************
69 //-----------------------------------------------------------------------------
70 var bug = 103351; // <--- (Outgrowth of original bug 83293)
71 var summ_OLD = 'Testing str.replace(strA, strB) == str.replace(new RegExp(strA),strB)';
72 var summ_NEW = 'Testing String.prototype.replace(x,y) when x is a string';
73 var summary = summ_NEW;
77 var cnEmptyString = '';
79 var strA = cnEmptyString;
83 //-----------------------------------------------------------------------------
85 //-----------------------------------------------------------------------------
89 * In this test, it's important to reportCompare() each other case
90 * BEFORE the last two cases are attempted. Don't store all results
91 * in an array and reportCompare() them at the end, as we usually do.
93 * When this bug was filed, str.replace(strA, strB) would return no value
94 * whatsoever if strA == cnEmptyString, and no error, either -
100 printStatus (summary);
102 /******************* THESE WERE INCORRECT; SEE ABOVE ************************
103 status = 'Section A of test';
105 actual = str.replace(strA, strB);
106 expect = str.replace(new RegExp(strA), strB);
107 reportCompare(expect, actual, status);
109 status = 'Section B of test';
111 actual = str.replace(strA, strB);
112 expect = str.replace(new RegExp(strA), strB);
113 reportCompare(expect, actual, status);
115 status = 'Section C of test';
117 actual = str.replace(strA, strB);
118 expect = str.replace(new RegExp(strA), strB);
119 reportCompare(expect, actual, status);
121 status = 'Section D of test';
123 actual = str.replace(strA, strB);
124 expect = str.replace(new RegExp(strA), strB);
125 reportCompare(expect, actual, status);
128 * This example is from jim@jibbering.com (see Bugzilla bug 92942)
129 * It is a variation on the example below.
131 * Namely, we are using the regexp /$/ instead of the regexp //.
132 * The regexp /$/ means we should match the "empty string" at the
133 * end-boundary of the word, instead of the one at the beginning.
135 status = 'Section E of test';
136 var strJim = 'aa$aa';
138 actual = strJim.replace(strA, strB); // bug -> 'aaZaa'
139 expect = strJim.replace(new RegExp(strA), strB); // expect 'aa$aaZ'
140 reportCompare(expect, actual, status);
144 * Note: 'Zabc' is the result we expect for 'abc'.replace('', 'Z').
146 * The string '' is supposed to be equivalent to new RegExp('') = //.
147 * The regexp // means we should match the "empty string" conceived of
148 * at the beginning boundary of the word, before the first character.
150 status = 'Section F of test';
151 strA = cnEmptyString;
152 actual = str.replace(strA, strB);
154 reportCompare(expect, actual, status);
156 status = 'Section G of test';
157 strA = cnEmptyString;
158 actual = str.replace(strA, strB);
159 expect = str.replace(new RegExp(strA), strB);
160 reportCompare(expect, actual, status);
162 ************************* END OF INCORRECT CASES ****************************/
165 ////////////////////////// OK, LET'S START OVER //////////////////////////////
167 status = 'Section 1 of test';
168 actual = 'abc'.replace('a', 'Z');
170 reportCompare(expect, actual, status);
172 status = 'Section 2 of test';
173 actual = 'abc'.replace('b', 'Z');
175 reportCompare(expect, actual, status);
177 status = 'Section 3 of test';
178 actual = 'abc'.replace(undefined, 'Z');
179 expect = 'abc'; // String(undefined) == 'undefined'; no replacement possible
180 reportCompare(expect, actual, status);
182 status = 'Section 4 of test';
183 actual = 'abc'.replace(null, 'Z');
184 expect = 'abc'; // String(null) == 'null'; no replacement possible
185 reportCompare(expect, actual, status);
187 status = 'Section 5 of test';
188 actual = 'abc'.replace(true, 'Z');
189 expect = 'abc'; // String(true) == 'true'; no replacement possible
190 reportCompare(expect, actual, status);
192 status = 'Section 6 of test';
193 actual = 'abc'.replace(false, 'Z');
194 expect = 'abc'; // String(false) == 'false'; no replacement possible
195 reportCompare(expect, actual, status);
197 status = 'Section 7 of test';
198 actual = 'aa$aa'.replace('$', 'Z');
199 expect = 'aaZaa'; // NOT 'aa$aaZ' as in ECMA Final Draft; see above
200 reportCompare(expect, actual, status);
202 status = 'Section 8 of test';
203 actual = 'abc'.replace('.*', 'Z');
204 expect = 'abc'; // not 'Z' as in EMCA Final Draft
205 reportCompare(expect, actual, status);
207 status = 'Section 9 of test';
208 actual = 'abc'.replace('', 'Z');
209 expect = 'Zabc'; // Still expect 'Zabc' for this
210 reportCompare(expect, actual, status);