* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / Microsoft.JScript / Test / Mozilla / ecma_3 / String / regress-83293.js
1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
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/
8  *
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
12  * License.
13  *
14  * The Original Code is mozilla.org code.
15  *
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.
20  *
21  * Contributor(s):
22  *   pschwartau@netscape.com, jim@jibbering.com
23  *
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.
35  *
36  * ***** END LICENSE BLOCK ***** */
37
38 /* 
39  * Creation Date:   30 May 2001
40  * Correction Date: 14 Aug 2001
41  *
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
46  *
47  *
48  * ********************   CORRECTION !!!  *****************************
49  *
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
53  *
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
58  *
59  *                  new RegExp(searchValue)
60  * but rather:
61  *                  String(searchValue)
62  *
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  **********************************************************************
67  */
68  
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;
74 var status = '';
75 var actual = '';
76 var expect= '';
77 var cnEmptyString = '';
78 var str = 'abc';
79 var strA = cnEmptyString;
80 var strB = 'Z';
81
82
83 //-----------------------------------------------------------------------------
84 test();
85 //-----------------------------------------------------------------------------
86
87
88 /*
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.
92  *
93  * When this bug was filed, str.replace(strA, strB) would return no value
94  * whatsoever if strA == cnEmptyString, and no error, either -
95  */
96 function test()
97 {
98   enterFunc ('test');
99   printBugNumber (bug);
100   printStatus (summary);
101
102 /*******************  THESE WERE INCORRECT; SEE ABOVE  ************************
103   status = 'Section A of test';
104   strA = 'a';
105   actual = str.replace(strA, strB);
106   expect = str.replace(new RegExp(strA), strB);
107   reportCompare(expect, actual, status);
108
109   status = 'Section B of test';
110   strA = 'x';
111   actual = str.replace(strA, strB);
112   expect = str.replace(new RegExp(strA), strB);
113   reportCompare(expect, actual, status);
114
115   status = 'Section C of test';
116   strA = undefined;
117   actual = str.replace(strA, strB);
118   expect = str.replace(new RegExp(strA), strB);
119   reportCompare(expect, actual, status);
120
121   status = 'Section D of test';
122   strA = null;
123   actual = str.replace(strA, strB);
124   expect = str.replace(new RegExp(strA), strB);
125   reportCompare(expect, actual, status);
126
127
128   * This example is from jim@jibbering.com (see Bugzilla bug 92942)
129   * It is a variation on the example below.
130   *
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.
134   *
135   status = 'Section E of test';
136   var strJim = 'aa$aa';
137   strA = '$';
138   actual = strJim.replace(strA, strB);             // bug -> 'aaZaa'
139   expect = strJim.replace(new RegExp(strA), strB); // expect 'aa$aaZ'
140   reportCompare(expect, actual, status);
141
142
143   *
144   * Note: 'Zabc' is the result we expect for 'abc'.replace('', 'Z').
145   *
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.
149   *
150   status = 'Section F of test';
151   strA = cnEmptyString;
152   actual = str.replace(strA, strB);
153   expect = 'Zabc';
154   reportCompare(expect, actual, status);
155
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);
161
162  *************************  END OF INCORRECT CASES ****************************/
163
164
165 //////////////////////////  OK, LET'S START OVER //////////////////////////////
166
167   status = 'Section 1 of test';
168   actual = 'abc'.replace('a', 'Z');
169   expect = 'Zbc';
170   reportCompare(expect, actual, status);
171
172   status = 'Section 2 of test';
173   actual = 'abc'.replace('b', 'Z');
174   expect = 'aZc';
175   reportCompare(expect, actual, status);
176
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);
181
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);
186
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);
191
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);
196
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);
201
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);
206
207   status = 'Section 9 of test';
208   actual = 'abc'.replace('', 'Z');
209   expect = 'Zabc';  // Still expect 'Zabc' for this
210   reportCompare(expect, actual, status);
211
212   exitFunc ('test');
213 }