mirror of
https://github.com/tc39/test262.git
synced 2025-07-05 13:14:38 +02:00
Add tests for well-known Symbol: @@toPrimitive
This commit is contained in:
parent
9cb89a4e3c
commit
4e88365dc6
84
test/built-ins/Date/prototype/Symbol.toPrimitive/hint-default.js
vendored
Normal file
84
test/built-ins/Date/prototype/Symbol.toPrimitive/hint-default.js
vendored
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
// Copyright (C) 2015 the V8 project authors. All rights reserved.
|
||||||
|
// This code is governed by the BSD license found in the LICENSE file.
|
||||||
|
/*---
|
||||||
|
es6id: 20.3.4.45
|
||||||
|
description: Behavior when `hint` argument is specified as "default"
|
||||||
|
info: >
|
||||||
|
1. Let O be the this value.
|
||||||
|
2. If Type(O) is not Object, throw a TypeError exception.
|
||||||
|
3. If hint is the String value "string" or the String value "default", then
|
||||||
|
a. Let tryFirst be "string".
|
||||||
|
4. Else if hint is the String value "number", then
|
||||||
|
a. Let tryFirst be "number".
|
||||||
|
5. Else, throw a TypeError exception.
|
||||||
|
6. Return OrdinaryToPrimitive(O, tryFirst).
|
||||||
|
features: [Symbol.toPrimitive]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
var voAccessCount, voCallCount, tsCallCount;
|
||||||
|
var obj;
|
||||||
|
|
||||||
|
obj = {
|
||||||
|
get valueOf() {
|
||||||
|
voAccessCount += 1;
|
||||||
|
},
|
||||||
|
toString: function() {
|
||||||
|
tsCallCount += 1;
|
||||||
|
return 'toString test262';
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
voAccessCount = 0;
|
||||||
|
tsCallCount = 0;
|
||||||
|
assert.sameValue(
|
||||||
|
Date.prototype[Symbol.toPrimitive].call(obj, 'default'),
|
||||||
|
'toString test262'
|
||||||
|
);
|
||||||
|
assert.sameValue(voAccessCount, 0, '`valueOf` method was not referenced');
|
||||||
|
assert.sameValue(tsCallCount, 1, '`toString` method was invoked exactly once');
|
||||||
|
|
||||||
|
obj = {
|
||||||
|
valueOf: function() {
|
||||||
|
voCallCount += 1;
|
||||||
|
return 'valueOf test262';
|
||||||
|
},
|
||||||
|
toString: function() {
|
||||||
|
tsCallCount += 1;
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
voCallCount = 0;
|
||||||
|
tsCallCount = 0;
|
||||||
|
assert.sameValue(
|
||||||
|
Date.prototype[Symbol.toPrimitive].call(obj, 'default'),
|
||||||
|
'valueOf test262',
|
||||||
|
'`valueOf` is used as a fallback when `toString` returns an object'
|
||||||
|
);
|
||||||
|
assert.sameValue(voCallCount, 1, '`valueOf` method was invoked exactly once');
|
||||||
|
assert.sameValue(tsCallCount, 1, '`toString` method was invoked exactly once');
|
||||||
|
|
||||||
|
obj = {
|
||||||
|
valueOf: function() {
|
||||||
|
voCallCount += 1;
|
||||||
|
return 'valueOf test262';
|
||||||
|
},
|
||||||
|
toString: null
|
||||||
|
};
|
||||||
|
|
||||||
|
voCallCount = 0;
|
||||||
|
assert.sameValue(
|
||||||
|
Date.prototype[Symbol.toPrimitive].call(obj, 'default'),
|
||||||
|
'valueOf test262',
|
||||||
|
'`valueOf` is used as a fallback when `toString` is not callable'
|
||||||
|
);
|
||||||
|
assert.sameValue(voCallCount, 1, '`valueOf` method was invoked exactly once');
|
||||||
|
|
||||||
|
obj = {
|
||||||
|
valueOf: null,
|
||||||
|
toString: null
|
||||||
|
};
|
||||||
|
|
||||||
|
assert.throws(TypeError, function() {
|
||||||
|
Date.prototype[Symbol.toPrimitive].call(obj, 'default');
|
||||||
|
});
|
51
test/built-ins/Date/prototype/Symbol.toPrimitive/hint-invalid.js
vendored
Normal file
51
test/built-ins/Date/prototype/Symbol.toPrimitive/hint-invalid.js
vendored
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
// Copyright (C) 2015 the V8 project authors. All rights reserved.
|
||||||
|
// This code is governed by the BSD license found in the LICENSE file.
|
||||||
|
/*---
|
||||||
|
es6id: 20.3.4.45
|
||||||
|
description: Behavior when an invalid `hint` argument is specified
|
||||||
|
info: >
|
||||||
|
1. Let O be the this value.
|
||||||
|
2. If Type(O) is not Object, throw a TypeError exception.
|
||||||
|
3. If hint is the String value "string" or the String value "default", then
|
||||||
|
a. Let tryFirst be "string".
|
||||||
|
4. Else if hint is the String value "number", then
|
||||||
|
a. Let tryFirst be "number".
|
||||||
|
5. Else, throw a TypeError exception.
|
||||||
|
features: [Symbol.toPrimitive]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
var d = new Date();
|
||||||
|
|
||||||
|
assert.sameValue(typeof d[Symbol.toPrimitive], 'function');
|
||||||
|
|
||||||
|
assert.throws(TypeError, function() {
|
||||||
|
d[Symbol.toPrimitive]();
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.throws(TypeError, function() {
|
||||||
|
d[Symbol.toPrimitive](undefined);
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.throws(TypeError, function() {
|
||||||
|
d[Symbol.toPrimitive](null);
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.throws(TypeError, function() {
|
||||||
|
d[Symbol.toPrimitive]('');
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.throws(TypeError, function() {
|
||||||
|
d[Symbol.toPrimitive]('String');
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.throws(TypeError, function() {
|
||||||
|
d[Symbol.toPrimitive]('defaultnumber');
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.throws(TypeError, function() {
|
||||||
|
d[Symbol.toPrimitive](new String('number'));
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.throws(TypeError, function() {
|
||||||
|
d[Symbol.toPrimitive]({ toString: function() { 'number'; } });
|
||||||
|
});
|
84
test/built-ins/Date/prototype/Symbol.toPrimitive/hint-number.js
vendored
Normal file
84
test/built-ins/Date/prototype/Symbol.toPrimitive/hint-number.js
vendored
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
// Copyright (C) 2015 the V8 project authors. All rights reserved.
|
||||||
|
// This code is governed by the BSD license found in the LICENSE file.
|
||||||
|
/*---
|
||||||
|
es6id: 20.3.4.45
|
||||||
|
description: Behavior when `hint` argument is specified as "number"
|
||||||
|
info: >
|
||||||
|
1. Let O be the this value.
|
||||||
|
2. If Type(O) is not Object, throw a TypeError exception.
|
||||||
|
3. If hint is the String value "string" or the String value "default", then
|
||||||
|
a. Let tryFirst be "string".
|
||||||
|
4. Else if hint is the String value "number", then
|
||||||
|
a. Let tryFirst be "number".
|
||||||
|
5. Else, throw a TypeError exception.
|
||||||
|
6. Return OrdinaryToPrimitive(O, tryFirst).
|
||||||
|
features: [Symbol.toPrimitive]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
var voCallCount, tsAccessCount, tsCallCount;
|
||||||
|
var obj;
|
||||||
|
|
||||||
|
obj = {
|
||||||
|
valueOf: function() {
|
||||||
|
voCallCount += 1;
|
||||||
|
return 'valueOf test262';
|
||||||
|
},
|
||||||
|
get toString() {
|
||||||
|
tsAccessCount += 1;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
voCallCount = 0;
|
||||||
|
tsAccessCount = 0;
|
||||||
|
assert.sameValue(
|
||||||
|
Date.prototype[Symbol.toPrimitive].call(obj, 'number'),
|
||||||
|
'valueOf test262'
|
||||||
|
);
|
||||||
|
assert.sameValue(voCallCount, 1, '`valueOf` method was invoked exactly once');
|
||||||
|
assert.sameValue(tsAccessCount, 0, '`toString` method was not referenced');
|
||||||
|
|
||||||
|
obj = {
|
||||||
|
valueOf: function() {
|
||||||
|
voCallCount += 1;
|
||||||
|
return {};
|
||||||
|
},
|
||||||
|
toString: function() {
|
||||||
|
tsCallCount += 1;
|
||||||
|
return 'toString test262';
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
voCallCount = 0;
|
||||||
|
tsCallCount = 0;
|
||||||
|
assert.sameValue(
|
||||||
|
Date.prototype[Symbol.toPrimitive].call(obj, 'number'),
|
||||||
|
'toString test262',
|
||||||
|
'`toString` is used as a fallback when `valueOf` returns an object'
|
||||||
|
);
|
||||||
|
assert.sameValue(voCallCount, 1, '`valueOf` method was invoked exactly once');
|
||||||
|
assert.sameValue(tsCallCount, 1, '`toString` method was invoked exactly once');
|
||||||
|
|
||||||
|
obj = {
|
||||||
|
valueOf: null,
|
||||||
|
toString: function() {
|
||||||
|
tsCallCount += 1;
|
||||||
|
return 'toString test262';
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
tsCallCount = 0;
|
||||||
|
assert.sameValue(
|
||||||
|
Date.prototype[Symbol.toPrimitive].call(obj, 'number'),
|
||||||
|
'toString test262',
|
||||||
|
'`toString` is used as a fallback when `valueOf` is not callable'
|
||||||
|
);
|
||||||
|
assert.sameValue(tsCallCount, 1, '`toString` method was invoked exactly once');
|
||||||
|
|
||||||
|
obj = {
|
||||||
|
valueOf: null,
|
||||||
|
toString: null
|
||||||
|
};
|
||||||
|
|
||||||
|
assert.throws(TypeError, function() {
|
||||||
|
Date.prototype[Symbol.toPrimitive].call(obj, 'number');
|
||||||
|
});
|
84
test/built-ins/Date/prototype/Symbol.toPrimitive/hint-string.js
vendored
Normal file
84
test/built-ins/Date/prototype/Symbol.toPrimitive/hint-string.js
vendored
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
// Copyright (C) 2015 the V8 project authors. All rights reserved.
|
||||||
|
// This code is governed by the BSD license found in the LICENSE file.
|
||||||
|
/*---
|
||||||
|
es6id: 20.3.4.45
|
||||||
|
description: Behavior when `hint` argument is specified as "string"
|
||||||
|
info: >
|
||||||
|
1. Let O be the this value.
|
||||||
|
2. If Type(O) is not Object, throw a TypeError exception.
|
||||||
|
3. If hint is the String value "string" or the String value "default", then
|
||||||
|
a. Let tryFirst be "string".
|
||||||
|
4. Else if hint is the String value "number", then
|
||||||
|
a. Let tryFirst be "number".
|
||||||
|
5. Else, throw a TypeError exception.
|
||||||
|
6. Return OrdinaryToPrimitive(O, tryFirst).
|
||||||
|
features: [Symbol.toPrimitive]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
var voAccessCount, voCallCount, tsCallCount;
|
||||||
|
var obj;
|
||||||
|
|
||||||
|
obj = {
|
||||||
|
get valueOf() {
|
||||||
|
voAccessCount += 1;
|
||||||
|
},
|
||||||
|
toString: function() {
|
||||||
|
tsCallCount += 1;
|
||||||
|
return 'toString test262';
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
voAccessCount = 0;
|
||||||
|
tsCallCount = 0;
|
||||||
|
assert.sameValue(
|
||||||
|
Date.prototype[Symbol.toPrimitive].call(obj, 'string'),
|
||||||
|
'toString test262'
|
||||||
|
);
|
||||||
|
assert.sameValue(voAccessCount, 0, '`valueOf` method was not referenced');
|
||||||
|
assert.sameValue(tsCallCount, 1, '`toString` method was invoked exactly once');
|
||||||
|
|
||||||
|
obj = {
|
||||||
|
valueOf: function() {
|
||||||
|
voCallCount += 1;
|
||||||
|
return 'valueOf test262';
|
||||||
|
},
|
||||||
|
toString: function() {
|
||||||
|
tsCallCount += 1;
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
voCallCount = 0;
|
||||||
|
tsCallCount = 0;
|
||||||
|
assert.sameValue(
|
||||||
|
Date.prototype[Symbol.toPrimitive].call(obj, 'string'),
|
||||||
|
'valueOf test262',
|
||||||
|
'`valueOf` is used as a fallback when `toString` returns an object'
|
||||||
|
);
|
||||||
|
assert.sameValue(voCallCount, 1, '`valueOf` method was invoked exactly once');
|
||||||
|
assert.sameValue(tsCallCount, 1, '`toString` method was invoked exactly once');
|
||||||
|
|
||||||
|
obj = {
|
||||||
|
valueOf: function() {
|
||||||
|
voCallCount += 1;
|
||||||
|
return 'valueOf test262';
|
||||||
|
},
|
||||||
|
toString: null
|
||||||
|
};
|
||||||
|
|
||||||
|
voCallCount = 0;
|
||||||
|
assert.sameValue(
|
||||||
|
Date.prototype[Symbol.toPrimitive].call(obj, 'string'),
|
||||||
|
'valueOf test262',
|
||||||
|
'`valueOf` is used as a fallback when `toString` is not callable'
|
||||||
|
);
|
||||||
|
assert.sameValue(voCallCount, 1, '`valueOf` method was invoked exactly once');
|
||||||
|
|
||||||
|
obj = {
|
||||||
|
valueOf: null,
|
||||||
|
toString: null
|
||||||
|
};
|
||||||
|
|
||||||
|
assert.throws(TypeError, function() {
|
||||||
|
Date.prototype[Symbol.toPrimitive].call(obj, 'string');
|
||||||
|
});
|
27
test/built-ins/Date/prototype/Symbol.toPrimitive/length.js
vendored
Normal file
27
test/built-ins/Date/prototype/Symbol.toPrimitive/length.js
vendored
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
// Copyright (C) 2015 the V8 project authors. All rights reserved.
|
||||||
|
// This code is governed by the BSD license found in the LICENSE file.
|
||||||
|
/*---
|
||||||
|
es6id: 20.3.4.45
|
||||||
|
description: Date.prototype[Symbol.toPrimitive] `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 }.
|
||||||
|
features: [Symbol.toPrimitive]
|
||||||
|
includes: [propertyHelper.js]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
assert.sameValue(Date.prototype[Symbol.toPrimitive].length, 1);
|
||||||
|
|
||||||
|
verifyNotEnumerable(Date.prototype[Symbol.toPrimitive], 'length');
|
||||||
|
verifyNotWritable(Date.prototype[Symbol.toPrimitive], 'length');
|
||||||
|
verifyConfigurable(Date.prototype[Symbol.toPrimitive], 'length');
|
26
test/built-ins/Date/prototype/Symbol.toPrimitive/name.js
vendored
Normal file
26
test/built-ins/Date/prototype/Symbol.toPrimitive/name.js
vendored
Normal file
@ -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: 20.3.4.45
|
||||||
|
description: Date.prototype[Symbol.toPrimitive] `name` property
|
||||||
|
info: >
|
||||||
|
The value of the name property of this function is "[Symbol.toPrimitive]".
|
||||||
|
|
||||||
|
ES6 Section 17:
|
||||||
|
|
||||||
|
[...]
|
||||||
|
|
||||||
|
Unless otherwise specified, the name property of a built-in Function
|
||||||
|
object, if it exists, has the attributes { [[Writable]]: false,
|
||||||
|
[[Enumerable]]: false, [[Configurable]]: true }.
|
||||||
|
features: [Symbol.toPrimitive]
|
||||||
|
includes: [propertyHelper.js]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
assert.sameValue(
|
||||||
|
Date.prototype[Symbol.toPrimitive].name, '[Symbol.toPrimitive]'
|
||||||
|
);
|
||||||
|
|
||||||
|
verifyNotEnumerable(Date.prototype[Symbol.toPrimitive], 'name');
|
||||||
|
verifyNotWritable(Date.prototype[Symbol.toPrimitive], 'name');
|
||||||
|
verifyConfigurable(Date.prototype[Symbol.toPrimitive], 'name');
|
16
test/built-ins/Date/prototype/Symbol.toPrimitive/prop-desc.js
vendored
Normal file
16
test/built-ins/Date/prototype/Symbol.toPrimitive/prop-desc.js
vendored
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
// Copyright (C) 2015 the V8 project authors. All rights reserved.
|
||||||
|
// This code is governed by the BSD license found in the LICENSE file.
|
||||||
|
|
||||||
|
/*---
|
||||||
|
es6id: 20.3.4.45
|
||||||
|
description: Date.prototype[Symbol.toPrimitive] property descriptor
|
||||||
|
info: >
|
||||||
|
This property has the attributes { [[Writable]]: false, [[Enumerable]]:
|
||||||
|
false, [[Configurable]]: true }.
|
||||||
|
includes: [propertyHelper.js]
|
||||||
|
features: [Symbol.toPrimitive]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
verifyNotEnumerable(Date.prototype, Symbol.toPrimitive);
|
||||||
|
verifyNotWritable(Date.prototype, Symbol.toPrimitive);
|
||||||
|
verifyConfigurable(Date.prototype, Symbol.toPrimitive);
|
32
test/built-ins/Date/prototype/Symbol.toPrimitive/this-val-non-obj.js
vendored
Normal file
32
test/built-ins/Date/prototype/Symbol.toPrimitive/this-val-non-obj.js
vendored
Normal file
@ -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: 20.3.4.45
|
||||||
|
description: Behavior when `this` value is not an Object
|
||||||
|
info: >
|
||||||
|
1. Let O be the this value.
|
||||||
|
2. If Type(O) is not Object, throw a TypeError exception.
|
||||||
|
features: [Symbol.toPrimitive]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
assert.sameValue(typeof Date.prototype[Symbol.toPrimitive], 'function');
|
||||||
|
|
||||||
|
assert.throws(TypeError, function() {
|
||||||
|
Date.prototype[Symbol.toPrimitive].call(undefined, 'string');
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.throws(TypeError, function() {
|
||||||
|
Date.prototype[Symbol.toPrimitive].call(null, 'string');
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.throws(TypeError, function() {
|
||||||
|
Date.prototype[Symbol.toPrimitive].call(86, 'string');
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.throws(TypeError, function() {
|
||||||
|
Date.prototype[Symbol.toPrimitive].call('', 'string');
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.throws(TypeError, function() {
|
||||||
|
Date.prototype[Symbol.toPrimitive].call(true, 'string');
|
||||||
|
});
|
27
test/built-ins/Symbol/prototype/Symbol.toPrimitive/length.js
vendored
Normal file
27
test/built-ins/Symbol/prototype/Symbol.toPrimitive/length.js
vendored
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
// 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.4.3.4
|
||||||
|
description: Symbol.prototype[Symbol.toPrimitive] `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 }.
|
||||||
|
features: [Symbol.toPrimitive]
|
||||||
|
includes: [propertyHelper.js]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
assert.sameValue(Symbol.prototype[Symbol.toPrimitive].length, 1);
|
||||||
|
|
||||||
|
verifyNotEnumerable(Symbol.prototype[Symbol.toPrimitive], 'length');
|
||||||
|
verifyNotWritable(Symbol.prototype[Symbol.toPrimitive], 'length');
|
||||||
|
verifyConfigurable(Symbol.prototype[Symbol.toPrimitive], 'length');
|
26
test/built-ins/Symbol/prototype/Symbol.toPrimitive/name.js
vendored
Normal file
26
test/built-ins/Symbol/prototype/Symbol.toPrimitive/name.js
vendored
Normal file
@ -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: 19.4.3.4
|
||||||
|
description: Symbol.prototype[Symbol.toPrimitive] `name` property
|
||||||
|
info: >
|
||||||
|
The value of the name property of this function is "[Symbol.toPrimitive]".
|
||||||
|
|
||||||
|
ES6 Section 17:
|
||||||
|
|
||||||
|
[...]
|
||||||
|
|
||||||
|
Unless otherwise specified, the name property of a built-in Function
|
||||||
|
object, if it exists, has the attributes { [[Writable]]: false,
|
||||||
|
[[Enumerable]]: false, [[Configurable]]: true }.
|
||||||
|
features: [Symbol.toPrimitive]
|
||||||
|
includes: [propertyHelper.js]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
assert.sameValue(
|
||||||
|
Symbol.prototype[Symbol.toPrimitive].name, '[Symbol.toPrimitive]'
|
||||||
|
);
|
||||||
|
|
||||||
|
verifyNotEnumerable(Symbol.prototype[Symbol.toPrimitive], 'name');
|
||||||
|
verifyNotWritable(Symbol.prototype[Symbol.toPrimitive], 'name');
|
||||||
|
verifyConfigurable(Symbol.prototype[Symbol.toPrimitive], 'name');
|
16
test/built-ins/Symbol/prototype/Symbol.toPrimitive/prop-desc.js
vendored
Normal file
16
test/built-ins/Symbol/prototype/Symbol.toPrimitive/prop-desc.js
vendored
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
// 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.4.3.4
|
||||||
|
description: Symbol.prototype[Symbol.toPrimitive] property descriptor
|
||||||
|
info: >
|
||||||
|
This property has the attributes { [[Writable]]: false, [[Enumerable]]:
|
||||||
|
false, [[Configurable]]: true }.
|
||||||
|
includes: [propertyHelper.js]
|
||||||
|
features: [Symbol.toPrimitive]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
verifyNotEnumerable(Symbol.prototype, Symbol.toPrimitive);
|
||||||
|
verifyNotWritable(Symbol.prototype, Symbol.toPrimitive);
|
||||||
|
verifyConfigurable(Symbol.prototype, Symbol.toPrimitive);
|
33
test/built-ins/Symbol/prototype/Symbol.toPrimitive/this-val-non-obj.js
vendored
Normal file
33
test/built-ins/Symbol/prototype/Symbol.toPrimitive/this-val-non-obj.js
vendored
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
// 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.4.3.4
|
||||||
|
description: Behavior when `this` value is neither a Symbol nor an Object
|
||||||
|
info: >
|
||||||
|
1. Let s be the this value.
|
||||||
|
2. If Type(s) is Symbol, return s.
|
||||||
|
3. If Type(s) is not Object, throw a TypeError exception.
|
||||||
|
features: [Symbol.toPrimitive]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
assert.sameValue(typeof Symbol.prototype[Symbol.toPrimitive], 'function');
|
||||||
|
|
||||||
|
assert.throws(TypeError, function() {
|
||||||
|
Symbol.prototype[Symbol.toPrimitive].call(undefined);
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.throws(TypeError, function() {
|
||||||
|
Symbol.prototype[Symbol.toPrimitive].call(null);
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.throws(TypeError, function() {
|
||||||
|
Symbol.prototype[Symbol.toPrimitive].call(86);
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.throws(TypeError, function() {
|
||||||
|
Symbol.prototype[Symbol.toPrimitive].call('');
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.throws(TypeError, function() {
|
||||||
|
Symbol.prototype[Symbol.toPrimitive].call(true);
|
||||||
|
});
|
19
test/built-ins/Symbol/prototype/Symbol.toPrimitive/this-val-obj-non-symbol-wrapper.js
vendored
Normal file
19
test/built-ins/Symbol/prototype/Symbol.toPrimitive/this-val-obj-non-symbol-wrapper.js
vendored
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
// 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.4.3.4
|
||||||
|
description: >
|
||||||
|
Behavior when `this` value is an object without a [[SymbolData]] internal
|
||||||
|
slot
|
||||||
|
info: >
|
||||||
|
1. Let s be the this value.
|
||||||
|
2. If Type(s) is Symbol, return s.
|
||||||
|
3. If Type(s) is not Object, throw a TypeError exception.
|
||||||
|
4. If s does not have a [[SymbolData]] internal slot, throw a TypeError
|
||||||
|
exception.
|
||||||
|
features: [Symbol.toPrimitive]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
assert.throws(TypeError, function() {
|
||||||
|
Symbol.prototype[Symbol.toPrimitive].call({});
|
||||||
|
});
|
20
test/built-ins/Symbol/prototype/Symbol.toPrimitive/this-val-obj-symbol-wrapper.js
vendored
Normal file
20
test/built-ins/Symbol/prototype/Symbol.toPrimitive/this-val-obj-symbol-wrapper.js
vendored
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
// 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.4.3.4
|
||||||
|
description: >
|
||||||
|
Behavior when `this` value is an object with a [[SymboldData]] internal
|
||||||
|
slot
|
||||||
|
info: >
|
||||||
|
1. Let s be the this value.
|
||||||
|
2. If Type(s) is Symbol, return s.
|
||||||
|
3. If Type(s) is not Object, throw a TypeError exception.
|
||||||
|
4. If s does not have a [[SymbolData]] internal slot, throw a TypeError
|
||||||
|
exception.
|
||||||
|
5. Return the value of s’s [[SymbolData]] internal slot.
|
||||||
|
features: [Symbol.toPrimitive]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
assert.sameValue(
|
||||||
|
Object(Symbol.toPrimitive)[Symbol.toPrimitive](), Symbol.toPrimitive
|
||||||
|
);
|
12
test/built-ins/Symbol/prototype/Symbol.toPrimitive/this-val-symbol.js
vendored
Normal file
12
test/built-ins/Symbol/prototype/Symbol.toPrimitive/this-val-symbol.js
vendored
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
// 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.4.3.4
|
||||||
|
description: Behavior when `this` value is a Symbol
|
||||||
|
info: >
|
||||||
|
1. Let s be the this value.
|
||||||
|
2. If Type(s) is Symbol, return s.
|
||||||
|
features: [Symbol.toPrimitive]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
assert.sameValue(Symbol.toPrimitive[Symbol.toPrimitive](), Symbol.toPrimitive);
|
17
test/built-ins/Symbol/toPrimitive/prop-desc.js
Normal file
17
test/built-ins/Symbol/toPrimitive/prop-desc.js
Normal 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.4.2.12
|
||||||
|
description: >
|
||||||
|
`Symbol.toPrimitive` property descriptor
|
||||||
|
info: >
|
||||||
|
This property has the attributes { [[Writable]]: false, [[Enumerable]]:
|
||||||
|
false, [[Configurable]]: false }.
|
||||||
|
includes: [propertyHelper.js]
|
||||||
|
features: [Symbol.toPrimitive]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
assert.sameValue(typeof Symbol.toPrimitive, 'symbol');
|
||||||
|
verifyNotEnumerable(Symbol, 'toPrimitive');
|
||||||
|
verifyNotWritable(Symbol, 'toPrimitive');
|
||||||
|
verifyNotConfigurable(Symbol, 'toPrimitive');
|
@ -0,0 +1,37 @@
|
|||||||
|
// Copyright (C) 2015 the V8 project authors. All rights reserved.
|
||||||
|
// This code is governed by the BSD license found in the LICENSE file.
|
||||||
|
/*---
|
||||||
|
es6id: 12.10.3
|
||||||
|
description: >
|
||||||
|
Behavior when error thrown by invocation of `Symbol.toPrimitive` method
|
||||||
|
during coercion
|
||||||
|
info: >
|
||||||
|
[...]
|
||||||
|
7. Return the result of performing Abstract Equality Comparison rval ==
|
||||||
|
lval.
|
||||||
|
|
||||||
|
ES6 Section 7.2.12 Abstract Equality Comparison
|
||||||
|
|
||||||
|
[...]
|
||||||
|
10. If Type(x) is either String, Number, or Symbol and Type(y) is Object,
|
||||||
|
then return the result of the comparison x == ToPrimitive(y).
|
||||||
|
|
||||||
|
ES6 Section 7.1.1 ToPrimitive ( input [, PreferredType] )
|
||||||
|
|
||||||
|
[...]
|
||||||
|
4. Let exoticToPrim be GetMethod(input, @@toPrimitive).
|
||||||
|
5. ReturnIfAbrupt(exoticToPrim).
|
||||||
|
6. If exoticToPrim is not undefined, then
|
||||||
|
a. Let result be Call(exoticToPrim, input, «hint»).
|
||||||
|
b. ReturnIfAbrupt(result).
|
||||||
|
features: [Symbol.toPrimitive]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
var y = {};
|
||||||
|
y[Symbol.toPrimitive] = function() {
|
||||||
|
throw new Test262Error();
|
||||||
|
};
|
||||||
|
|
||||||
|
assert.throws(Test262Error, function() {
|
||||||
|
0 == y;
|
||||||
|
});
|
@ -0,0 +1,48 @@
|
|||||||
|
// Copyright (C) 2015 the V8 project authors. All rights reserved.
|
||||||
|
// This code is governed by the BSD license found in the LICENSE file.
|
||||||
|
/*---
|
||||||
|
es6id: 12.10.3
|
||||||
|
description: Invocation of `Symbol.toPrimitive` method during coercion
|
||||||
|
info: >
|
||||||
|
[...]
|
||||||
|
7. Return the result of performing Abstract Equality Comparison rval ==
|
||||||
|
lval.
|
||||||
|
|
||||||
|
ES6 Section 7.2.12 Abstract Equality Comparison
|
||||||
|
|
||||||
|
[...]
|
||||||
|
10. If Type(x) is either String, Number, or Symbol and Type(y) is Object,
|
||||||
|
then return the result of the comparison x == ToPrimitive(y).
|
||||||
|
|
||||||
|
ES6 Section 7.1.1 ToPrimitive ( input [, PreferredType] )
|
||||||
|
|
||||||
|
1. If PreferredType was not passed, let hint be "default".
|
||||||
|
[...]
|
||||||
|
4. Let exoticToPrim be GetMethod(input, @@toPrimitive).
|
||||||
|
5. ReturnIfAbrupt(exoticToPrim).
|
||||||
|
6. If exoticToPrim is not undefined, then
|
||||||
|
a. Let result be Call(exoticToPrim, input, «hint»).
|
||||||
|
[...]
|
||||||
|
features: [Symbol.toPrimitive]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
var y = {};
|
||||||
|
var callCount = 0;
|
||||||
|
var thisVal, args;
|
||||||
|
|
||||||
|
y[Symbol.toPrimitive] = function() {
|
||||||
|
callCount += 1;
|
||||||
|
thisVal = this;
|
||||||
|
args = arguments;
|
||||||
|
};
|
||||||
|
|
||||||
|
0 == y;
|
||||||
|
|
||||||
|
assert.sameValue(callCount, 1, 'method invoked exactly once');
|
||||||
|
assert.sameValue(thisVal, y, '`this` value is the object being compared');
|
||||||
|
assert.sameValue(args.length, 1, 'method invoked with exactly one argument');
|
||||||
|
assert.sameValue(
|
||||||
|
args[0],
|
||||||
|
'default',
|
||||||
|
'method invoked with the string "default" as the first argument'
|
||||||
|
);
|
@ -0,0 +1,51 @@
|
|||||||
|
// Copyright (C) 2015 the V8 project authors. All rights reserved.
|
||||||
|
// This code is governed by the BSD license found in the LICENSE file.
|
||||||
|
/*---
|
||||||
|
es6id: 12.10.3
|
||||||
|
description: >
|
||||||
|
Behavior when coercion via `Symbol.toPrimitive` yields an Object
|
||||||
|
info: >
|
||||||
|
[...]
|
||||||
|
7. Return the result of performing Abstract Equality Comparison rval ==
|
||||||
|
lval.
|
||||||
|
|
||||||
|
ES6 Section 7.2.12 Abstract Equality Comparison
|
||||||
|
|
||||||
|
[...]
|
||||||
|
10. If Type(x) is either String, Number, or Symbol and Type(y) is Object,
|
||||||
|
then return the result of the comparison x == ToPrimitive(y).
|
||||||
|
|
||||||
|
ES6 Section 7.1.1 ToPrimitive ( input [, PreferredType] )
|
||||||
|
|
||||||
|
[...]
|
||||||
|
4. Let exoticToPrim be GetMethod(input, @@toPrimitive).
|
||||||
|
5. ReturnIfAbrupt(exoticToPrim).
|
||||||
|
6. If exoticToPrim is not undefined, then
|
||||||
|
a. Let result be Call(exoticToPrim, input, «hint»).
|
||||||
|
b. ReturnIfAbrupt(result).
|
||||||
|
c. If Type(result) is not Object, return result.
|
||||||
|
d. Throw a TypeError exception.
|
||||||
|
features: [Symbol.toPrimitive]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
var y = {};
|
||||||
|
var retVal;
|
||||||
|
|
||||||
|
y[Symbol.toPrimitive] = function() {
|
||||||
|
return retVal;
|
||||||
|
};
|
||||||
|
|
||||||
|
retVal = {};
|
||||||
|
assert.throws(TypeError, function() {
|
||||||
|
0 == y;
|
||||||
|
});
|
||||||
|
|
||||||
|
retVal = new Number();
|
||||||
|
assert.throws(TypeError, function() {
|
||||||
|
0 == y;
|
||||||
|
});
|
||||||
|
|
||||||
|
retVal = new String();
|
||||||
|
assert.throws(TypeError, function() {
|
||||||
|
0 == y;
|
||||||
|
});
|
@ -0,0 +1,47 @@
|
|||||||
|
// Copyright (C) 2015 the V8 project authors. All rights reserved.
|
||||||
|
// This code is governed by the BSD license found in the LICENSE file.
|
||||||
|
/*---
|
||||||
|
es6id: 12.10.3
|
||||||
|
description: >
|
||||||
|
Behavior when coercion via `Symbol.toPrimitive` yields a primitive value
|
||||||
|
info: >
|
||||||
|
[...]
|
||||||
|
7. Return the result of performing Abstract Equality Comparison rval ==
|
||||||
|
lval.
|
||||||
|
|
||||||
|
ES6 Section 7.2.12 Abstract Equality Comparison
|
||||||
|
|
||||||
|
[...]
|
||||||
|
10. If Type(x) is either String, Number, or Symbol and Type(y) is Object,
|
||||||
|
then return the result of the comparison x == ToPrimitive(y).
|
||||||
|
|
||||||
|
ES6 Section 7.1.1 ToPrimitive ( input [, PreferredType] )
|
||||||
|
|
||||||
|
[...]
|
||||||
|
4. Let exoticToPrim be GetMethod(input, @@toPrimitive).
|
||||||
|
5. ReturnIfAbrupt(exoticToPrim).
|
||||||
|
6. If exoticToPrim is not undefined, then
|
||||||
|
a. Let result be Call(exoticToPrim, input, «hint»).
|
||||||
|
b. ReturnIfAbrupt(result).
|
||||||
|
c. If Type(result) is not Object, return result.
|
||||||
|
features: [Symbol.toPrimitive]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
var y = {};
|
||||||
|
var retVal;
|
||||||
|
|
||||||
|
y[Symbol.toPrimitive] = function() {
|
||||||
|
return retVal;
|
||||||
|
};
|
||||||
|
|
||||||
|
retVal = 86;
|
||||||
|
assert.sameValue(0 == y, false, 'number primitive (not equal)');
|
||||||
|
assert.sameValue(86 == y, true, 'number primitive (equal)');
|
||||||
|
|
||||||
|
retVal = 'str';
|
||||||
|
assert.sameValue(0 == y, false, 'string primitive (not equal)');
|
||||||
|
assert.sameValue('str' == y, true, 'sting primitive (equal)');
|
||||||
|
|
||||||
|
retVal = Symbol.toPrimitive;
|
||||||
|
assert.sameValue(0 == y, false, 'symbol (not equal)');
|
||||||
|
assert.sameValue(Symbol.toPrimitive == y, true, 'symbol (equal)');
|
34
test/language/expressions/equals/get-symbol-to-prim-err.js
Normal file
34
test/language/expressions/equals/get-symbol-to-prim-err.js
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
// Copyright (C) 2015 the V8 project authors. All rights reserved.
|
||||||
|
// This code is governed by the BSD license found in the LICENSE file.
|
||||||
|
/*---
|
||||||
|
es6id: 12.10.3
|
||||||
|
description: >
|
||||||
|
Behavior when error thrown while accessing `Symbol.toPrimitive` property
|
||||||
|
info: >
|
||||||
|
[...]
|
||||||
|
7. Return the result of performing Abstract Equality Comparison rval ==
|
||||||
|
lval.
|
||||||
|
|
||||||
|
ES6 Section 7.2.12 Abstract Equality Comparison
|
||||||
|
|
||||||
|
[...]
|
||||||
|
10. If Type(x) is either String, Number, or Symbol and Type(y) is Object,
|
||||||
|
then return the result of the comparison x == ToPrimitive(y).
|
||||||
|
|
||||||
|
ES6 Section 7.1.1 ToPrimitive ( input [, PreferredType] )
|
||||||
|
|
||||||
|
[...]
|
||||||
|
4. Let exoticToPrim be GetMethod(input, @@toPrimitive).
|
||||||
|
5. ReturnIfAbrupt(exoticToPrim).
|
||||||
|
features: [Symbol.toPrimitive]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
var y = Object.defineProperty({}, Symbol.toPrimitive, {
|
||||||
|
get: function() {
|
||||||
|
throw new Test262Error();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.throws(Test262Error, function() {
|
||||||
|
0 == y;
|
||||||
|
});
|
Loading…
x
Reference in New Issue
Block a user