michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: var BUGNUMBER = 505587; michael@0: var summary = 'ES5 Object.getOwnPropertyDescriptor(O)'; michael@0: var actual = ''; michael@0: var expect = ''; michael@0: michael@0: printBugNumber(BUGNUMBER); michael@0: printStatus (summary); michael@0: michael@0: /************** michael@0: * BEGIN TEST * michael@0: **************/ michael@0: michael@0: function assertEq(a, e, msg) michael@0: { michael@0: function SameValue(v1, v2) michael@0: { michael@0: if (v1 === 0 && v2 === 0) michael@0: return 1 / v1 === 1 / v2; michael@0: if (v1 !== v1 && v2 !== v2) michael@0: return true; michael@0: return v1 === v2; michael@0: } michael@0: michael@0: if (!SameValue(a, e)) michael@0: { michael@0: var stack = new Error().stack || ""; michael@0: throw "Assertion failed: got " + a + ", expected " + e + michael@0: (msg ? ": " + msg : "") + michael@0: (stack ? "\nStack:\n" + stack : ""); michael@0: } michael@0: } michael@0: michael@0: function expectDescriptor(actual, expected) michael@0: { michael@0: if (actual === undefined && expected === undefined) michael@0: return; michael@0: michael@0: assertEq(typeof actual, "object"); michael@0: assertEq(typeof expected, "object"); michael@0: michael@0: var fields = michael@0: { michael@0: value: true, michael@0: get: true, michael@0: set: true, michael@0: enumerable: true, michael@0: writable: true, michael@0: configurable: true michael@0: }; michael@0: for (var p in fields) michael@0: assertEq(actual.hasOwnProperty(p), expected.hasOwnProperty(p), p); michael@0: for (var p in actual) michael@0: assertEq(p in fields, true, p); michael@0: for (var p in expected) michael@0: assertEq(p in fields, true, p); michael@0: michael@0: assertEq(actual.hasOwnProperty("value"), actual.hasOwnProperty("writable")); michael@0: assertEq(actual.hasOwnProperty("get"), actual.hasOwnProperty("set")); michael@0: if (actual.hasOwnProperty("value")) michael@0: { michael@0: assertEq(actual.value, expected.value); michael@0: assertEq(actual.writable, expected.writable); michael@0: } michael@0: else michael@0: { michael@0: assertEq(actual.get, expected.get); michael@0: assertEq(actual.set, expected.set); michael@0: } michael@0: michael@0: assertEq(actual.hasOwnProperty("enumerable"), true); michael@0: assertEq(actual.hasOwnProperty("configurable"), true); michael@0: assertEq(actual.enumerable, expected.enumerable); michael@0: assertEq(actual.configurable, expected.configurable); michael@0: } michael@0: michael@0: function adjustDescriptorField(o, actual, expect, field) michael@0: { michael@0: assertEq(field === "get" || field === "set", true); michael@0: var lookup = "__lookup" + (field === "get" ? "G" : "S") + "etter"; michael@0: if (typeof o[lookup] === "function") michael@0: expect[field] = o[lookup](field); michael@0: else michael@0: actual[field] = expect[field] = undefined; /* censor if we can't lookup */ michael@0: } michael@0: michael@0: /******************************************************************************/ michael@0: michael@0: var o, pd, expected; michael@0: michael@0: o = { get x() { return 12; } }; michael@0: michael@0: pd = Object.getOwnPropertyDescriptor(o, "x"); michael@0: expected = michael@0: { michael@0: set: undefined, michael@0: enumerable: true, michael@0: configurable: true michael@0: }; michael@0: adjustDescriptorField(o, pd, expected, "get"); michael@0: michael@0: expectDescriptor(pd, expected); michael@0: michael@0: /******************************************************************************/ michael@0: michael@0: var o2; michael@0: michael@0: o = Object.create(Object.prototype, { x: {get: function () { return 12; } } }); michael@0: michael@0: pd = Object.getOwnPropertyDescriptor(o, "x"); michael@0: expected = michael@0: { michael@0: set: undefined, michael@0: enumerable: false, michael@0: configurable: false michael@0: }; michael@0: adjustDescriptorField(o, pd, expected, "get"); michael@0: michael@0: expectDescriptor(pd, expected); michael@0: michael@0: o2 = Object.create(o); michael@0: assertEq(Object.getOwnPropertyDescriptor(o2, "x"), undefined); michael@0: michael@0: /******************************************************************************/ michael@0: michael@0: o = {}; michael@0: o.b = 12; michael@0: michael@0: pd = Object.getOwnPropertyDescriptor(o, "b"); michael@0: expected = michael@0: { michael@0: value: 12, michael@0: writable: true, michael@0: enumerable: true, michael@0: configurable: true michael@0: }; michael@0: expectDescriptor(pd, expected); michael@0: michael@0: /******************************************************************************/ michael@0: michael@0: o = { get y() { return 17; }, set y(z) { } }; michael@0: michael@0: pd = Object.getOwnPropertyDescriptor(o, "y"); michael@0: expected = michael@0: { michael@0: enumerable: true, michael@0: configurable: true michael@0: }; michael@0: adjustDescriptorField(o, pd, expected, "get"); michael@0: adjustDescriptorField(o, pd, expected, "set"); michael@0: michael@0: expectDescriptor(pd, expected); michael@0: michael@0: /******************************************************************************/ michael@0: michael@0: o = {}; michael@0: michael@0: pd = Object.getOwnPropertyDescriptor(o, "absent"); michael@0: michael@0: expectDescriptor(pd, undefined); michael@0: michael@0: /******************************************************************************/ michael@0: michael@0: pd = Object.getOwnPropertyDescriptor([], "length"); michael@0: expected = michael@0: { michael@0: value: 0, michael@0: writable: true, michael@0: enumerable: false, michael@0: configurable: false michael@0: }; michael@0: michael@0: expectDescriptor(pd, expected); michael@0: michael@0: pd = Object.getOwnPropertyDescriptor([1], "length"); michael@0: expected = michael@0: { michael@0: value: 1, michael@0: writable: true, michael@0: enumerable: false, michael@0: configurable: false michael@0: }; michael@0: michael@0: expectDescriptor(pd, expected); michael@0: michael@0: pd = Object.getOwnPropertyDescriptor([1,], "length"); michael@0: expected = michael@0: { michael@0: value: 1, michael@0: writable: true, michael@0: enumerable: false, michael@0: configurable: false michael@0: }; michael@0: michael@0: expectDescriptor(pd, expected); michael@0: michael@0: pd = Object.getOwnPropertyDescriptor([1,,], "length"); michael@0: expected = michael@0: { michael@0: value: 2, michael@0: writable: true, michael@0: enumerable: false, michael@0: configurable: false michael@0: }; michael@0: michael@0: expectDescriptor(pd, expected); michael@0: michael@0: /******************************************************************************/ michael@0: michael@0: pd = Object.getOwnPropertyDescriptor(new String("foobar"), "length"); michael@0: expected = michael@0: { michael@0: value: 6, michael@0: writable: false, michael@0: enumerable: false, michael@0: configurable: false michael@0: }; michael@0: michael@0: expectDescriptor(pd, expected); michael@0: michael@0: /******************************************************************************/ michael@0: michael@0: function foo() { } michael@0: o = foo; michael@0: michael@0: pd = Object.getOwnPropertyDescriptor(o, "length"); michael@0: expected = michael@0: { michael@0: value: 0, michael@0: writable: false, michael@0: enumerable: false, michael@0: configurable: false michael@0: }; michael@0: michael@0: expectDescriptor(pd, expected); michael@0: michael@0: pd = Object.getOwnPropertyDescriptor(o, "prototype"); michael@0: expected = michael@0: { michael@0: value: foo.prototype, michael@0: writable: true, michael@0: enumerable: false, michael@0: configurable: false michael@0: }; michael@0: michael@0: expectDescriptor(pd, expected); michael@0: michael@0: /******************************************************************************/ michael@0: michael@0: pd = Object.getOwnPropertyDescriptor(Function, "length"); michael@0: expected = michael@0: { michael@0: value: 1, michael@0: writable: false, michael@0: enumerable: false, michael@0: configurable: false michael@0: }; michael@0: michael@0: expectDescriptor(pd, expected); michael@0: michael@0: /******************************************************************************/ michael@0: michael@0: o = /foo/im; michael@0: michael@0: pd = Object.getOwnPropertyDescriptor(o, "source"); michael@0: expected = michael@0: { michael@0: value: "foo", michael@0: writable: false, michael@0: enumerable: false, michael@0: configurable: false michael@0: }; michael@0: michael@0: expectDescriptor(pd, expected); michael@0: michael@0: pd = Object.getOwnPropertyDescriptor(o, "global"); michael@0: expected = michael@0: { michael@0: value: false, michael@0: writable: false, michael@0: enumerable: false, michael@0: configurable: false michael@0: }; michael@0: michael@0: expectDescriptor(pd, expected); michael@0: michael@0: pd = Object.getOwnPropertyDescriptor(o, "ignoreCase"); michael@0: expected = michael@0: { michael@0: value: true, michael@0: writable: false, michael@0: enumerable: false, michael@0: configurable: false michael@0: }; michael@0: michael@0: expectDescriptor(pd, expected); michael@0: michael@0: pd = Object.getOwnPropertyDescriptor(o, "multiline"); michael@0: expected = michael@0: { michael@0: value: true, michael@0: writable: false, michael@0: enumerable: false, michael@0: configurable: false michael@0: }; michael@0: michael@0: expectDescriptor(pd, expected); michael@0: michael@0: pd = Object.getOwnPropertyDescriptor(o, "lastIndex"); michael@0: expected = michael@0: { michael@0: value: 0, michael@0: writable: true, michael@0: enumerable: false, michael@0: configurable: false michael@0: }; michael@0: michael@0: expectDescriptor(pd, expected); michael@0: michael@0: /******************************************************************************/ michael@0: michael@0: reportCompare(expect, actual, "Object.getOwnPropertyDescriptor"); michael@0: michael@0: printStatus("All tests passed");