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
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/
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
15 * The Original Code is SpiderMonkey ES5 tests.
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.
23 * Jeff Walden <jwalden+code@mit.edu> (original author)
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.
37 * ***** END LICENSE BLOCK ***** */
39 //-----------------------------------------------------------------------------
40 var BUGNUMBER = 505587;
41 var summary = 'ES5 Object.getOwnPropertyDescriptor(O)';
45 printBugNumber(BUGNUMBER);
46 printStatus (summary);
52 function assertEq(a, e, msg)
54 function SameValue(v1, v2)
56 if (v1 === 0 && v2 === 0)
57 return 1 / v1 === 1 / v2;
58 if (v1 !== v1 && v2 !== v2)
65 var stack = new Error().stack || "";
66 throw "Assertion failed: got " + a + ", expected " + e +
67 (msg ? ": " + msg : "") +
68 (stack ? "\nStack:\n" + stack : "");
72 function expectDescriptor(actual, expected)
74 if (actual === undefined && expected === undefined)
77 assertEq(typeof actual, "object");
78 assertEq(typeof expected, "object");
90 assertEq(actual.hasOwnProperty(p), expected.hasOwnProperty(p), p);
92 assertEq(p in fields, true, p);
93 for (var p in expected)
94 assertEq(p in fields, true, p);
96 assertEq(actual.hasOwnProperty("value"), actual.hasOwnProperty("writable"));
97 assertEq(actual.hasOwnProperty("get"), actual.hasOwnProperty("set"));
98 if (actual.hasOwnProperty("value"))
100 assertEq(actual.value, expected.value);
101 assertEq(actual.writable, expected.writable);
105 assertEq(actual.get, expected.get);
106 assertEq(actual.set, expected.set);
109 assertEq(actual.hasOwnProperty("enumerable"), true);
110 assertEq(actual.hasOwnProperty("configurable"), true);
111 assertEq(actual.enumerable, expected.enumerable);
112 assertEq(actual.configurable, expected.configurable);
115 function adjustDescriptorField(o, actual, expect, field)
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);
122 actual[field] = expect[field] = undefined; /* censor if we can't lookup */
125 /******************************************************************************/
129 o = { get x() { return 12; } };
131 pd = Object.getOwnPropertyDescriptor(o, "x");
138 adjustDescriptorField(o, pd, expected, "get");
140 expectDescriptor(pd, expected);
142 /******************************************************************************/
147 pd = Object.getOwnPropertyDescriptor(o, "b");
155 expectDescriptor(pd, expected);
157 /******************************************************************************/
159 o = { get y() { return 17; }, set y(z) { } };
161 pd = Object.getOwnPropertyDescriptor(o, "y");
167 adjustDescriptorField(o, pd, expected, "get");
168 adjustDescriptorField(o, pd, expected, "set");
170 expectDescriptor(pd, expected);
172 /******************************************************************************/
176 pd = Object.getOwnPropertyDescriptor(o, "absent");
178 expectDescriptor(pd, undefined);
180 /******************************************************************************/
182 pd = Object.getOwnPropertyDescriptor([], "length");
191 expectDescriptor(pd, expected);
193 pd = Object.getOwnPropertyDescriptor([1], "length");
202 expectDescriptor(pd, expected);
204 pd = Object.getOwnPropertyDescriptor([1,], "length");
213 expectDescriptor(pd, expected);
215 pd = Object.getOwnPropertyDescriptor([1,,], "length");
224 expectDescriptor(pd, expected);
226 /******************************************************************************/
228 pd = Object.getOwnPropertyDescriptor(new String("foobar"), "length");
237 expectDescriptor(pd, expected);
239 /******************************************************************************/
244 pd = Object.getOwnPropertyDescriptor(o, "length");
253 expectDescriptor(pd, expected);
255 pd = Object.getOwnPropertyDescriptor(o, "prototype");
258 value: foo.prototype,
264 expectDescriptor(pd, expected);
266 /******************************************************************************/
268 pd = Object.getOwnPropertyDescriptor(Function, "length");
277 expectDescriptor(pd, expected);
279 /******************************************************************************/
283 pd = Object.getOwnPropertyDescriptor(o, "source");
292 expectDescriptor(pd, expected);
294 pd = Object.getOwnPropertyDescriptor(o, "global");
303 expectDescriptor(pd, expected);
305 pd = Object.getOwnPropertyDescriptor(o, "ignoreCase");
314 expectDescriptor(pd, expected);
316 pd = Object.getOwnPropertyDescriptor(o, "multiline");
325 expectDescriptor(pd, expected);
327 pd = Object.getOwnPropertyDescriptor(o, "lastIndex");
336 expectDescriptor(pd, expected);
338 /******************************************************************************/
340 reportCompare(expect, actual, "Object.getOwnPropertyDescriptor");
342 printStatus("All tests passed");