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