diff options
author | Valerie R Young <valerie@bocoup.com> | 2017-10-04 16:14:02 -0400 |
---|---|---|
committer | Rick Waldron <waldron.rick@gmail.com> | 2018-01-25 14:10:40 -0500 |
commit | 232eec1f3bb9ceac404a1734b4d7f2ac8462b800 (patch) | |
tree | 906219a26b620248d3ce27de65ff650a6ac532f7 /test | |
parent | fc86cef984011185a4efa4428aa3e3bf67e2059b (diff) | |
download | qtdeclarative-testsuites-232eec1f3bb9ceac404a1734b4d7f2ac8462b800.tar.gz |
Completely rewrite ToPrimitive method priority testing
Diffstat (limited to 'test')
4 files changed, 259 insertions, 95 deletions
diff --git a/test/built-ins/String/prototype/trimStart/this-value-object-toprimitive-meth-priority.js b/test/built-ins/String/prototype/trimStart/this-value-object-toprimitive-meth-priority.js new file mode 100644 index 000000000..0238f6f0d --- /dev/null +++ b/test/built-ins/String/prototype/trimStart/this-value-object-toprimitive-meth-priority.js @@ -0,0 +1,74 @@ +// Copyright (C) 2017 Valerie Young. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-string.prototype.trimStart +description: > + Priority of Symbol[toPrimitive] when converting object to string for trimming +info: | + Runtime Semantics: TrimString ( string, where ) + 1. Let str be ? RequireObjectCoercible(string). + 2. Let S be ? ToString(str). + ... + + ToString ( argument ) + If argument is Object: + 1. Let primValue be ? ToPrimitive(argument, hint String). + ... + + ToPrimitive ( input [, PreferredType ]) + ... + 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. + ... +features: [string-trimming, Symbol.toPrimitive] +---*/ + + +var toPrimitiveAccessed = 0; +var toStringAccessed = 0; +var valueOfAccessed = 0; +var thisVal = { + get [Symbol.toPrimitive]() { + toPrimitiveAccessed += 1; + return function() { return ' 42'; }; + }, + get toString() { + toStringAccessed += 1; + return function() {return ''; } + }, + get valueOf() { + valueOfAccessed += 1; + return function() {return ''; } + }, +}; + +// Test that thisVal[Symbol.toPrimitive] has been called. + +var result = String.prototype.trimStart.call(thisVal); + +assert.sameValue( + toPrimitiveAccessed, + 1, + 'thisVal[Symbol.toPrimitive] expected to have been accessed.' +); +assert.sameValue( + result, + '42', + 'thisVal[Symbol.toPrimitive] expected to have been called.', +); + +// Test that thisVal.toString and thisVal.valueOf have not been accessedo + +assert.sameValue( + toStringAccessed, + 0, + 'thisVal.toString should not have been accessed.' +); +assert.sameValue( + valueOfAccessed, + 0, + 'thisVal.valueOf should not have been accessed.' +); diff --git a/test/built-ins/String/prototype/trimStart/this-value-object-tostring-meth-priority.js b/test/built-ins/String/prototype/trimStart/this-value-object-tostring-meth-priority.js new file mode 100644 index 000000000..862142a39 --- /dev/null +++ b/test/built-ins/String/prototype/trimStart/this-value-object-tostring-meth-priority.js @@ -0,0 +1,93 @@ +// Copyright (C) 2017 Valerie Young. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-string.prototype.trimStart +description: > + Priority of toString when converting object to string for trimming +info: | + Runtime Semantics: TrimString ( string, where ) + 1. Let str be ? RequireObjectCoercible(string). + 2. Let S be ? ToString(str). + ... + + ToString ( argument ) + If argument 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" ». + ... + 5. For each name in methodNames in List order, do + a. Let method be ? Get(O, name). + b. If IsCallable(method) is true, then + i. Let result be ? Call(method, O). + ii. If Type(result) is not Object, return result. + ... +features: [string-trimming, Symbol.toPrimitive] +---*/ + +var toPrimitiveAccessed = 0; +var toStringAccessed = 0; +var valueOfAccessed = 0; +var thisVal = { + get [Symbol.toPrimitive]() { + toPrimitiveAccessed +=1; + return undefined; + }, + get toString() { + toStringAccessed += 1; + return function() { return ' 42'; }; + }, + get valueOf() { + valueOfAccessed += 1; + return function() { return ''; }; + }, +}; + +// Test that toString is called when Symbol.toPrimitive is undefined. + +var result = String.prototype.trimStart.call(thisVal) + +assert.sameValue( + toPrimitiveAccessed, + 1, + 'thisVal.toString expected to have been accessed.' +); +assert.sameValue( + result, + '42', + 'thisVal.toString expected to have been called.', +); + +// Test that thisVal[toPrimitive] has been accessed. + +assert.sameValue( + toPrimitiveAccessed, + 1, + 'thisVal[Symbol.toPrimitive should have been accessed.' +); + +// Test that thisVal.valueOf has not been accessed. + +assert.sameValue( + valueOfAccessed, + 0, + 'thisVal.valueOf should not have been accessed.' +); diff --git a/test/built-ins/String/prototype/trimStart/this-value-object-valueof-meth-priority.js b/test/built-ins/String/prototype/trimStart/this-value-object-valueof-meth-priority.js new file mode 100644 index 000000000..4b2a61567 --- /dev/null +++ b/test/built-ins/String/prototype/trimStart/this-value-object-valueof-meth-priority.js @@ -0,0 +1,92 @@ +// Copyright (C) 2017 Valerie Young. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-string.prototype.trimStart +description: > + Priority of valueOf when converting object to string for trimming +info: | + Runtime Semantics: TrimString ( string, where ) + 1. Let str be ? RequireObjectCoercible(string). + 2. Let S be ? ToString(str). + ... + + ToString ( argument ) + If argument 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" ». + ... + 5. For each name in methodNames in List order, do + a. Let method be ? Get(O, name). + b. If IsCallable(method) is true, then + i. Let result be ? Call(method, O). + ii. If Type(result) is not Object, return result. + ... +features: [string-trimming, Symbol.toPrimitive] +---*/ + +var toPrimitiveAccessed = 0; +var toStringAccessed = 0; +var valueOfAccessed = 0; +var thisVal = { + get [Symbol.toPrimitive]() { + toPrimitiveAccessed += 1; + return undefined; + }, + get toString() { + toStringAccessed += 1; + return undefined; + }, + get valueOf() { + valueOfAccessed += 1; + return function() { return ' 42' }; + }, +}; + +// Test that valueOf is called when Symbol.toPrimitive and toString are both +// undefined. + +var result = String.prototype.trimStart.call(thisVal); + +assert.sameValue( + valueOfAccessed, + 1, + 'thisVal.toString expected to have been accessed.' +); +assert.sameValue( + result, + '42', + 'thisVal.valueOf expected to have been called.', +); + +// Test that thisVal[toPrimitive] and thisVal.toString has been accessed. + +assert.sameValue( + toPrimitiveAccessed, + 1, + 'thisVal[Symbol.toPrimitive should have been accessed.' +); + +assert.sameValue( + toStringAccessed, + 1, + 'thisVal[Symbol.toString should have been accessed.' +); diff --git a/test/built-ins/String/prototype/trimStart/this-value-toprimitive-meth-priority.js b/test/built-ins/String/prototype/trimStart/this-value-toprimitive-meth-priority.js deleted file mode 100644 index ec9482bb8..000000000 --- a/test/built-ins/String/prototype/trimStart/this-value-toprimitive-meth-priority.js +++ /dev/null @@ -1,95 +0,0 @@ -// 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'); |