Skip to content
Snippets Groups Projects
Commit d3743c3b authored by Leonardo Balter's avatar Leonardo Balter
Browse files

Add tests for Reflect.set

parent e7f5f3cf
No related branches found
No related tags found
No related merge requests found
Showing
with 637 additions and 0 deletions
// 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.13
description: >
Call accessor's set from target's prototype.
info: >
26.1.13 Reflect.set ( target, propertyKey, V [ , receiver ] )
...
4. If receiver is not present, then
a. Let receiver be target.
5. Return target.[[Set]](key, V, receiver).
9.1.9 [[Set]] ( P, V, Receiver)
...
4. If ownDesc is undefined, then
a. Let parent be O.[[GetPrototypeOf]]().
b. ReturnIfAbrupt(parent).
c. If parent is not null, then
i. Return parent.[[Set]](P, V, Receiver).
...
11. Return true.
---*/
var args;
var count = 0;
var _this;
var proto = {};
Object.defineProperty(proto, 'p', {
set: function() {
_this = this;
args = arguments;
count++;
}
});
var target = Object.create(proto);
var result = Reflect.set(target, 'p', 42);
assert.sameValue(result, true, 'returns true');
assert.sameValue(args.length, 1, 'prototype `set` called with 1 argument');
assert.sameValue(args[0], 42, 'prototype `set` called with 42');
assert.sameValue(_this, target, 'prototype `set` called with target as `this`');
assert.sameValue(count, 1, 'prototype `set` called once');
// 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.13
description: >
Creates a property data descriptor.
info: >
26.1.13 Reflect.set ( target, propertyKey, V [ , receiver ] )
...
4. If receiver is not present, then
a. Let receiver be target.
5. Return target.[[Set]](key, V, receiver).
9.1.9 [[Set]] ( P, V, Receiver)
...
4. If ownDesc is undefined, then
a. Let parent be O.[[GetPrototypeOf]]().
b. ReturnIfAbrupt(parent).
c. If parent is not null, then
i. Return parent.[[Set]](P, V, Receiver).
d. Else,
ii. Let ownDesc be the PropertyDescriptor{[[Value]]: undefined,
[[Writable]]: true, [[Enumerable]]: true, [[Configurable]]: true}.
5. If IsDataDescriptor(ownDesc) is true, then
a. If ownDesc.[[Writable]] is false, return false.
b. If Type(Receiver) is not Object, return false.
c. Let existingDescriptor be Receiver.[[GetOwnProperty]](P).
d. ReturnIfAbrupt(existingDescriptor).
e. If existingDescriptor is not undefined, then
i. If IsAccessorDescriptor(existingDescriptor) is true, return false.
ii. If existingDescriptor.[[Writable]] is false, return false.
iii. Let valueDesc be the PropertyDescriptor{[[Value]]: V}.
iv. Return Receiver.[[DefineOwnProperty]](P, valueDesc).
f. Else Receiver does not currently have a property P,
i. Return CreateDataProperty(Receiver, P, V).
6. Assert: IsAccessorDescriptor(ownDesc) is true.
7. Let setter be ownDesc.[[Set]].
8. If setter is undefined, return false.
...
11. Return true.
includes: [propertyHelper.js]
---*/
var o1 = {};
var result = Reflect.set(o1, 'p', 42);
assert.sameValue(result, true, 'returns true on a successful setting');
var desc = Object.getOwnPropertyDescriptor(o1, 'p');
assert.sameValue(
desc.value, 42,
'sets a data descriptor to set a new property'
);
verifyWritable(o1, 'p');
verifyEnumerable(o1, 'p');
verifyConfigurable(o1, 'p');
var o2 = {};
var receiver = {};
result = Reflect.set(o2, 'p', 43, receiver);
assert.sameValue(
result, true,
'returns true on a successful setting with a receiver'
);
desc = Object.getOwnPropertyDescriptor(o2, 'p');
assert.sameValue(
desc, undefined,
'does not set a data descriptor on target if receiver is given'
);
desc = Object.getOwnPropertyDescriptor(receiver, 'p');
assert.sameValue(
desc.value, 43,
'sets a data descriptor on the receiver object to set a new property'
);
verifyWritable(receiver, 'p');
verifyEnumerable(receiver, 'p');
verifyConfigurable(receiver, 'p');
// 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.13
description: >
Return false if target property turns to a data descriptor and receiver
property is an accessor descriptor.
info: >
26.1.13 Reflect.set ( target, propertyKey, V [ , receiver ] )
...
4. If receiver is not present, then
a. Let receiver be target.
5. Return target.[[Set]](key, V, receiver).
9.1.9 [[Set]] ( P, V, Receiver)
...
4. If ownDesc is undefined, then
a. Let parent be O.[[GetPrototypeOf]]().
b. ReturnIfAbrupt(parent).
c. If parent is not null, then
i. Return parent.[[Set]](P, V, Receiver).
d. Else,
ii. Let ownDesc be the PropertyDescriptor{[[Value]]: undefined,
[[Writable]]: true, [[Enumerable]]: true, [[Configurable]]: true}.
5. If IsDataDescriptor(ownDesc) is true, then
a. If ownDesc.[[Writable]] is false, return false.
b. If Type(Receiver) is not Object, return false.
c. Let existingDescriptor be Receiver.[[GetOwnProperty]](P).
d. ReturnIfAbrupt(existingDescriptor).
e. If existingDescriptor is not undefined, then
i. If IsAccessorDescriptor(existingDescriptor) is true, return false.
...
---*/
var receiver = {};
var fn = function() {};
Object.defineProperty(receiver, 'p', {
set: fn
});
var o1 = {};
var result = Reflect.set(o1, 'p', 42, receiver);
assert.sameValue(
result, false,
'target has no own `p` and receiver.p has an accessor descriptor'
);
assert.sameValue(
Object.getOwnPropertyDescriptor(receiver, 'p').set, fn,
'receiver.p is not changed'
);
assert.sameValue(o1.hasOwnProperty('p'), false, 'target.p is not set');
var o2 = {p: 43};
result = Reflect.set(o2, 'p', 42, receiver);
assert.sameValue(
result, false,
'target.p has a data descriptor and receiver.p has an accessor descriptor'
);
assert.sameValue(
Object.getOwnPropertyDescriptor(receiver, 'p').set, fn,
'receiver.p is not changed'
);
assert.sameValue(o2.p, 43, 'target.p is not changed');
// 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.13
description: >
Reflect.set.length value and property descriptor
info: >
26.1.13 Reflect.set ( target, propertyKey, V [ , receiver ] )
The length property of the set function is 3.
includes: [propertyHelper.js]
---*/
assert.sameValue(
Reflect.set.length, 3,
'The value of `Reflect.set.length` is `3`'
);
verifyNotEnumerable(Reflect.set, 'length');
verifyNotWritable(Reflect.set, 'length');
verifyConfigurable(Reflect.set, 'length');
// 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.13
description: >
Reflect.set.name value and property descriptor
info: >
26.1.13 Reflect.set ( target, propertyKey, V [ , receiver ] )
17 ECMAScript Standard Built-in Objects
includes: [propertyHelper.js]
---*/
assert.sameValue(
Reflect.set.name, 'set',
'The value of `Reflect.set.name` is `"set"`'
);
verifyNotEnumerable(Reflect.set, 'name');
verifyNotWritable(Reflect.set, 'name');
verifyConfigurable(Reflect.set, 'name');
// 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.13
description: >
Return false if receiver is not an object.
info: >
26.1.13 Reflect.set ( target, propertyKey, V [ , receiver ] )
...
4. If receiver is not present, then
a. Let receiver be target.
5. Return target.[[Set]](key, V, receiver).
9.1.9 [[Set]] ( P, V, Receiver)
...
4. If ownDesc is undefined, then
a. Let parent be O.[[GetPrototypeOf]]().
b. ReturnIfAbrupt(parent).
c. If parent is not null, then
i. Return parent.[[Set]](P, V, Receiver).
d. Else,
ii. Let ownDesc be the PropertyDescriptor{[[Value]]: undefined,
[[Writable]]: true, [[Enumerable]]: true, [[Configurable]]: true}.
5. If IsDataDescriptor(ownDesc) is true, then
a. If ownDesc.[[Writable]] is false, return false.
b. If Type(Receiver) is not Object, return false.
...
---*/
var o1 = {p: 42};
var receiver = 'receiver is a string';
var result = Reflect.set(o1, 'p', 43, receiver);
assert.sameValue(result, false, 'returns false');
assert.sameValue(o1.p, 42, 'does not set a value');
assert.sameValue(
receiver.hasOwnProperty('p'), false,
'does not set a new property on receiver if it is not an object'
);
// 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.13
description: >
Return abrupt from ToPropertyKey(propertyKey)
info: >
26.1.13 Reflect.set ( target, propertyKey, V [ , receiver ] )
...
2. Let key be ToPropertyKey(propertyKey).
3. ReturnIfAbrupt(key).
...
---*/
var p = {
toString: function() {
throw new Test262Error();
}
};
assert.throws(Test262Error, function() {
Reflect.set({}, p);
});
// 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.13
description: >
Return abrupt result from get a property value.
info: >
26.1.13 Reflect.set ( target, propertyKey, V [ , receiver ] )
...
5. Return target.[[Set]](key, V, receiver).
---*/
var o1 = {};
Object.defineProperty(o1, 'p1', {
set: function() {
throw new Test262Error();
}
});
assert.throws(Test262Error, function() {
Reflect.set(o1, 'p1', 42);
});
// Abrupt from the prototype property
var o2 = Object.create(o1);
assert.throws(Test262Error, function() {
Reflect.set(o2, 'p1', 42);
});
// 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.13
description: >
Return false if receiver is not writable.
info: >
26.1.13 Reflect.set ( target, propertyKey, V [ , receiver ] )
...
4. If receiver is not present, then
a. Let receiver be target.
5. Return target.[[Set]](key, V, receiver).
9.1.9 [[Set]] ( P, V, Receiver)
...
5. If IsDataDescriptor(ownDesc) is true, then
a. If ownDesc.[[Writable]] is false, return false.
...
---*/
var o1 = {};
Object.defineProperty(o1, 'p', {
writable: false,
value: 42
});
var result = Reflect.set(o1, 'p', 43);
assert.sameValue(result, false, 'returns false');
assert.sameValue(o1.p, 42, 'does not set a new value');
// 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.13
description: >
Return false if target is not writable.
info: >
26.1.13 Reflect.set ( target, propertyKey, V [ , receiver ] )
...
4. If receiver is not present, then
a. Let receiver be target.
5. Return target.[[Set]](key, V, receiver).
9.1.9 [[Set]] ( P, V, Receiver)
...
5. If IsDataDescriptor(ownDesc) is true, then
a. If ownDesc.[[Writable]] is false, return false.
b. If Type(Receiver) is not Object, return false.
c. Let existingDescriptor be Receiver.[[GetOwnProperty]](P).
d. ReturnIfAbrupt(existingDescriptor).
e. If existingDescriptor is not undefined, then
i. If IsAccessorDescriptor(existingDescriptor) is true, return false.
ii. If existingDescriptor.[[Writable]] is false, return false.
...
---*/
var o1 = {};
var receiver = {};
Object.defineProperty(receiver, 'p', {
writable: false,
value: 42
});
var result = Reflect.set(o1, 'p', 43, receiver);
assert.sameValue(result, false, 'returns false');
assert.sameValue(receiver.p, 42, 'does not set a new value on receiver');
assert.sameValue(
o1.hasOwnProperty('p'), false,
'does not set a new value on target'
);
// 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.13
description: >
Set value on an accessor descriptor property with receiver as `this`.
info: >
26.1.13 Reflect.set ( target, propertyKey, V [ , receiver ] )
...
4. If receiver is not present, then
a. Let receiver be target.
5. Return target.[[Set]](key, V, receiver).
9.1.9 [[Set]] ( P, V, Receiver)
...
6. Assert: IsAccessorDescriptor(ownDesc) is true.
7. Let setter be ownDesc.[[Set]].
8. If setter is undefined, return false.
9. Let setterResult be Call(setter, Receiver, «V»).
10. ReturnIfAbrupt(setterResult).
11. Return true.
---*/
var args;
var count = 0
var o1 = {};
var receiver = {};
var _receiver;
Object.defineProperty(o1, 'p', {
set: function(a) {
count++;
args = arguments;
_receiver = this;
return false;
}
});
var result = Reflect.set(o1, 'p', 42, receiver);
assert.sameValue(
result, true,
'returns true if target.p has an accessor descriptor'
);
assert.sameValue(args.length, 1, 'target.p set is called with 1 argument');
assert.sameValue(args[0], 42, 'target.p set is called with V');
assert.sameValue(count, 1, 'target.p set is called once');
assert.sameValue(
_receiver, receiver,
'target.p set is called with receiver as `this`'
);
// 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.13
description: >
Set value on an accessor descriptor property.
info: >
26.1.13 Reflect.set ( target, propertyKey, V [ , receiver ] )
...
4. If receiver is not present, then
a. Let receiver be target.
5. Return target.[[Set]](key, V, receiver).
9.1.9 [[Set]] ( P, V, Receiver)
...
6. Assert: IsAccessorDescriptor(ownDesc) is true.
7. Let setter be ownDesc.[[Set]].
8. If setter is undefined, return false.
9. Let setterResult be Call(setter, Receiver, «V»).
10. ReturnIfAbrupt(setterResult).
11. Return true.
---*/
var args;
var count = 0
var o1 = {};
Object.defineProperty(o1, 'p', {
set: function(a) {
count++;
args = arguments;
return false;
}
});
var result = Reflect.set(o1, 'p', 42);
assert.sameValue(
result, true,
'returns true if target.p has an accessor descriptor'
);
assert.sameValue(args.length, 1, 'target.p set is called with 1 argument');
assert.sameValue(args[0], 42, 'target.p set is called with V');
assert.sameValue(count, 1, 'target.p set is called once');
// 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.13
description: >
Sets the new value.
info: >
26.1.13 Reflect.set ( target, propertyKey, V [ , receiver ] )
...
4. If receiver is not present, then
a. Let receiver be target.
5. Return target.[[Set]](key, V, receiver).
9.1.9 [[Set]] ( P, V, Receiver)
...
5. If IsDataDescriptor(ownDesc) is true, then
a. If ownDesc.[[Writable]] is false, return false.
b. If Type(Receiver) is not Object, return false.
c. Let existingDescriptor be Receiver.[[GetOwnProperty]](P).
d. ReturnIfAbrupt(existingDescriptor).
e. If existingDescriptor is not undefined, then
i. If IsAccessorDescriptor(existingDescriptor) is true, return false.
ii. If existingDescriptor.[[Writable]] is false, return false.
iii. Let valueDesc be the PropertyDescriptor{[[Value]]: V}.
iv. Return Receiver.[[DefineOwnProperty]](P, valueDesc).
f. Else Receiver does not currently have a property P,
i. Return CreateDataProperty(Receiver, P, V).
...
---*/
var o1 = {p: 43};
var result = Reflect.set(o1, 'p', 42);
assert.sameValue(result, true, 'returns true on a successful setting');
assert.sameValue(
o1.p, 42,
'sets the new value'
);
var o2 = {p: 43};
var receiver = {p: 44};
var result = Reflect.set(o2, 'p', 42, receiver);
assert.sameValue(result, true, 'returns true on a successful setting');
assert.sameValue(o2.p, 43, 'with a receiver, does not set a value on target');
assert.sameValue(receiver.p, 42, 'sets the new value on the receiver');
// 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.13
description: >
Reflect.set is configurable, writable and not enumerable.
info: >
26.1.13 Reflect.set ( target, propertyKey, V [ , receiver ] )
17 ECMAScript Standard Built-in Objects
includes: [propertyHelper.js]
---*/
verifyNotEnumerable(Reflect, 'set');
verifyWritable(Reflect, 'set');
verifyConfigurable(Reflect, 'set');
// 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.13
description: >
Sets the new value.
info: >
26.1.13 Reflect.set ( target, propertyKey, V [ , receiver ] )
...
2. Let key be ToPropertyKey(propertyKey).
...
7.1.14 ToPropertyKey ( argument )
...
3. If Type(key) is Symbol, then
a. Return key.
...
features: [Symbol]
---*/
var o1 = {};
var s = Symbol('1');
var result = Reflect.set(o1, s, 42);
assert.sameValue(result, true, 'returns true on a successful setting');
assert.sameValue(o1[s], 42, 'sets the new value');
var o2 = {};
o2[s] = 43;
var receiver = {};
receiver[s] = 44;
var result = Reflect.set(o2, s, 42, receiver);
assert.sameValue(result, true, 'returns true on a successful setting');
assert.sameValue(o2[s], 43, 'with a receiver, does not set a value on target');
assert.sameValue(receiver[s], 42, 'sets the new value on the receiver');
// 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.13
description: >
Throws a TypeError if target is not an Object.
info: >
26.1.13 Reflect.set ( target, propertyKey, V [ , receiver ] )
1. If Type(target) is not Object, throw a TypeError exception.
...
---*/
assert.throws(TypeError, function() {
Reflect.set(1, 'p', 42);
});
assert.throws(TypeError, function() {
Reflect.set(null, 'p', 42);
});
assert.throws(TypeError, function() {
Reflect.set(undefined, 'p', 42);
});
assert.throws(TypeError, function() {
Reflect.set('', 'p', 42);
});
// 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.13
description: >
Throws a TypeError if target is a Symbol
info: >
26.1.13 Reflect.set ( target, propertyKey, V [ , receiver ] )
1. If Type(target) is not Object, throw a TypeError exception.
...
features: [Symbol]
---*/
assert.throws(TypeError, function() {
Reflect.set(Symbol(1), 'p', 42);
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment