Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / tests / js1_5 / extensions / regress-90596-002.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
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  * Date: 28 August 2001
41  *
42  * SUMMARY: A [DontEnum] prop, if overridden, should appear in uneval().
43  * See http://bugzilla.mozilla.org/show_bug.cgi?id=90596
44  *
45  * NOTE: some inefficiencies in the test are made for the sake of readability.
46  * Sorting properties alphabetically is done for definiteness in comparisons.
47  */
48 //-----------------------------------------------------------------------------
49 var UBound = 0;
50 var BUGNUMBER = 90596;
51 var summary = 'A [DontEnum] prop, if overridden, should appear in uneval()';
52 var cnCOMMA = ',';
53 var cnLBRACE = '{';
54 var cnRBRACE = '}';
55 var cnLPAREN = '(';
56 var cnRPAREN = ')';
57 var status = '';
58 var statusitems = [];
59 var actual = '';
60 var actualvalues = [];
61 var expect= '';
62 var expectedvalues = [];
63 var obj = {};
64
65
66 status = inSection(1);
67 obj = {toString:9};
68 actual = uneval(obj);
69 expect = '({toString:9})';
70 addThis();
71
72 status = inSection(2);
73 obj = {hasOwnProperty:"Hi"};
74 actual = uneval(obj);
75 expect = '({hasOwnProperty:"Hi"})';
76 addThis();
77
78 status = inSection(3);
79 obj = {toString:9, hasOwnProperty:"Hi"};
80 actual = uneval(obj);
81 expect = '({toString:9, hasOwnProperty:"Hi"})';
82 addThis();
83
84 status = inSection(4);
85 obj = {prop1:1, toString:9, hasOwnProperty:"Hi"};
86 actual = uneval(obj);
87 expect = '({prop1:1, toString:9, hasOwnProperty:"Hi"})';
88 addThis();
89
90
91 // TRY THE SAME THING IN EVAL CODE
92 var s = '';
93
94 status = inSection(5);
95 s = 'obj = {toString:9}';
96 eval(s);
97 actual = uneval(obj);
98 expect = '({toString:9})';
99 addThis();
100
101 status = inSection(6);
102 s = 'obj = {hasOwnProperty:"Hi"}';
103 eval(s);
104 actual = uneval(obj);
105 expect = '({hasOwnProperty:"Hi"})';
106 addThis();
107
108 status = inSection(7);
109 s = 'obj = {toString:9, hasOwnProperty:"Hi"}';
110 eval(s);
111 actual = uneval(obj);
112 expect = '({toString:9, hasOwnProperty:"Hi"})';
113 addThis();
114
115 status = inSection(8);
116 s = 'obj = {prop1:1, toString:9, hasOwnProperty:"Hi"}';
117 eval(s);
118 actual = uneval(obj);
119 expect = '({prop1:1, toString:9, hasOwnProperty:"Hi"})';
120 addThis();
121
122
123 // TRY THE SAME THING IN FUNCTION CODE
124 function A()
125 {
126   status = inSection(9);
127   var s = 'obj = {toString:9}';
128   eval(s);
129   actual = uneval(obj);
130   expect = '({toString:9})';
131   addThis();
132 }
133 A();
134
135 function B()
136 {
137   status = inSection(10);
138   var s = 'obj = {hasOwnProperty:"Hi"}';
139   eval(s);
140   actual = uneval(obj);
141   expect = '({hasOwnProperty:"Hi"})';
142   addThis();
143 }
144 B();
145
146 function C()
147 {
148   status = inSection(11);
149   var s = 'obj = {toString:9, hasOwnProperty:"Hi"}';
150   eval(s);
151   actual = uneval(obj);
152   expect = '({toString:9, hasOwnProperty:"Hi"})';
153   addThis();
154 }
155 C();
156
157 function D()
158 {
159   status = inSection(12);
160   var s = 'obj = {prop1:1, toString:9, hasOwnProperty:"Hi"}';
161   eval(s);
162   actual = uneval(obj);
163   expect = '({prop1:1, toString:9, hasOwnProperty:"Hi"})';
164   addThis();
165 }
166 D();
167
168
169
170 //-----------------------------------------------------------------------------
171 test();
172 //-----------------------------------------------------------------------------
173
174
175
176 /*
177  * Sort properties alphabetically -
178  */
179 function addThis()
180 {
181   statusitems[UBound] = status;
182   actualvalues[UBound] = sortThis(actual);
183   expectedvalues[UBound] = sortThis(expect);
184   UBound++;
185 }
186
187
188 /*
189  * Takes string of form '({"c", "b", "a", 2})' and returns '({"a","b","c",2})'
190  */
191 function sortThis(sList)
192 {
193   sList = compactThis(sList);
194   sList = stripParens(sList);
195   sList = stripBraces(sList);
196   var arr = sList.split(cnCOMMA);
197   arr = arr.sort();
198   var ret = String(arr);
199   ret = addBraces(ret);
200   ret = addParens(ret);
201   return ret;
202 }
203
204
205 /*
206  * Strips out any whitespace from the text -
207  */
208 function compactThis(text)
209 {
210   var charCode = 0;
211   var ret = '';
212
213   for (var i=0; i<text.length; i++)
214   {
215     charCode = text.charCodeAt(i);
216
217     if (!isWhiteSpace(charCode))
218       ret += text.charAt(i);
219   }
220
221   return ret;
222 }
223
224
225 function isWhiteSpace(charCode)
226 {
227   switch (charCode)
228   {
229   case (0x0009):
230   case (0x000B):
231   case (0x000C):
232   case (0x0020):
233   case (0x000A):  // '\n'
234   case (0x000D):  // '\r'
235     return true;
236     break;
237
238   default:
239     return false;
240   }
241 }
242
243
244 /*
245  * strips off parens at beginning and end of text -
246  */
247 function stripParens(text)
248 {
249   // remember to escape the parens...
250   var arr = text.match(/^\((.*)\)$/);
251
252   // defend against a null match...
253   if (arr != null && arr[1] != null)
254     return arr[1];
255   return text;
256 }
257
258
259 /*
260  * strips off braces at beginning and end of text -
261  */
262 function stripBraces(text)
263 {
264   // remember to escape the braces...
265   var arr = text.match(/^\{(.*)\}$/);
266
267   // defend against a null match...
268   if (arr != null && arr[1] != null)
269     return arr[1];
270   return text;
271 }
272
273
274 function addBraces(text)
275 {
276   return cnLBRACE + text + cnRBRACE;
277 }
278
279
280 function addParens(text)
281 {
282   return cnLPAREN + text + cnRPAREN;
283 }
284
285
286 function test()
287 {
288   enterFunc ('test');
289   printBugNumber(BUGNUMBER);
290   printStatus (summary);
291
292   for (var i=0; i<UBound; i++)
293   {
294     reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]);
295   }
296
297   exitFunc ('test');
298 }