mirror of https://github.com/tc39/test262.git
Add tests for Reflect.defineProperty
This commit is contained in:
parent
980e50441f
commit
2fcf931f81
|
@ -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);
|
|
@ -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);
|
|
@ -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');
|
|
@ -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');
|
|
@ -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');
|
|
@ -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);
|
||||
});
|
|
@ -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);
|
||||
});
|
|
@ -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', {});
|
||||
});
|
|
@ -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);
|
|
@ -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', {});
|
||||
});
|
|
@ -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', {});
|
||||
});
|
Loading…
Reference in New Issue