Debian Codegraph @

rhino/testsrc/tests/js1_5/Object/regress-90596-003.js

raw symbols SCIP
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 * Date: 28 August 2001
8 *
9 * SUMMARY: A [DontEnum] prop, if overridden, should appear in for-in loops.
10 * See http://bugzilla.mozilla.org/show_bug.cgi?id=90596
11 *
12 * NOTE: some inefficiencies in the test are made for the sake of readability.
13 * For example, we quote string values like "Hi" in lines like this:
14 *
15 * actual = enumerateThis(obj);
16 * expect = '{prop:"Hi"}';
17 *
18 * But enumerateThis(obj) gets literal value Hi for obj.prop, not
19 * literal "Hi". We take care of all these details in the
20 * compactThis(), sortThis() functions. Sorting properties
21 * alphabetically is necessary for the test to work in Rhino.
22 */
23 //-----------------------------------------------------------------------------
24 var gTestfile = 'regress-90596-003.js';
25 var UBound = 0;
26 var BUGNUMBER = 90596;
27 var summary = '[DontEnum] props (if overridden) should appear in for-in loops';
28 var cnCOMMA = ',';
29 var cnCOLON = ':';
30 var cnLBRACE = '{';
31 var cnRBRACE = '}';
32 var status = '';
33 var statusitems = [];
34 var actual = '';
35 var actualvalues = [];
36 var expect= '';
37 var expectedvalues = [];
38 var obj = {};
39
40
41 status = inSection(1);
42 obj = {toString:9};
43 actual = enumerateThis(obj);
44 expect = '{toString:9}';
45 addThis();
46
47 status = inSection(2);
48 obj = {hasOwnProperty:"Hi"};
49 actual = enumerateThis(obj);
50 expect = '{hasOwnProperty:"Hi"}';
51 addThis();
52
53 status = inSection(3);
54 obj = {toString:9, hasOwnProperty:"Hi"};
55 actual = enumerateThis(obj);
56 expect = '{toString:9, hasOwnProperty:"Hi"}';
57 addThis();
58
59 status = inSection(4);
60 obj = {prop1:1, toString:9, hasOwnProperty:"Hi"};
61 actual = enumerateThis(obj);
62 expect = '{prop1:1, toString:9, hasOwnProperty:"Hi"}';
63 addThis();
64
65
66 // TRY THE SAME THING IN EVAL CODE
67 var s = '';
68
69 status = inSection(5);
70 s = 'obj = {toString:9}';
71 eval(s);
72 actual = enumerateThis(obj);
73 expect = '{toString:9}';
74 addThis();
75
76 status = inSection(6);
77 s = 'obj = {hasOwnProperty:"Hi"}';
78 eval(s);
79 actual = enumerateThis(obj);
80 expect = '{hasOwnProperty:"Hi"}';
81 addThis();
82
83 status = inSection(7);
84 s = 'obj = {toString:9, hasOwnProperty:"Hi"}';
85 eval(s);
86 actual = enumerateThis(obj);
87 expect = '{toString:9, hasOwnProperty:"Hi"}';
88 addThis();
89
90 status = inSection(8);
91 s = 'obj = {prop1:1, toString:9, hasOwnProperty:"Hi"}';
92 eval(s);
93 actual = enumerateThis(obj);
94 expect = '{prop1:1, toString:9, hasOwnProperty:"Hi"}';
95 addThis();
96
97
98 // TRY THE SAME THING IN FUNCTION CODE
99 function A()
100 {
101 status = inSection(9);
102 var s = 'obj = {toString:9}';
103 eval(s);
104 actual = enumerateThis(obj);
105 expect = '{toString:9}';
106 addThis();
107 }
108 A();
109
110 function B()
111 {
112 status = inSection(10);
113 var s = 'obj = {hasOwnProperty:"Hi"}';
114 eval(s);
115 actual = enumerateThis(obj);
116 expect = '{hasOwnProperty:"Hi"}';
117 addThis();
118 }
119 B();
120
121 function C()
122 {
123 status = inSection(11);
124 var s = 'obj = {toString:9, hasOwnProperty:"Hi"}';
125 eval(s);
126 actual = enumerateThis(obj);
127 expect = '{toString:9, hasOwnProperty:"Hi"}';
128 addThis();
129 }
130 C();
131
132 function D()
133 {
134 status = inSection(12);
135 var s = 'obj = {prop1:1, toString:9, hasOwnProperty:"Hi"}';
136 eval(s);
137 actual = enumerateThis(obj);
138 expect = '{prop1:1, toString:9, hasOwnProperty:"Hi"}';
139 addThis();
140 }
141 D();
142
143
144
145 //-----------------------------------------------------------------------------
146 test();
147 //-----------------------------------------------------------------------------
148
149
150
151 function enumerateThis(obj)
152 {
153 var arr = new Array();
154
155 for (var prop in obj)
156 {
157 arr.push(prop + cnCOLON + obj[prop]);
158 }
159
160 var ret = addBraces(String(arr));
161 return ret;
162 }
163
164
165 function addBraces(text)
166 {
167 return cnLBRACE + text + cnRBRACE;
168 }
169
170
171 /*
172 * Sort properties alphabetically so the test will work in Rhino
173 */
174 function addThis()
175 {
176 statusitems[UBound] = status;
177 actualvalues[UBound] = sortThis(actual);
178 expectedvalues[UBound] = sortThis(expect);
179 UBound++;
180 }
181
182
183 /*
184 * Takes a string of the form '{"c", "b", "a", 2}' and returns '{2,a,b,c}'
185 */
186 function sortThis(sList)
187 {
188 sList = compactThis(sList);
189 sList = stripBraces(sList);
190 var arr = sList.split(cnCOMMA);
191 arr = arr.sort();
192 var ret = String(arr);
193 ret = addBraces(ret);
194 return ret;
195 }
196
197
198 /*
199 * Strips out any whitespace or quotes from the text -
200 */
201 function compactThis(text)
202 {
203 var charCode = 0;
204 var ret = '';
205
206 for (var i=0; i<text.length; i++)
207 {
208 charCode = text.charCodeAt(i);
209
210 if (!isWhiteSpace(charCode) && !isQuote(charCode))
211 ret += text.charAt(i);
212 }
213
214 return ret;
215 }
216
217
218 function isWhiteSpace(charCode)
219 {
220 switch (charCode)
221 {
222 case (0x0009):
223 case (0x000B):
224 case (0x000C):
225 case (0x0020):
226 case (0x000A): // '\n'
227 case (0x000D): // '\r'
228 return true;
229 break;
230
231 default:
232 return false;
233 }
234 }
235
236
237 function isQuote(charCode)
238 {
239 switch (charCode)
240 {
241 case (0x0027): // single quote
242 case (0x0022): // double quote
243 return true;
244 break;
245
246 default:
247 return false;
248 }
249 }
250
251
252 /*
253 * strips off braces at beginning and end of text -
254 */
255 function stripBraces(text)
256 {
257 // remember to escape the braces...
258 var arr = text.match(/^\{(.*)\}$/);
259
260 // defend against a null match...
261 if (arr != null && arr[1] != null)
262 return arr[1];
263 return text;
264 }
265
266
267 function test()
268 {
269 enterFunc ('test');
270 printBugNumber(BUGNUMBER);
271 printStatus (summary);
272
273 for (var i=0; i<UBound; i++)
274 {
275 reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]);
276 }
277
278 exitFunc ('test');
279 }