michael@0: /* michael@0: * Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/licenses/publicdomain/ michael@0: */ michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: var BUGNUMBER = 430133; michael@0: var summary = 'ES5 Object.defineProperties(O, Properties)'; michael@0: michael@0: print(BUGNUMBER + ": " + summary); michael@0: michael@0: /************** michael@0: * BEGIN TEST * michael@0: **************/ michael@0: michael@0: assertEq("defineProperties" in Object, true); michael@0: assertEq(Object.defineProperties.length, 2); michael@0: michael@0: var o, props, desc, passed; michael@0: michael@0: o = {}; michael@0: props = michael@0: { michael@0: a: { value: 17, enumerable: true, configurable: true, writable: true }, michael@0: b: { value: 42, enumerable: false, configurable: false, writable: false } michael@0: }; michael@0: Object.defineProperties(o, props); michael@0: assertEq("a" in o, true); michael@0: assertEq("b" in o, true); michael@0: desc = Object.getOwnPropertyDescriptor(o, "a"); michael@0: assertEq(desc.value, 17); michael@0: assertEq(desc.enumerable, true); michael@0: assertEq(desc.configurable, true); michael@0: assertEq(desc.writable, true); michael@0: desc = Object.getOwnPropertyDescriptor(o, "b"); michael@0: assertEq(desc.value, 42); michael@0: assertEq(desc.enumerable, false); michael@0: assertEq(desc.configurable, false); michael@0: assertEq(desc.writable, false); michael@0: michael@0: props = michael@0: { michael@0: c: { value: NaN, enumerable: false, configurable: true, writable: true }, michael@0: b: { value: 44 } michael@0: }; michael@0: var error = "before"; michael@0: try michael@0: { michael@0: Object.defineProperties(o, props); michael@0: error = "no exception thrown"; michael@0: } michael@0: catch (e) michael@0: { michael@0: if (e instanceof TypeError) michael@0: error = "typeerror"; michael@0: else michael@0: error = "bad exception: " + e; michael@0: } michael@0: assertEq(error, "typeerror", "didn't throw or threw wrongly"); michael@0: assertEq("c" in o, true, "new property added"); michael@0: assertEq(o.b, 42, "old property value preserved"); michael@0: michael@0: function Properties() { } michael@0: Properties.prototype = { b: { value: 42, enumerable: true } }; michael@0: props = new Properties(); michael@0: Object.defineProperty(props, "a", { enumerable: false }); michael@0: o = {}; michael@0: Object.defineProperties(o, props); michael@0: assertEq("a" in o, false); michael@0: assertEq(Object.getOwnPropertyDescriptor(o, "a"), undefined, michael@0: "Object.defineProperties(O, Properties) should only use enumerable " + michael@0: "properties on Properties"); michael@0: assertEq("b" in o, false); michael@0: assertEq(Object.getOwnPropertyDescriptor(o, "b"), undefined, michael@0: "Object.defineProperties(O, Properties) should only use enumerable " + michael@0: "properties directly on Properties"); michael@0: michael@0: Number.prototype.foo = { value: 17, enumerable: true }; michael@0: Boolean.prototype.bar = { value: 8675309, enumerable: true }; michael@0: String.prototype.quux = { value: "Are you HAPPY yet?", enumerable: true }; michael@0: o = {}; michael@0: Object.defineProperties(o, 5); // ToObject only throws for null/undefined michael@0: assertEq("foo" in o, false, "foo is not an enumerable own property"); michael@0: Object.defineProperties(o, false); michael@0: assertEq("bar" in o, false, "bar is not an enumerable own property"); michael@0: Object.defineProperties(o, ""); michael@0: assertEq("quux" in o, false, "quux is not an enumerable own property"); michael@0: michael@0: error = "before"; michael@0: try michael@0: { michael@0: Object.defineProperties(o, "1"); michael@0: } michael@0: catch (e) michael@0: { michael@0: if (e instanceof TypeError) michael@0: error = "typeerror"; michael@0: else michael@0: error = "bad exception: " + e; michael@0: } michael@0: assertEq(error, "typeerror", michael@0: "should throw on Properties == '1' due to '1'[0] not being a " + michael@0: "property descriptor"); michael@0: michael@0: error = "before"; michael@0: try michael@0: { michael@0: Object.defineProperties(o, null); michael@0: } michael@0: catch (e) michael@0: { michael@0: if (e instanceof TypeError) michael@0: error = "typeerror"; michael@0: else michael@0: error = "bad exception: " + e; michael@0: } michael@0: assertEq(error, "typeerror", "should throw on Properties == null"); michael@0: michael@0: error = "before"; michael@0: try michael@0: { michael@0: Object.defineProperties(o, undefined); michael@0: } michael@0: catch (e) michael@0: { michael@0: if (e instanceof TypeError) michael@0: error = "typeerror"; michael@0: else michael@0: error = "bad exception: " + e; michael@0: } michael@0: assertEq(error, "typeerror", "should throw on Properties == undefined"); michael@0: michael@0: /******************************************************************************/ michael@0: michael@0: reportCompare(true, true); michael@0: michael@0: print("All tests passed!");