Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / tests / ecma_3 / String / regress-83293.js
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
4  *
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/
9  *
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
13  * License.
14  *
15  * The Original Code is mozilla.org code.
16  *
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.
21  *
22  * Contributor(s):
23  *   pschwartau@netscape.com, jim@jibbering.com
24  *
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.
36  *
37  * ***** END LICENSE BLOCK ***** */
38
39
40 /*
41  * Creation Date:   30 May 2001
42  * Correction Date: 14 Aug 2001
43  *
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
48  *
49  *
50  * ********************   CORRECTION !!!  *****************************
51  *
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
55  *
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
60  *
61  *                  new RegExp(searchValue)
62  * but rather:
63  *                  String(searchValue)
64  *
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  **********************************************************************
69  */
70
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;
76 var status = '';
77 var actual = '';
78 var expect= '';
79 var cnEmptyString = '';
80 var str = 'abc';
81 var strA = cnEmptyString;
82 var strB = 'Z';
83
84
85 //-----------------------------------------------------------------------------
86 test();
87 //-----------------------------------------------------------------------------
88
89
90 /*
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.
94  *
95  * When this bug was filed, str.replace(strA, strB) would return no value
96  * whatsoever if strA == cnEmptyString, and no error, either -
97  */
98 function test()
99 {
100   enterFunc ('test');
101   printBugNumber(BUGNUMBER);
102   printStatus (summary);
103
104 /*******************  THESE WERE INCORRECT; SEE ABOVE  ************************
105   status = 'Section A of test';
106   strA = 'a';
107   actual = str.replace(strA, strB);
108   expect = str.replace(new RegExp(strA), strB);
109   reportCompare(expect, actual, status);
110
111   status = 'Section B of test';
112   strA = 'x';
113   actual = str.replace(strA, strB);
114   expect = str.replace(new RegExp(strA), strB);
115   reportCompare(expect, actual, status);
116
117   status = 'Section C of test';
118   strA = undefined;
119   actual = str.replace(strA, strB);
120   expect = str.replace(new RegExp(strA), strB);
121   reportCompare(expect, actual, status);
122
123   status = 'Section D of test';
124   strA = null;
125   actual = str.replace(strA, strB);
126   expect = str.replace(new RegExp(strA), strB);
127   reportCompare(expect, actual, status);
128
129
130   * This example is from jim@jibbering.com (see Bugzilla bug 92942)
131   * It is a variation on the example below.
132   *
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.
136   *
137   status = 'Section E of test';
138   var strJim = 'aa$aa';
139   strA = '$';
140   actual = strJim.replace(strA, strB);             // bug -> 'aaZaa'
141   expect = strJim.replace(new RegExp(strA), strB); // expect 'aa$aaZ'
142   reportCompare(expect, actual, status);
143
144
145   *
146   * Note: 'Zabc' is the result we expect for 'abc'.replace('', 'Z').
147   *
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.
151   *
152   status = 'Section F of test';
153   strA = cnEmptyString;
154   actual = str.replace(strA, strB);
155   expect = 'Zabc';
156   reportCompare(expect, actual, status);
157
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);
163
164   *************************  END OF INCORRECT CASES ****************************/
165
166
167 //////////////////////////  OK, LET'S START OVER //////////////////////////////
168
169   status = 'Section 1 of test';
170   actual = 'abc'.replace('a', 'Z');
171   expect = 'Zbc';
172   reportCompare(expect, actual, status);
173
174   status = 'Section 2 of test';
175   actual = 'abc'.replace('b', 'Z');
176   expect = 'aZc';
177   reportCompare(expect, actual, status);
178
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);
183
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);
188
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);
193
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);
198
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);
203
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);
208
209   status = 'Section 9 of test';
210   actual = 'abc'.replace('', 'Z');
211   expect = 'Zabc';  // Still expect 'Zabc' for this
212   reportCompare(expect, actual, status);
213
214   exitFunc ('test');
215 }