Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / tests / ecma_5 / Object / 15.2.3.3-01.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 SpiderMonkey ES5 tests.
16  *
17  * The Initial Developer of the Original Code is
18  * Mozilla Corporation.
19  * Portions created by the Initial Developer are Copyright (C) 2009
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s):
23  *   Jeff Walden <jwalden+code@mit.edu> (original author)
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 var BUGNUMBER = 505587;
41 var summary = 'ES5 Object.getOwnPropertyDescriptor(O)';
42 var actual = '';
43 var expect = '';
44
45 printBugNumber(BUGNUMBER);
46 printStatus (summary);
47
48 /**************
49  * BEGIN TEST *
50  **************/
51
52 function assertEq(a, e, msg)
53 {
54   function SameValue(v1, v2)
55   {
56     if (v1 === 0 && v2 === 0)
57       return 1 / v1 === 1 / v2;
58     if (v1 !== v1 && v2 !== v2)
59       return true;
60     return v1 === v2;
61   }
62
63   if (!SameValue(a, e))
64   {
65     var stack = new Error().stack || "";
66     throw "Assertion failed: got " + a + ", expected " + e +
67           (msg ? ": " + msg : "") +
68           (stack ? "\nStack:\n" + stack : "");
69   }
70 }
71
72 function expectDescriptor(actual, expected)
73 {
74   if (actual === undefined && expected === undefined)
75     return;
76
77   assertEq(typeof actual, "object");
78   assertEq(typeof expected, "object");
79
80   var fields =
81     {
82       value: true,
83       get: true,
84       set: true,
85       enumerable: true,
86       writable: true,
87       configurable: true
88     };
89   for (var p in fields)
90     assertEq(actual.hasOwnProperty(p), expected.hasOwnProperty(p), p);
91   for (var p in actual)
92     assertEq(p in fields, true, p);
93   for (var p in expected)
94     assertEq(p in fields, true, p);
95
96   assertEq(actual.hasOwnProperty("value"), actual.hasOwnProperty("writable"));
97   assertEq(actual.hasOwnProperty("get"), actual.hasOwnProperty("set"));
98   if (actual.hasOwnProperty("value"))
99   {
100     assertEq(actual.value, expected.value);
101     assertEq(actual.writable, expected.writable);
102   }
103   else
104   {
105     assertEq(actual.get, expected.get);
106     assertEq(actual.set, expected.set);
107   }
108
109   assertEq(actual.hasOwnProperty("enumerable"), true);
110   assertEq(actual.hasOwnProperty("configurable"), true);
111   assertEq(actual.enumerable, expected.enumerable);
112   assertEq(actual.configurable, expected.configurable);
113 }
114
115 function adjustDescriptorField(o, actual, expect, field)
116 {
117   assertEq(field === "get" || field === "set", true);
118   var lookup = "__lookup" + (field === "get" ? "G" : "S") + "etter";
119   if (typeof o[lookup] === "function")
120     expect[field] = o[lookup](field);
121   else
122     actual[field] = expect[field] = undefined; /* censor if we can't lookup */
123 }
124
125 /******************************************************************************/
126
127 var o, pd, expected;
128
129 o = { get x() { return 12; } };
130
131 pd = Object.getOwnPropertyDescriptor(o, "x");
132 expected =
133   {
134     set: undefined,
135     enumerable: true,
136     configurable: true
137   };
138 adjustDescriptorField(o, pd, expected, "get");
139
140 expectDescriptor(pd, expected);
141
142 /******************************************************************************/
143
144 o = {};
145 o.b = 12;
146
147 pd = Object.getOwnPropertyDescriptor(o, "b");
148 expected =
149   {
150     value: 12,
151     writable: true,
152     enumerable: true,
153     configurable: true
154   };
155 expectDescriptor(pd, expected);
156
157 /******************************************************************************/
158
159 o = { get y() { return 17; }, set y(z) { } };
160
161 pd = Object.getOwnPropertyDescriptor(o, "y");
162 expected =
163   {
164     enumerable: true,
165     configurable: true
166   };
167 adjustDescriptorField(o, pd, expected, "get");
168 adjustDescriptorField(o, pd, expected, "set");
169
170 expectDescriptor(pd, expected);
171
172 /******************************************************************************/
173
174 o = {};
175
176 pd = Object.getOwnPropertyDescriptor(o, "absent");
177
178 expectDescriptor(pd, undefined);
179
180 /******************************************************************************/
181
182 pd = Object.getOwnPropertyDescriptor([], "length");
183 expected =
184   {
185     value: 0,
186     writable: true,
187     enumerable: false,
188     configurable: false
189   };
190
191 expectDescriptor(pd, expected);
192
193 pd = Object.getOwnPropertyDescriptor([1], "length");
194 expected =
195   {
196     value: 1,
197     writable: true,
198     enumerable: false,
199     configurable: false
200   };
201
202 expectDescriptor(pd, expected);
203
204 pd = Object.getOwnPropertyDescriptor([1,], "length");
205 expected =
206   {
207     value: 1,
208     writable: true,
209     enumerable: false,
210     configurable: false
211   };
212
213 expectDescriptor(pd, expected);
214
215 pd = Object.getOwnPropertyDescriptor([1,,], "length");
216 expected =
217   {
218     value: 2,
219     writable: true,
220     enumerable: false,
221     configurable: false
222   };
223
224 expectDescriptor(pd, expected);
225
226 /******************************************************************************/
227
228 pd = Object.getOwnPropertyDescriptor(new String("foobar"), "length");
229 expected =
230   {
231     value: 6,
232     writable: false,
233     enumerable: false,
234     configurable: false
235   };
236
237 expectDescriptor(pd, expected);
238
239 /******************************************************************************/
240
241 function foo() { }
242 o = foo;
243
244 pd = Object.getOwnPropertyDescriptor(o, "length");
245 expected =
246   {
247     value: 0,
248     writable: false,
249     enumerable: false,
250     configurable: false
251   };
252
253 expectDescriptor(pd, expected);
254
255 pd = Object.getOwnPropertyDescriptor(o, "prototype");
256 expected =
257   {
258     value: foo.prototype,
259     writable: true,
260     enumerable: false,
261     configurable: false
262   };
263
264 expectDescriptor(pd, expected);
265
266 /******************************************************************************/
267
268 pd = Object.getOwnPropertyDescriptor(Function, "length");
269 expected =
270   {
271     value: 1,
272     writable: false,
273     enumerable: false,
274     configurable: false
275   };
276
277 expectDescriptor(pd, expected);
278
279 /******************************************************************************/
280
281 o = /foo/im;
282
283 pd = Object.getOwnPropertyDescriptor(o, "source");
284 expected =
285   {
286     value: "foo",
287     writable: false,
288     enumerable: false,
289     configurable: false
290   };
291
292 expectDescriptor(pd, expected);
293
294 pd = Object.getOwnPropertyDescriptor(o, "global");
295 expected =
296   {
297     value: false,
298     writable: false,
299     enumerable: false,
300     configurable: false
301   };
302
303 expectDescriptor(pd, expected);
304
305 pd = Object.getOwnPropertyDescriptor(o, "ignoreCase");
306 expected =
307   {
308     value: true,
309     writable: false,
310     enumerable: false,
311     configurable: false
312   };
313
314 expectDescriptor(pd, expected);
315
316 pd = Object.getOwnPropertyDescriptor(o, "multiline");
317 expected =
318   {
319     value: true,
320     writable: false,
321     enumerable: false,
322     configurable: false
323   };
324
325 expectDescriptor(pd, expected);
326
327 pd = Object.getOwnPropertyDescriptor(o, "lastIndex");
328 expected =
329   {
330     value: 0,
331     writable: true,
332     enumerable: false,
333     configurable: false
334   };
335
336 expectDescriptor(pd, expected);
337
338 /******************************************************************************/
339
340 reportCompare(expect, actual, "Object.getOwnPropertyDescriptor");
341
342 printStatus("All tests passed");