Add tests for well-known Symbol: @@toPrimitive

Split up test files as per review feedback.
This commit is contained in:
Mike Pennisi 2015-09-12 14:47:38 -04:00
parent 4e88365dc6
commit a63d75c1b6
15 changed files with 399 additions and 252 deletions

View File

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

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

View File

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

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

View File

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

View File

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

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

View File

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

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

View File

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

View File

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

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

View File

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

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

View File

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