]>
git.saurik.com Git - apple/javascriptcore.git/blob - tests/mozilla/ecma_3/String/regress-83293.js
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/
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.
12 * The Original Code is mozilla.org code.
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
19 * Contributor(s): pschwartau@netscape.com, jim@jibbering.com
20 * Creation Date: 30 May 2001
21 * Correction Date: 14 Aug 2001
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
29 * ******************** CORRECTION !!! *****************************
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
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
40 * new RegExp(searchValue)
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 **********************************************************************
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
;
57 var cnEmptyString
= '';
59 var strA
= cnEmptyString
;
63 //-----------------------------------------------------------------------------
65 //-----------------------------------------------------------------------------
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.
73 * When this bug was filed, str.replace(strA, strB) would return no value
74 * whatsoever if strA == cnEmptyString, and no error, either -
80 printStatus (summary
);
82 /******************* THESE WERE INCORRECT; SEE ABOVE ************************
83 status = 'Section A of test';
85 actual = str.replace(strA, strB);
86 expect = str.replace(new RegExp(strA), strB);
87 reportCompare(expect, actual, status);
89 status = 'Section B of test';
91 actual = str.replace(strA, strB);
92 expect = str.replace(new RegExp(strA), strB);
93 reportCompare(expect, actual, status);
95 status = 'Section C of test';
97 actual = str.replace(strA, strB);
98 expect = str.replace(new RegExp(strA), strB);
99 reportCompare(expect, actual, status);
101 status = 'Section D of test';
103 actual = str.replace(strA, strB);
104 expect = str.replace(new RegExp(strA), strB);
105 reportCompare(expect, actual, status);
108 * This example is from jim@jibbering.com (see Bugzilla bug 92942)
109 * It is a variation on the example below.
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.
115 status = 'Section E of test';
116 var strJim = 'aa$aa';
118 actual = strJim.replace(strA, strB); // bug -> 'aaZaa'
119 expect = strJim.replace(new RegExp(strA), strB); // expect 'aa$aaZ'
120 reportCompare(expect, actual, status);
124 * Note: 'Zabc' is the result we expect for 'abc'.replace('', 'Z').
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.
130 status = 'Section F of test';
131 strA = cnEmptyString;
132 actual = str.replace(strA, strB);
134 reportCompare(expect, actual, status);
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);
142 ************************* END OF INCORRECT CASES ****************************/
145 ////////////////////////// OK, LET'S START OVER //////////////////////////////
147 status
= 'Section 1 of test';
148 actual
= 'abc'.replace('a', 'Z');
150 reportCompare(expect
, actual
, status
);
152 status
= 'Section 2 of test';
153 actual
= 'abc'.replace('b', 'Z');
155 reportCompare(expect
, actual
, status
);
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
);
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
);
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
);
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
);
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
);
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
);
187 status
= 'Section 9 of test';
188 actual
= 'abc'.replace('', 'Z');
189 expect
= 'Zabc'; // Still expect 'Zabc' for this
190 reportCompare(expect
, actual
, status
);