From 2fcf931f81b7f774e40e23d36562ef8d60001da4 Mon Sep 17 00:00:00 2001 From: Leonardo Balter Date: Tue, 28 Jul 2015 17:14:36 -0400 Subject: [PATCH] Add tests for Reflect.defineProperty --- .../defineProperty/define-properties.js | 39 ++++++++++++++ .../define-symbol-properties.js | 53 +++++++++++++++++++ .../Reflect/defineProperty/defineProperty.js | 17 ++++++ .../Reflect/defineProperty/length.js | 17 ++++++ test/built-ins/Reflect/defineProperty/name.js | 22 ++++++++ .../return-abrupt-from-attributes.js | 26 +++++++++ .../return-abrupt-from-property-key.js | 24 +++++++++ .../return-abrupt-from-result.js | 31 +++++++++++ .../Reflect/defineProperty/return-boolean.js | 32 +++++++++++ .../target-is-not-object-throws.js | 28 ++++++++++ .../defineProperty/target-is-symbol-throws.js | 17 ++++++ 11 files changed, 306 insertions(+) create mode 100644 test/built-ins/Reflect/defineProperty/define-properties.js create mode 100644 test/built-ins/Reflect/defineProperty/define-symbol-properties.js create mode 100644 test/built-ins/Reflect/defineProperty/defineProperty.js create mode 100644 test/built-ins/Reflect/defineProperty/length.js create mode 100644 test/built-ins/Reflect/defineProperty/name.js create mode 100644 test/built-ins/Reflect/defineProperty/return-abrupt-from-attributes.js create mode 100644 test/built-ins/Reflect/defineProperty/return-abrupt-from-property-key.js create mode 100644 test/built-ins/Reflect/defineProperty/return-abrupt-from-result.js create mode 100644 test/built-ins/Reflect/defineProperty/return-boolean.js create mode 100644 test/built-ins/Reflect/defineProperty/target-is-not-object-throws.js create mode 100644 test/built-ins/Reflect/defineProperty/target-is-symbol-throws.js diff --git a/test/built-ins/Reflect/defineProperty/define-properties.js b/test/built-ins/Reflect/defineProperty/define-properties.js new file mode 100644 index 0000000000..45ea369b64 --- /dev/null +++ b/test/built-ins/Reflect/defineProperty/define-properties.js @@ -0,0 +1,39 @@ +// Copyright (C) 2015 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +es6id: 26.1.3 +description: > + Define properties from the attributes object. +info: > + 26.1.3 Reflect.defineProperty ( target, propertyKey, attributes ) + + ... + 6. Return target.[[DefineOwnProperty]](key, desc). +includes: [propertyHelper.js] +---*/ + +var o = {}; +var desc; + +Reflect.defineProperty(o, 'p1', { + value: 42, + writable: true, + enumerable: true +}); + +assert.sameValue(o.p1, 42); + +verifyWritable(o, 'p1'); +verifyNotConfigurable(o, 'p1'); +verifyEnumerable(o, 'p1'); + +var f1 = function() {}; +var f2 = function() {}; +Reflect.defineProperty(o, 'p2', { + get: f1, + set: f2 +}); + +desc = Object.getOwnPropertyDescriptor(o, 'p2'); +assert.sameValue(desc.get, f1); +assert.sameValue(desc.set, f2); diff --git a/test/built-ins/Reflect/defineProperty/define-symbol-properties.js b/test/built-ins/Reflect/defineProperty/define-symbol-properties.js new file mode 100644 index 0000000000..79e1a5d0ca --- /dev/null +++ b/test/built-ins/Reflect/defineProperty/define-symbol-properties.js @@ -0,0 +1,53 @@ +// Copyright (C) 2015 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +es6id: 26.1.3 +description: > + Define symbol properties. +info: > + 26.1.3 Reflect.defineProperty ( target, propertyKey, attributes ) + + ... + 2. Let key be ToPropertyKey(propertyKey). + ... + + 7.1.14 ToPropertyKey ( argument ) + + ... + 3. If Type(key) is Symbol, then + a. Return key. + ... +features: [Symbol] +---*/ + +var o = {}; +var desc; + +var s1 = Symbol('1'); + +Reflect.defineProperty(o, s1, { + value: 42, + writable: true, + enumerable: true +}); + +assert.sameValue(o[s1], 42); + +desc = Object.getOwnPropertyDescriptor(o, s1); + +assert.sameValue(desc.writable, true); +assert.sameValue(desc.configurable, false); +assert.sameValue(desc.enumerable, true); + +var s2 = Symbol('2'); + +var f1 = function() {}; +var f2 = function() {}; +Reflect.defineProperty(o, s2, { + get: f1, + set: f2 +}); + +desc = Object.getOwnPropertyDescriptor(o, s2); +assert.sameValue(desc.get, f1); +assert.sameValue(desc.set, f2); diff --git a/test/built-ins/Reflect/defineProperty/defineProperty.js b/test/built-ins/Reflect/defineProperty/defineProperty.js new file mode 100644 index 0000000000..d95496aa08 --- /dev/null +++ b/test/built-ins/Reflect/defineProperty/defineProperty.js @@ -0,0 +1,17 @@ +// Copyright (C) 2015 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +es6id: 26.1.3 +description: > + Reflect.defineProperty is configurable, writable and not enumerable. +info: > + 26.1.3 Reflect.defineProperty ( target, propertyKey, attributes ) + + 17 ECMAScript Standard Built-in Objects + +includes: [propertyHelper.js] +---*/ + +verifyNotEnumerable(Reflect, 'defineProperty'); +verifyWritable(Reflect, 'defineProperty'); +verifyConfigurable(Reflect, 'defineProperty'); diff --git a/test/built-ins/Reflect/defineProperty/length.js b/test/built-ins/Reflect/defineProperty/length.js new file mode 100644 index 0000000000..02005b2e24 --- /dev/null +++ b/test/built-ins/Reflect/defineProperty/length.js @@ -0,0 +1,17 @@ +// Copyright (C) 2015 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +es6id: 26.1.3 +description: > + Reflect.defineProperty.length value and property descriptor +includes: [propertyHelper.js] +---*/ + +assert.sameValue( + Reflect.defineProperty.length, 3, + 'The value of `Reflect.defineProperty.length` is `3`' +); + +verifyNotEnumerable(Reflect.defineProperty, 'length'); +verifyNotWritable(Reflect.defineProperty, 'length'); +verifyConfigurable(Reflect.defineProperty, 'length'); diff --git a/test/built-ins/Reflect/defineProperty/name.js b/test/built-ins/Reflect/defineProperty/name.js new file mode 100644 index 0000000000..fea916ebef --- /dev/null +++ b/test/built-ins/Reflect/defineProperty/name.js @@ -0,0 +1,22 @@ +// Copyright (C) 2015 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +es6id: 26.1.3 +description: > + Reflect.defineProperty.name value and property descriptor +info: > + 26.1.3 Reflect.defineProperty ( target, propertyKey, attributes ) + + 17 ECMAScript Standard Built-in Objects + +includes: [propertyHelper.js] +---*/ + +assert.sameValue( + Reflect.defineProperty.name, 'defineProperty', + 'The value of `Reflect.defineProperty.name` is `"defineProperty"`' +); + +verifyNotEnumerable(Reflect.defineProperty, 'name'); +verifyNotWritable(Reflect.defineProperty, 'name'); +verifyConfigurable(Reflect.defineProperty, 'name'); diff --git a/test/built-ins/Reflect/defineProperty/return-abrupt-from-attributes.js b/test/built-ins/Reflect/defineProperty/return-abrupt-from-attributes.js new file mode 100644 index 0000000000..34ec9d586f --- /dev/null +++ b/test/built-ins/Reflect/defineProperty/return-abrupt-from-attributes.js @@ -0,0 +1,26 @@ +// Copyright (C) 2015 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +es6id: 26.1.3 +description: > + Return abrupt from ToPropertyDescriptor(attributes). +info: > + 26.1.3 Reflect.defineProperty ( target, propertyKey, attributes ) + + ... + 4. Let desc be ToPropertyDescriptor(attributes). + 5. ReturnIfAbrupt(desc). + ... +---*/ + +var attributes = {}; + +Object.defineProperty(attributes, 'enumerable', { + get: function() { + throw new Test262Error(); + } +}); + +assert.throws(Test262Error, function() { + Reflect.defineProperty({}, 'a', attributes); +}); diff --git a/test/built-ins/Reflect/defineProperty/return-abrupt-from-property-key.js b/test/built-ins/Reflect/defineProperty/return-abrupt-from-property-key.js new file mode 100644 index 0000000000..d6efe614c4 --- /dev/null +++ b/test/built-ins/Reflect/defineProperty/return-abrupt-from-property-key.js @@ -0,0 +1,24 @@ +// Copyright (C) 2015 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +es6id: 26.1.3 +description: > + Return abrupt from ToPropertyKey(propertyKey) +info: > + 26.1.3 Reflect.defineProperty ( target, propertyKey, attributes ) + + ... + 2. Let key be ToPropertyKey(propertyKey). + 3. ReturnIfAbrupt(key). + ... +---*/ + +var p = { + toString: function() { + throw new Test262Error(); + } +}; + +assert.throws(Test262Error, function() { + Reflect.defineProperty({}, p); +}); diff --git a/test/built-ins/Reflect/defineProperty/return-abrupt-from-result.js b/test/built-ins/Reflect/defineProperty/return-abrupt-from-result.js new file mode 100644 index 0000000000..13664f6b1c --- /dev/null +++ b/test/built-ins/Reflect/defineProperty/return-abrupt-from-result.js @@ -0,0 +1,31 @@ +// Copyright (C) 2015 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +es6id: 26.1.3 +description: > + Return abrupt result on defining a property. +info: > + 26.1.3 Reflect.defineProperty ( target, propertyKey, attributes ) + + ... + 6. Return target.[[DefineOwnProperty]](key, desc). + ... + + 9.1.6.1 OrdinaryDefineOwnProperty (O, P, Desc) + + 1. Let current be O.[[GetOwnProperty]](P). + 2. ReturnIfAbrupt(current). + ... +features: [Proxy] +---*/ + +var o = {}; +var p = new Proxy(o, { + defineProperty: function() { + throw new Test262Error(); + } +}); + +assert.throws(Test262Error, function() { + Reflect.defineProperty(p, 'p1', {}); +}); diff --git a/test/built-ins/Reflect/defineProperty/return-boolean.js b/test/built-ins/Reflect/defineProperty/return-boolean.js new file mode 100644 index 0000000000..deb9dd00df --- /dev/null +++ b/test/built-ins/Reflect/defineProperty/return-boolean.js @@ -0,0 +1,32 @@ +// Copyright (C) 2015 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +es6id: 26.1.3 +description: > + Return boolean result of the property definition. +info: > + 26.1.3 Reflect.defineProperty ( target, propertyKey, attributes ) + + ... + 6. Return target.[[DefineOwnProperty]](key, desc). +---*/ + +var o = {}; + +o.p1 = 'foo'; +assert.sameValue(Reflect.defineProperty(o, 'p1', {}), true); +assert.sameValue(o.hasOwnProperty('p1'), true); + +assert.sameValue(Reflect.defineProperty(o, 'p2', {value: 42}), true); +assert.sameValue(o.hasOwnProperty('p2'), true); + +Object.freeze(o); + +assert.sameValue(Reflect.defineProperty(o, 'p2', {value: 43}), false); +assert.sameValue(o.p2, 42); + +assert.sameValue(Reflect.defineProperty(o, 'p3', {}), false); +assert.sameValue(o.hasOwnProperty('p4'), false); + +assert.sameValue(Reflect.defineProperty(o, 'p4', {value: 1}), false); +assert.sameValue(o.hasOwnProperty('p4'), false); diff --git a/test/built-ins/Reflect/defineProperty/target-is-not-object-throws.js b/test/built-ins/Reflect/defineProperty/target-is-not-object-throws.js new file mode 100644 index 0000000000..421e700cf6 --- /dev/null +++ b/test/built-ins/Reflect/defineProperty/target-is-not-object-throws.js @@ -0,0 +1,28 @@ +// Copyright (C) 2015 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +es6id: 26.1.3 +description: > + Throws a TypeError if target is not an Object. +info: > + 26.1.3 Reflect.defineProperty ( target, propertyKey, attributes ) + + 1. If Type(target) is not Object, throw a TypeError exception. + ... +---*/ + +assert.throws(TypeError, function() { + Reflect.defineProperty(1, 'p', {}); +}); + +assert.throws(TypeError, function() { + Reflect.defineProperty(null, 'p', {}); +}); + +assert.throws(TypeError, function() { + Reflect.defineProperty(undefined, 'p', {}); +}); + +assert.throws(TypeError, function() { + Reflect.defineProperty('', 'p', {}); +}); diff --git a/test/built-ins/Reflect/defineProperty/target-is-symbol-throws.js b/test/built-ins/Reflect/defineProperty/target-is-symbol-throws.js new file mode 100644 index 0000000000..5cef833187 --- /dev/null +++ b/test/built-ins/Reflect/defineProperty/target-is-symbol-throws.js @@ -0,0 +1,17 @@ +// Copyright (C) 2015 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +es6id: 26.1.3 +description: > + Throws a TypeError if target is a Symbol +info: > + 26.1.3 Reflect.defineProperty ( target, propertyKey, attributes ) + + 1. If Type(target) is not Object, throw a TypeError exception. + ... +features: [Symbol] +---*/ + +assert.throws(TypeError, function() { + Reflect.defineProperty(Symbol(1), 'p', {}); +});