Skip to content
Snippets Groups Projects
Commit f06791ea authored by Brian Terlson's avatar Brian Terlson
Browse files

Merge pull request #328 from bocoup/object-set-prototype-of

Add tests for Object.setPrototypeOf
parents e4a25da8 3fb882ac
No related branches found
No related tags found
No related merge requests found
Showing with 246 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: 19.1.2.18
description: Object.setPrototypeOf '`length` property'
info: >
ES6 Section 17:
Every built-in Function object, including constructors, has a length
property whose value is an integer. Unless otherwise specified, this value
is equal to the largest number of named arguments shown in the subclause
headings for the function description, including optional parameters.
[...]
Unless otherwise specified, the length property of a built-in Function
object has the attributes { [[Writable]]: false, [[Enumerable]]: false,
[[Configurable]]: true }.
includes: [propertyHelper.js]
---*/
assert.sameValue(
Object.setPrototypeOf.length,
2,
'The value of `Object.setPrototypeOf.length` is `2`'
);
verifyNotEnumerable(Object.setPrototypeOf, 'length');
verifyNotWritable(Object.setPrototypeOf, 'length');
verifyConfigurable(Object.setPrototypeOf, '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: 19.1.2.18
description: Object.setPrototypeOf '`name` property'
info: >
ES6 Section 17:
Every built-in Function object, including constructors, that is not
identified as an anonymous function has a name property whose value is a
String. Unless otherwise specified, this value is the name that is given to
the function in this specification.
[...]
Unless otherwise specified, the name property of a built-in Function
object, if it exists, has the attributes { [[Writable]]: false,
[[Enumerable]]: false, [[Configurable]]: true }.
includes: [propertyHelper.js]
---*/
assert.sameValue(
Object.setPrototypeOf.name,
'setPrototypeOf',
'The value of `Object.setPrototypeOf.name` is `"setPrototypeOf"`'
);
verifyNotEnumerable(Object.setPrototypeOf, 'name');
verifyNotWritable(Object.setPrototypeOf, 'name');
verifyConfigurable(Object.setPrototypeOf, '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: 19.1.2.18
description: Object.setPrototypeOf invoked with a non-object-coercible value
info: >
1. Let O be RequireObjectCoercible(O).
2. ReturnIfAbrupt(O).
---*/
assert.throws(TypeError, function() {
Object.setPrototypeOf(null);
});
assert.throws(TypeError, function() {
Object.setPrototypeOf(undefined);
});
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 19.1.2.18
description: Object.setPrototypeOf invoked with a non-object value
info: >
1. Let O be RequireObjectCoercible(O).
2. ReturnIfAbrupt(O).
3. If Type(proto) is neither Object nor Null, throw a TypeError exception.
4. If Type(O) is not Object, return O.
features: [Symbol]
---*/
var symbol;
assert.sameValue(Object.setPrototypeOf(true, null), true);
assert.sameValue(Object.setPrototypeOf(3, null), 3);
assert.sameValue(Object.setPrototypeOf('string', null), 'string');
symbol = Symbol('s');
assert.sameValue(Object.setPrototypeOf(symbol, null), symbol);
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 19.1.2.18
description: Object.setPrototypeOf property descriptor
info: >
Every other data property described in clauses 18 through 26 and in Annex
B.2 has the attributes { [[Writable]]: true, [[Enumerable]]: false,
[[Configurable]]: true } unless otherwise specified.
includes: [propertyHelper.js]
---*/
assert.sameValue(typeof Object.setPrototypeOf, 'function');
verifyNotEnumerable(Object, 'setPrototypeOf');
verifyWritable(Object, 'setPrototypeOf');
verifyConfigurable(Object, 'setPrototypeOf');
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 19.1.2.18
description: Object.setPrototypeOf invoked with an invalid prototype value
info: >
1. Let O be RequireObjectCoercible(O).
2. ReturnIfAbrupt(O).
3. If Type(proto) is neither Object nor Null, throw a TypeError exception.
features: [Symbol]
---*/
assert.throws(TypeError, function() {
Object.setPrototypeOf({});
});
assert.throws(TypeError, function() {
Object.setPrototypeOf({}, undefined);
});
assert.throws(TypeError, function() {
Object.setPrototypeOf({}, true);
});
assert.throws(TypeError, function() {
Object.setPrototypeOf({}, 1);
});
assert.throws(TypeError, function() {
Object.setPrototypeOf({}, 'string');
});
assert.throws(TypeError, function() {
Object.setPrototypeOf({}, Symbol('s'));
});
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 19.1.2.18
description: >
Object.setPrototypeOf invoked with an object whose prototype cannot be set
info: >
1. Let O be RequireObjectCoercible(O).
2. ReturnIfAbrupt(O).
3. If Type(proto) is neither Object nor Null, throw a TypeError exception.
4. If Type(O) is not Object, return O.
5. Let status be O.[[SetPrototypeOf]](proto).
6. ReturnIfAbrupt(status).
features: [Proxy]
---*/
var obj = new Proxy({}, {
setPrototypeOf: function() {
throw new Test262Error();
}
});
assert.throws(Test262Error, function() {
Object.setPrototypeOf(obj, null);
});
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 19.1.2.18
description: >
Object.setPrototypeOf invoked with a value that would create a cycle
info: >
1. Let O be RequireObjectCoercible(O).
2. ReturnIfAbrupt(O).
3. If Type(proto) is neither Object nor Null, throw a TypeError exception.
4. If Type(O) is not Object, return O.
5. Let status be O.[[SetPrototypeOf]](proto).
6. ReturnIfAbrupt(status).
7. If status is false, throw a TypeError exception.
---*/
var obj = {};
assert.throws(TypeError, function() {
Object.setPrototypeOf(Object.prototype, Array.prototype);
});
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 19.1.2.18
description: Object.setPrototypeOf invoked with a non-extensible object
info: >
1. Let O be RequireObjectCoercible(O).
2. ReturnIfAbrupt(O).
3. If Type(proto) is neither Object nor Null, throw a TypeError exception.
4. If Type(O) is not Object, return O.
5. Let status be O.[[SetPrototypeOf]](proto).
6. ReturnIfAbrupt(status).
7. If status is false, throw a TypeError exception.
---*/
var obj = {};
Object.preventExtensions(obj);
assert.throws(TypeError, function() {
Object.setPrototypeOf(obj, null);
});
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 19.1.2.18
description: Object.setPrototypeOf invoked with a non-extensible object
info: >
1. Let O be RequireObjectCoercible(O).
2. ReturnIfAbrupt(O).
3. If Type(proto) is neither Object nor Null, throw a TypeError exception.
4. If Type(O) is not Object, return O.
5. Let status be O.[[SetPrototypeOf]](proto).
6. ReturnIfAbrupt(status).
7. If status is false, throw a TypeError exception.
8. Return O.
---*/
var propValue = {};
var newProto = {
test262prop: propValue
};
var obj = {};
var result;
result = Object.setPrototypeOf(obj, newProto);
assert.sameValue(result, obj, 'Return value');
assert.sameValue(Object.hasOwnProperty.call(obj, 'test262prop'), false);
assert.sameValue(obj.test262prop, propValue);
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