1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
23 * pschwartau@netscape.com, jim@jibbering.com
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
41 * Creation Date: 30 May 2001
42 * Correction Date: 14 Aug 2001
44 * SUMMARY: Regression test for bugs 83293, 103351
45 * See http://bugzilla.mozilla.org/show_bug.cgi?id=83293
46 * http://bugzilla.mozilla.org/show_bug.cgi?id=103351
47 * http://bugzilla.mozilla.org/show_bug.cgi?id=92942
50 * ******************** CORRECTION !!! *****************************
52 * When I originally wrote this test, I thought this was true:
53 * str.replace(strA, strB) == str.replace(new RegExp(strA),strB).
54 * See ECMA-262 Final Draft, 15.5.4.11 String.prototype.replace
56 * However, in http://bugzilla.mozilla.org/show_bug.cgi?id=83293
57 * Jim Ley points out the ECMA-262 Final Edition changed on this.
58 * String.prototype.replace (searchValue, replaceValue), if provided
59 * a searchValue that is not a RegExp, is NO LONGER to replace it with
61 * new RegExp(searchValue)
65 * This puts the replace() method at variance with search() and match(),
66 * which continue to follow the RegExp conversion of the Final Draft.
67 * It also makes most of this testcase, as originally written, invalid.
68 **********************************************************************
71 //-----------------------------------------------------------------------------
72 var BUGNUMBER = 103351; // <--- (Outgrowth of original bug 83293)
73 var summ_OLD = 'Testing str.replace(strA, strB) == str.replace(new RegExp(strA),strB)';
74 var summ_NEW = 'Testing String.prototype.replace(x,y) when x is a string';
75 var summary = summ_NEW;
79 var cnEmptyString = '';
81 var strA = cnEmptyString;
85 //-----------------------------------------------------------------------------
87 //-----------------------------------------------------------------------------
91 * In this test, it's important to reportCompare() each other case
92 * BEFORE the last two cases are attempted. Don't store all results
93 * in an array and reportCompare() them at the end, as we usually do.
95 * When this bug was filed, str.replace(strA, strB) would return no value
96 * whatsoever if strA == cnEmptyString, and no error, either -
101 printBugNumber(BUGNUMBER);
102 printStatus (summary);
104 /******************* THESE WERE INCORRECT; SEE ABOVE ************************
105 status = 'Section A of test';
107 actual = str.replace(strA, strB);
108 expect = str.replace(new RegExp(strA), strB);
109 reportCompare(expect, actual, status);
111 status = 'Section B of test';
113 actual = str.replace(strA, strB);
114 expect = str.replace(new RegExp(strA), strB);
115 reportCompare(expect, actual, status);
117 status = 'Section C of test';
119 actual = str.replace(strA, strB);
120 expect = str.replace(new RegExp(strA), strB);
121 reportCompare(expect, actual, status);
123 status = 'Section D of test';
125 actual = str.replace(strA, strB);
126 expect = str.replace(new RegExp(strA), strB);
127 reportCompare(expect, actual, status);
130 * This example is from jim@jibbering.com (see Bugzilla bug 92942)
131 * It is a variation on the example below.
133 * Namely, we are using the regexp /$/ instead of the regexp //.
134 * The regexp /$/ means we should match the "empty string" at the
135 * end-boundary of the word, instead of the one at the beginning.
137 status = 'Section E of test';
138 var strJim = 'aa$aa';
140 actual = strJim.replace(strA, strB); // bug -> 'aaZaa'
141 expect = strJim.replace(new RegExp(strA), strB); // expect 'aa$aaZ'
142 reportCompare(expect, actual, status);
146 * Note: 'Zabc' is the result we expect for 'abc'.replace('', 'Z').
148 * The string '' is supposed to be equivalent to new RegExp('') = //.
149 * The regexp // means we should match the "empty string" conceived of
150 * at the beginning boundary of the word, before the first character.
152 status = 'Section F of test';
153 strA = cnEmptyString;
154 actual = str.replace(strA, strB);
156 reportCompare(expect, actual, status);
158 status = 'Section G of test';
159 strA = cnEmptyString;
160 actual = str.replace(strA, strB);
161 expect = str.replace(new RegExp(strA), strB);
162 reportCompare(expect, actual, status);
164 ************************* END OF INCORRECT CASES ****************************/
167 ////////////////////////// OK, LET'S START OVER //////////////////////////////
169 status = 'Section 1 of test';
170 actual = 'abc'.replace('a', 'Z');
172 reportCompare(expect, actual, status);
174 status = 'Section 2 of test';
175 actual = 'abc'.replace('b', 'Z');
177 reportCompare(expect, actual, status);
179 status = 'Section 3 of test';
180 actual = 'abc'.replace(undefined, 'Z');
181 expect = 'abc'; // String(undefined) == 'undefined'; no replacement possible
182 reportCompare(expect, actual, status);
184 status = 'Section 4 of test';
185 actual = 'abc'.replace(null, 'Z');
186 expect = 'abc'; // String(null) == 'null'; no replacement possible
187 reportCompare(expect, actual, status);
189 status = 'Section 5 of test';
190 actual = 'abc'.replace(true, 'Z');
191 expect = 'abc'; // String(true) == 'true'; no replacement possible
192 reportCompare(expect, actual, status);
194 status = 'Section 6 of test';
195 actual = 'abc'.replace(false, 'Z');
196 expect = 'abc'; // String(false) == 'false'; no replacement possible
197 reportCompare(expect, actual, status);
199 status = 'Section 7 of test';
200 actual = 'aa$aa'.replace('$', 'Z');
201 expect = 'aaZaa'; // NOT 'aa$aaZ' as in ECMA Final Draft; see above
202 reportCompare(expect, actual, status);
204 status = 'Section 8 of test';
205 actual = 'abc'.replace('.*', 'Z');
206 expect = 'abc'; // not 'Z' as in EMCA Final Draft
207 reportCompare(expect, actual, status);
209 status = 'Section 9 of test';
210 actual = 'abc'.replace('', 'Z');
211 expect = 'Zabc'; // Still expect 'Zabc' for this
212 reportCompare(expect, actual, status);