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