Skip to content
Snippets Groups Projects
Commit 117c3f38 authored by Gorkem Yakin's avatar Gorkem Yakin
Browse files

Merge pull request #418 from bocoup/symbol-to-primitive

Add tests for well-known Symbol: @@toPrimitive
parents df1d78d1 a63d75c1
No related branches found
No related tags found
No related merge requests found
Showing
with 620 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: 20.3.4.45
description: >
Behavior when `hint` is "default" and first try returns an invalid value
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 = 0;
var tsCallCount = 0;
var obj = {
valueOf: function() {
voCallCount += 1;
return 'valueOf test262';
},
toString: function() {
tsCallCount += 1;
return {};
}
};
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');
// 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` is "default" and first try is not callable
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 = 0;
var obj = {
valueOf: function() {
voCallCount += 1;
return 'valueOf test262';
},
toString: null
};
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');
// 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` is "default" and first try returns a valid value
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 = 0;
var tsCallCount = 0;;
var obj = {
get valueOf() {
voAccessCount += 1;
},
toString: function() {
tsCallCount += 1;
return 'toString test262';
}
};
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');
// 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` is "default" and neither first nor second try are callable.
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 obj = {
valueOf: null,
toString: null
};
assert.throws(TypeError, function() {
Date.prototype[Symbol.toPrimitive].call(obj, 'default');
});
// 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'; } });
});
// 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` is "number" and first try returns an invalid value
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 = 0;
var tsCallCount = 0;
var obj = {
valueOf: function() {
voCallCount += 1;
return {};
},
toString: function() {
tsCallCount += 1;
return 'toString test262';
}
};
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');
// 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` is "number" and first try is not callable
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 tsCallCount = 0;
var obj = {
valueOf: null,
toString: function() {
tsCallCount += 1;
return 'toString test262';
}
};
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');
// 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` is "number" and first try returns a valid value
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 = 0;
var tsAccessCount = 0;
var obj = {
valueOf: function() {
voCallCount += 1;
return 'valueOf test262';
},
get toString() {
tsAccessCount += 1;
}
};
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');
// 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` is "number" and neither first nor second try are callable.
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 obj = {
valueOf: null,
toString: null
};
assert.throws(TypeError, function() {
Date.prototype[Symbol.toPrimitive].call(obj, 'number');
});
// 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` is "string" and first try returns an invalid value
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 = 0;
var tsCallCount = 0;
var obj = {
valueOf: function() {
voCallCount += 1;
return 'valueOf test262';
},
toString: function() {
tsCallCount += 1;
return {};
}
};
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');
// 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` is "string" and first try is not callable
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 = 0;
var obj = {
valueOf: function() {
voCallCount += 1;
return 'valueOf test262';
},
toString: null
};
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');
// 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` is "string" and first try returns a valid value
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 = 0;
var tsCallCount = 0;
var obj = {
get valueOf() {
voAccessCount += 1;
},
toString: function() {
tsCallCount += 1;
return 'toString test262';
}
};
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');
// 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` is "string" and neither first nor second try are callable.
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 obj = {
valueOf: null,
toString: null
};
assert.throws(TypeError, function() {
Date.prototype[Symbol.toPrimitive].call(obj, 'string');
});
// 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');
// 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');
// 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);
// 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');
});
// 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');
// 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');
// 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);
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment