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

Add tests for Object.setPrototypeOf
This commit is contained in:
Brian Terlson 2015-06-25 14:46:43 -07:00
commit f06791ea4b
10 changed files with 246 additions and 0 deletions

View File

@ -0,0 +1,29 @@
// 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');

View File

@ -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: 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');

View File

@ -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: 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);
});

View File

@ -0,0 +1,21 @@
// 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);

View File

@ -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: 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');

View File

@ -0,0 +1,35 @@
// 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'));
});

View File

@ -0,0 +1,25 @@
// 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);
});

View File

@ -0,0 +1,21 @@
// 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);
});

View File

@ -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: 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);
});

View File

@ -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: 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);