-
Notifications
You must be signed in to change notification settings - Fork 472
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ToPrimitive method priority testing
- Loading branch information
Valerie R Young
committed
Oct 3, 2017
1 parent
2fcf45c
commit 6e179ba
Showing
1 changed file
with
95 additions
and
0 deletions.
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
test/built-ins/String/prototype/trimStart/this-value-toprimitive-meth-priority.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// Copyright (C) 2017 the Valerie Young. All rights reserved. | ||
// This code is governed by the BSD license found in the LICENSE file. | ||
|
||
/*--- | ||
esid: sec-String.prototype.trimStart | ||
description: > | ||
ToString perfers Symbol.toPrimitive to toString to valueOf | ||
info: | | ||
Runtime Semantics: TrimString ( string, where ) | ||
1. Let str be ? RequireObjectCoercible(string). | ||
2. Let S be ? ToString(str). | ||
... | ||
ToString ( argument ) | ||
If arguement is Object: | ||
1. Let primValue be ? ToPrimitive(argument, hint String). | ||
... | ||
ToPrimitive ( input [, PreferredType ]) | ||
... | ||
b. Else if PreferredType is hint String, let hint be "string". | ||
... | ||
d. Let exoticToPrim be ? GetMethod(input, @@toPrimitive) | ||
e. If exoticToPrim is not undefined, then | ||
i. Let result be ? Call(exoticToPrim, input, « hint »). | ||
ii. If Type(result) is not Object, return result. | ||
iii. Throw a TypeError exception. | ||
f. If hint is "default", set hint to "number". | ||
g. Return ? OrdinaryToPrimitive(input, hint). | ||
... | ||
OrdinaryToPrimitive( O, hint ) | ||
... | ||
3. If hint is "string", then | ||
a. Let methodNames be « "toString", "valueOf" ». | ||
... | ||
features: [string-trimming, Symbol.toPrimitive] | ||
---*/ | ||
|
||
var trimStart = String.prototype.trimStart; | ||
|
||
var called = 0; | ||
var thisVal = { | ||
get [Symbol.toPrimitive]() { | ||
called += 1; | ||
return function() { return '' }; | ||
}, | ||
toString: function() { | ||
throw new Test262Error( | ||
'this.toString called before this[Symbol.toPrimitive]' | ||
); | ||
}, | ||
valueOf: function() { | ||
throw new Test262Error( | ||
'this.valueOf called before this[Symbol.toPrimitive]' | ||
); | ||
}, | ||
}; | ||
|
||
// Test that thisVal[Symbol.toPrimitive] is called before toString or valueOf | ||
trimStart.call(thisVal); | ||
assert.sameValue(called, 1, '[Symbol.toPrimitive] expected to have been called'); | ||
|
||
var called = 0; | ||
var thisVal = { | ||
[Symbol.toPrimitive]: undefined, | ||
get toString() { | ||
called += 1; | ||
return function() { return '' }; | ||
}, | ||
valueOf: function() { | ||
throw new Test262Error( | ||
'this.valueOf called before this[Symbol.toPrimitive]' | ||
); | ||
}, | ||
}; | ||
|
||
// Test that toString is called before valueOf | ||
trimStart.call(thisVal); | ||
assert.sameValue(called, 1, 'this.toString expected to have been called'); | ||
|
||
var called = 0; | ||
var thisVal = { | ||
[Symbol.toPrimitive]: undefined, | ||
toString: undefined, | ||
get valueOf() { | ||
called += 1; | ||
return function() { return '' }; | ||
}, | ||
}; | ||
|
||
// Test that valueOf is called when neither [Symbol.toPrimitive] nor toString | ||
// are defined. | ||
trimStart.call(thisVal); | ||
assert.sameValue(called, 1, 'this.valueOf expected to have been called'); |