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