From 83ffa3d91182cdfe50d21f1cc683e737d682e7ea Mon Sep 17 00:00:00 2001 From: Alexey Shvayka Date: Fri, 5 Feb 2021 15:57:37 +0200 Subject: [PATCH] Add Object.preventExtensions tests --- ...n-extensible-existing-accessor-property.js | 50 ++++++++++++++++ ...s-non-extensible-existing-data-property.js | 57 +++++++++++++++++++ ...non-extensible-property-creation-throws.js | 53 +++++++++++++++++ 3 files changed, 160 insertions(+) create mode 100644 test/built-ins/Object/assign/target-is-non-extensible-existing-accessor-property.js create mode 100644 test/built-ins/Object/assign/target-is-non-extensible-existing-data-property.js create mode 100644 test/built-ins/Object/assign/target-is-non-extensible-property-creation-throws.js diff --git a/test/built-ins/Object/assign/target-is-non-extensible-existing-accessor-property.js b/test/built-ins/Object/assign/target-is-non-extensible-existing-accessor-property.js new file mode 100644 index 0000000000..205b8d84cb --- /dev/null +++ b/test/built-ins/Object/assign/target-is-non-extensible-existing-accessor-property.js @@ -0,0 +1,50 @@ +// Copyright (C) 2021 Alexey Shvayka. All rights reserved. +// This code is governed by the license found in the LICENSE file. + +/*--- +esid: sec-object.assign +description: > + [[Set]] to existing accessor property of non-extensible `target` is successful. +info: | + OrdinaryPreventExtensions ( O ) + + 1. Set O.[[Extensible]] to false. + + Object.assign ( target, ...sources ) + + [...] + 3. For each element nextSource of sources, do + a. If nextSource is neither undefined nor null, then + [...] + iii. For each element nextKey of keys, do + 1. Let desc be ? from.[[GetOwnProperty]](nextKey). + 2. If desc is not undefined and desc.[[Enumerable]] is true, then + [...] + b. Perform ? Set(to, nextKey, propValue, true). + + OrdinarySetWithOwnDescriptor ( O, P, V, Receiver, ownDesc ) + + [...] + 7. Perform ? Call(setter, Receiver, « V »). + 8. Return true. +features: [Symbol] +---*/ + +var value1 = 1; +var target1 = Object.preventExtensions({ + set foo(val) { value1 = val; }, +}); + +Object.assign(target1, { foo: 2 }); +assert.sameValue(value1, 2); + + +var sym = Symbol(); +var value2 = 1; +var target2 = { + set [sym](val) { value2 = val; }, +}; + +Object.preventExtensions(target2); +Object.assign(target2, { [sym]: 2 }); +assert.sameValue(value2, 2); diff --git a/test/built-ins/Object/assign/target-is-non-extensible-existing-data-property.js b/test/built-ins/Object/assign/target-is-non-extensible-existing-data-property.js new file mode 100644 index 0000000000..dc92f202d4 --- /dev/null +++ b/test/built-ins/Object/assign/target-is-non-extensible-existing-data-property.js @@ -0,0 +1,57 @@ +// Copyright (C) 2021 Alexey Shvayka. All rights reserved. +// This code is governed by the license found in the LICENSE file. + +/*--- +esid: sec-object.assign +description: > + [[Set]] to existing data property of non-extensible `target` is successful. +info: | + OrdinaryPreventExtensions ( O ) + + 1. Set O.[[Extensible]] to false. + + Object.assign ( target, ...sources ) + + [...] + 3. For each element nextSource of sources, do + a. If nextSource is neither undefined nor null, then + [...] + iii. For each element nextKey of keys, do + 1. Let desc be ? from.[[GetOwnProperty]](nextKey). + 2. If desc is not undefined and desc.[[Enumerable]] is true, then + [...] + b. Perform ? Set(to, nextKey, propValue, true). + + OrdinarySetWithOwnDescriptor ( O, P, V, Receiver, ownDesc ) + + [...] + 3. If IsDataDescriptor(ownDesc) is true, then + [...] + c. Let existingDescriptor be ? Receiver.[[GetOwnProperty]](P). + d. If existingDescriptor is not undefined, then + [...] + iii. Let valueDesc be the PropertyDescriptor { [[Value]]: V }. + iv. Return ? Receiver.[[DefineOwnProperty]](P, valueDesc). + + ValidateAndApplyPropertyDescriptor ( O, P, extensible, Desc, current ) + + [...] + 9. If O is not undefined, then + a. For each field of Desc that is present, set the corresponding attribute + of the property named P of object O to the value of the field. + 10. Return true. +features: [Symbol] +---*/ + +var target1 = Object.preventExtensions({ foo: 1 }); + +Object.assign(target1, { foo: 2 }); +assert.sameValue(target1.foo, 2); + + +var sym = Symbol(); +var target2 = { [sym]: 1 }; + +Object.preventExtensions(target2); +Object.assign(target2, { [sym]: 2 }); +assert.sameValue(target2[sym], 2); diff --git a/test/built-ins/Object/assign/target-is-non-extensible-property-creation-throws.js b/test/built-ins/Object/assign/target-is-non-extensible-property-creation-throws.js new file mode 100644 index 0000000000..ea5e4c63de --- /dev/null +++ b/test/built-ins/Object/assign/target-is-non-extensible-property-creation-throws.js @@ -0,0 +1,53 @@ +// Copyright (C) 2021 Alexey Shvayka. All rights reserved. +// This code is governed by the license found in the LICENSE file. + +/*--- +esid: sec-object.assign +description: > + [[Set]] to non-existing property of non-extensible `target` fails with TypeError. +info: | + Object.assign ( target, ...sources ) + + [...] + 3. For each element nextSource of sources, do + a. If nextSource is neither undefined nor null, then + [...] + iii. For each element nextKey of keys, do + 1. Let desc be ? from.[[GetOwnProperty]](nextKey). + 2. If desc is not undefined and desc.[[Enumerable]] is true, then + [...] + b. Perform ? Set(to, nextKey, propValue, true). + + OrdinarySetWithOwnDescriptor ( O, P, V, Receiver, ownDesc ) + + [...] + 3. If IsDataDescriptor(ownDesc) is true, then + [...] + c. Let existingDescriptor be ? Receiver.[[GetOwnProperty]](P). + d. If existingDescriptor is not undefined, then + [...] + e. Else, + i. Assert: Receiver does not currently have a property P. + ii. Return ? CreateDataProperty(Receiver, P, V). + + ValidateAndApplyPropertyDescriptor ( O, P, extensible, Desc, current ) + + [...] + 2. If current is undefined, then + a. If extensible is false, return false. +features: [Symbol] +---*/ + +var target1 = Object.preventExtensions({ foo: 1 }); + +assert.throws(TypeError, function() { + Object.assign(target1, { get bar() {} }); +}); + + +var target2 = {}; + +Object.preventExtensions(target2); +assert.throws(TypeError, function() { + Object.assign(target2, { [Symbol()]: 1 }); +});