diff --git a/features.txt b/features.txt index 328fa2312df..9c58b8a870a 100644 --- a/features.txt +++ b/features.txt @@ -55,11 +55,18 @@ regexp-unicode-property-escapes Atomics SharedArrayBuffer +# String Trimming +# https://github.com/tc39/proposal-string-left-right-trim +# Includes all tests for: +# String.prototype.{trimStart, trimEnd, trimLeft, trimRight } +string-trimming + # Array.prototype.flatten and Array.prototype.flatMap # https://github.com/tc39/proposal-flatMap Array.prototype.flatten Array.prototype.flatMap + # Standard language features # # Language features that have been included in a published version of the diff --git a/test/annexB/built-ins/String/prototype/trimLeft/length.js b/test/annexB/built-ins/String/prototype/trimLeft/length.js new file mode 100644 index 00000000000..7cf7285b709 --- /dev/null +++ b/test/annexB/built-ins/String/prototype/trimLeft/length.js @@ -0,0 +1,31 @@ +// 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.trimLeft +description: > + String.prototype.trimLeft.length is 0. +info: > + String.prototype.trimLeft ( ) + + 17 ECMAScript Standard Built-in Objects: + 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. However, rest parameters shown using the form “...name” + are not included in the default argument count. + + Unless otherwise specified, the length property of a built-in Function + object has the attributes { [[Writable]]: false, [[Enumerable]]: false, + [[Configurable]]: true }. +includes: [propertyHelper.js] +features: [string-trimming] +---*/ + +verifyProperty(String.prototype.trimLeft, "length", { + value: 0, + enumerable: false, + writable: false, + configurable: true, +}); diff --git a/test/annexB/built-ins/String/prototype/trimLeft/name.js b/test/annexB/built-ins/String/prototype/trimLeft/name.js new file mode 100644 index 00000000000..d9509c30865 --- /dev/null +++ b/test/annexB/built-ins/String/prototype/trimLeft/name.js @@ -0,0 +1,28 @@ +// 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.trimLeft +description: > + String.prototype.trimLeft.name is "trimLeft". +info: > + String.prototype.trimLeft ( ) + + 17 ECMAScript Standard Built-in Objects: + Every built-in Function object, including constructors, that is not + identified as an anonymous function has a name property whose value + is a String. + + Unless otherwise specified, the name property of a built-in Function + object, if it exists, has the attributes { [[Writable]]: false, + [[Enumerable]]: false, [[Configurable]]: true }. +includes: [propertyHelper.js] +features: [string-trimming] +---*/ + +verifyProperty(String.prototype.trimLeft, "name", { + value: "trimLeft", + enumerable: false, + writable: false, + configurable: true, +}); diff --git a/test/annexB/built-ins/String/prototype/trimLeft/prop-desc.js b/test/annexB/built-ins/String/prototype/trimLeft/prop-desc.js new file mode 100644 index 00000000000..90f5f355f7a --- /dev/null +++ b/test/annexB/built-ins/String/prototype/trimLeft/prop-desc.js @@ -0,0 +1,22 @@ +// 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.trimLeft +description: > + "trimLeft" property of String.prototype +info: > + 17 ECMAScript Standard Built-in Objects: + + Every other data property described in clauses 18 through 26 and in Annex B.2 + has the attributes { [[Writable]]: true, [[Enumerable]]: false, + [[Configurable]]: true } unless otherwise specified. +includes: [propertyHelper.js] +features: [string-trimming] +---*/ + +verifyProperty(String.prototype, "trimLeft", { + enumerable: false, + writable: true, + configurable: true, +}); diff --git a/test/annexB/built-ins/String/prototype/trimLeft/reference-trimStart.js b/test/annexB/built-ins/String/prototype/trimLeft/reference-trimStart.js new file mode 100644 index 00000000000..bc72d693a57 --- /dev/null +++ b/test/annexB/built-ins/String/prototype/trimLeft/reference-trimStart.js @@ -0,0 +1,17 @@ +// 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.trimLeft +description: > + String.prototype.trimLeft is a reference to String.prototype.trimStart. +info: > + String.prototype.trimLeft ( ) + + The function object that is the initial value of String.prototype.trimLeft + is the same function object that is the initial value of + String.prototype.trimStart. +features: [string-trimming] +---*/ + +assert.sameValue(String.prototype.trimLeft, String.prototype.trimStart); diff --git a/test/annexB/built-ins/String/prototype/trimRight/length.js b/test/annexB/built-ins/String/prototype/trimRight/length.js new file mode 100644 index 00000000000..0108341d7b0 --- /dev/null +++ b/test/annexB/built-ins/String/prototype/trimRight/length.js @@ -0,0 +1,31 @@ +// 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.trimRight +description: > + String.prototype.trimRight.length is 0. +info: > + String.prototype.trimRight ( ) + + 17 ECMAScript Standard Built-in Objects: + 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. However, rest parameters shown using the form “...name” + are not included in the default argument count. + + Unless otherwise specified, the length property of a built-in Function + object has the attributes { [[Writable]]: false, [[Enumerable]]: false, + [[Configurable]]: true }. +includes: [propertyHelper.js] +features: [string-trimming] +---*/ + +verifyProperty(String.prototype.trimRight, "length", { + value: 0, + enumerable: false, + writable: false, + configurable: true, +}); diff --git a/test/annexB/built-ins/String/prototype/trimRight/name.js b/test/annexB/built-ins/String/prototype/trimRight/name.js new file mode 100644 index 00000000000..14f6b494fc7 --- /dev/null +++ b/test/annexB/built-ins/String/prototype/trimRight/name.js @@ -0,0 +1,28 @@ +// 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.trimRight +description: > + String.prototype.trimRight.name is "trimRight". +info: > + String.prototype.trimRight ( ) + + 17 ECMAScript Standard Built-in Objects: + Every built-in Function object, including constructors, that is not + identified as an anonymous function has a name property whose value + is a String. + + Unless otherwise specified, the name property of a built-in Function + object, if it exists, has the attributes { [[Writable]]: false, + [[Enumerable]]: false, [[Configurable]]: true }. +includes: [propertyHelper.js] +features: [string-trimming] +---*/ + +verifyProperty(String.prototype.trimRight, "name", { + value: "trimRight", + enumerable: false, + writable: false, + configurable: true, +}); diff --git a/test/annexB/built-ins/String/prototype/trimRight/prop-desc.js b/test/annexB/built-ins/String/prototype/trimRight/prop-desc.js new file mode 100644 index 00000000000..9aad54d47de --- /dev/null +++ b/test/annexB/built-ins/String/prototype/trimRight/prop-desc.js @@ -0,0 +1,22 @@ +// 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.trimRight +description: > + "trimRight" property of String.prototype +info: > + 17 ECMAScript Standard Built-in Objects: + + Every other data property described in clauses 18 through 26 and in Annex B.2 + has the attributes { [[Writable]]: true, [[Enumerable]]: false, + [[Configurable]]: true } unless otherwise specified. +includes: [propertyHelper.js] +features: [string-trimming] +---*/ + +verifyProperty(String.prototype, "trimRight", { + enumerable: false, + writable: true, + configurable: true, +}); diff --git a/test/annexB/built-ins/String/prototype/trimRight/reference-trimEnd.js b/test/annexB/built-ins/String/prototype/trimRight/reference-trimEnd.js new file mode 100644 index 00000000000..515e8f60d47 --- /dev/null +++ b/test/annexB/built-ins/String/prototype/trimRight/reference-trimEnd.js @@ -0,0 +1,17 @@ +// 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.trimRight +description: > + String.prototype.trimRight is a reference to String.prototype.trimEnd. +info: > + String.prototype.trimRight ( ) + + The function object that is the initial value of String.prototype.trimRight + is the same function object that is the initial value of + String.prototype.trimEnd. +features: [string-trimming] +---*/ + +assert.sameValue(String.prototype.trimRight, String.prototype.trimEnd); diff --git a/test/built-ins/String/prototype/trimEnd/length.js b/test/built-ins/String/prototype/trimEnd/length.js new file mode 100644 index 00000000000..37c9fd8b826 --- /dev/null +++ b/test/built-ins/String/prototype/trimEnd/length.js @@ -0,0 +1,31 @@ +// 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.trimEnd +description: > + String.prototype.trimEnd.length is 0. +info: > + String.prototype.trimEnd ( ) + + 17 ECMAScript Standard Built-in Objects: + 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. However, rest parameters shown using the form “...name” + are not included in the default argument count. + + Unless otherwise specified, the length property of a built-in Function + object has the attributes { [[Writable]]: false, [[Enumerable]]: false, + [[Configurable]]: true }. +includes: [propertyHelper.js] +features: [string-trimming] +---*/ + +verifyProperty(String.prototype.trimEnd, "length", { + value: 0, + enumerable: false, + writable: false, + configurable: true, +}); diff --git a/test/built-ins/String/prototype/trimEnd/name.js b/test/built-ins/String/prototype/trimEnd/name.js new file mode 100644 index 00000000000..6a0dc7aeccd --- /dev/null +++ b/test/built-ins/String/prototype/trimEnd/name.js @@ -0,0 +1,28 @@ +// 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.trimEnd +description: > + String.prototype.trimEnd.name is "trimEnd". +info: > + String.prototype.trimEnd ( ) + + 17 ECMAScript Standard Built-in Objects: + Every built-in Function object, including constructors, that is not + identified as an anonymous function has a name property whose value + is a String. + + Unless otherwise specified, the name property of a built-in Function + object, if it exists, has the attributes { [[Writable]]: false, + [[Enumerable]]: false, [[Configurable]]: true }. +includes: [propertyHelper.js] +features: [string-trimming] +---*/ + +verifyProperty(String.prototype.trimEnd, "name", { + value: "trimEnd", + enumerable: false, + writable: false, + configurable: true, +}); diff --git a/test/built-ins/String/prototype/trimEnd/prop-desc.js b/test/built-ins/String/prototype/trimEnd/prop-desc.js new file mode 100644 index 00000000000..990e201b481 --- /dev/null +++ b/test/built-ins/String/prototype/trimEnd/prop-desc.js @@ -0,0 +1,22 @@ +// 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.trimEnd +description: > + "trimEnd" property of String.prototype +info: > + 17 ECMAScript Standard Built-in Objects: + + Every other data property described in clauses 18 through 26 and in Annex B.2 + has the attributes { [[Writable]]: true, [[Enumerable]]: false, + [[Configurable]]: true } unless otherwise specified. +includes: [propertyHelper.js] +features: [string-trimming] +---*/ + +verifyProperty(String.prototype, "trimEnd", { + enumerable: false, + writable: true, + configurable: true, +}); diff --git a/test/built-ins/String/prototype/trimEnd/this-value-boolean.js b/test/built-ins/String/prototype/trimEnd/this-value-boolean.js new file mode 100644 index 00000000000..948862f56ee --- /dev/null +++ b/test/built-ins/String/prototype/trimEnd/this-value-boolean.js @@ -0,0 +1,31 @@ +// 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.trimEnd +description: Behavior when "this" value is a boolean. +info: | + Runtime Semantics: TrimString ( string, where ) + 2. Let S be ? ToString(str). + + ToString ( argument ) + Argument Type: Boolean + Result: + If argument is true, return "true". + If argument is false, return "false". +features: [string-trimming] +---*/ + +var trimEnd = String.prototype.trimEnd + +assert.sameValue( + trimEnd.call(true), + 'true', + 'String.prototype.trimEnd.call(true)' +); + +assert.sameValue( + String.prototype.trimEnd.call(false), + 'false', + 'String.prototype.trimEnd.call(false)' +); diff --git a/test/built-ins/String/prototype/trimEnd/this-value-line-terminator.js b/test/built-ins/String/prototype/trimEnd/this-value-line-terminator.js new file mode 100644 index 00000000000..3e4eea802e4 --- /dev/null +++ b/test/built-ins/String/prototype/trimEnd/this-value-line-terminator.js @@ -0,0 +1,30 @@ +// 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.trimEnd +description: TrimEnd removes all line terminators from the end of a string. +info: | + Runtime Symantics: TrimString ( string, where ) + ... + 4. Else if where is "end", let T be a String value that is a copy of S with + trailing white space removed. + ... + + The definition of white space is the union of WhiteSpace and LineTerminator. + +features: [string-trimming] +---*/ + +var trimEnd = String.prototype.trimEnd; + +// A string of all valid LineTerminator Unicode code points +var lt = '\u000A\u000D\u2028\u2029'; + +var str = lt + 'a' + lt + 'b' + lt; +var expected = lt + 'a' + lt + 'b'; + +assert.sameValue( + trimEnd.call(str), + expected, +); diff --git a/test/built-ins/String/prototype/trimEnd/this-value-not-obj-coercible.js b/test/built-ins/String/prototype/trimEnd/this-value-not-obj-coercible.js new file mode 100644 index 00000000000..8f33e2de837 --- /dev/null +++ b/test/built-ins/String/prototype/trimEnd/this-value-not-obj-coercible.js @@ -0,0 +1,22 @@ +// 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.trimEnd +description: The "this" value must be object-coercible +info: | + 1. Let O be ? RequireObjectCoercible(this value). +features: [string-trimming] +---*/ + +var trimEnd = String.prototype.trimEnd; + +assert.sameValue(typeof trimEnd, 'function'); + +assert.throws(TypeError, function() { + trimEnd.call(undefined); +}, 'undefined'); + +assert.throws(TypeError, function() { + trimEnd.call(null); +}, 'null'); diff --git a/test/built-ins/String/prototype/trimEnd/this-value-number.js b/test/built-ins/String/prototype/trimEnd/this-value-number.js new file mode 100644 index 00000000000..dc57daecfa8 --- /dev/null +++ b/test/built-ins/String/prototype/trimEnd/this-value-number.js @@ -0,0 +1,48 @@ +// 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.trimEnd +description: Behavoir when "this" value is a number. +info: | + Runtime Semantics: TrimString ( string, where ) + 2. Let S be ? ToString(str). + + ToString ( argument ) + Argument Type: Number + Result: NumberToString(argument) +features: [string-trimming] +---*/ + +var trimEnd = String.prototype.trimEnd + +assert.sameValue( + trimEnd.call(NaN), + 'NaN', + 'String.prototype.trimEnd.call(NaN)' +); + +assert.sameValue( + trimEnd.call(Infinity), + 'Infinity', + 'String.prototype.trimEnd.call(Infinity)' +); + +assert.sameValue( + trimEnd.call(-0), + '0', + 'String.prototype.trimEnd.call(-0)' +); + +assert.sameValue( + trimEnd.call(1), + '1', + 'String.prototype.trimEnd.call(1)' +); + +assert.sameValue( + trimEnd.call(-1), + '-1', + 'String.prototype.trimEnd.call(-1)' +); + diff --git a/test/built-ins/String/prototype/trimEnd/this-value-object-cannot-convert-to-primitive-err.js b/test/built-ins/String/prototype/trimEnd/this-value-object-cannot-convert-to-primitive-err.js new file mode 100644 index 00000000000..4aebe845859 --- /dev/null +++ b/test/built-ins/String/prototype/trimEnd/this-value-object-cannot-convert-to-primitive-err.js @@ -0,0 +1,57 @@ +// 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.trimEnd +description: > + This value is an object which cannot be converted to a primitive +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. + 6. Throw a TypeError exception. +features: [string-trimming, Symbol.toPrimitive] +---*/ + +var thisVal = { + [Symbol.toPrimitive]: undefined, + toString: undefined, + valueOf: undefined, +}; + +// If trimEnd is called on an object with neither Symbol.toPrimitive, toString +// nor valueOf defined, then a TypeError exception should be thrown. +assert.throws( + TypeError, + function() { String.prototype.trimEnd.call(thisVal); }, +); diff --git a/test/built-ins/String/prototype/trimEnd/this-value-object-toprimitive-call-err.js b/test/built-ins/String/prototype/trimEnd/this-value-object-toprimitive-call-err.js new file mode 100644 index 00000000000..7ef1a2c1fbc --- /dev/null +++ b/test/built-ins/String/prototype/trimEnd/this-value-object-toprimitive-call-err.js @@ -0,0 +1,34 @@ +// 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.trimEnd +description: > + Abrupt completion when getting Symbol.toPrimitive method +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). + ... +features: [string-trimming, Symbol.toPrimitive] +---*/ + +var thisVal = { + get [Symbol.toPrimitive]() { + throw new Test262Error(); + }, +}; + +assert.throws(Test262Error, function() { + String.prototype.trimEnd.call(thisVal); +}); diff --git a/test/built-ins/String/prototype/trimEnd/this-value-object-toprimitive-meth-err.js b/test/built-ins/String/prototype/trimEnd/this-value-object-toprimitive-meth-err.js new file mode 100644 index 00000000000..f0bb5bf48be --- /dev/null +++ b/test/built-ins/String/prototype/trimEnd/this-value-object-toprimitive-meth-err.js @@ -0,0 +1,36 @@ +// 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.trimEnd +description: > + Abrupt completion when Symbol.toPrimitive abrupt completes. +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 »). + ... +features: [string-trimming, Symbol.toPrimitive] +---*/ + +var thisVal = { + [Symbol.toPrimitive]: function() { + throw new Test262Error(); + }, +}; + +assert.throws(Test262Error, function() { + String.prototype.trimEnd.call(thisVal); +}); diff --git a/test/built-ins/String/prototype/trimEnd/this-value-object-toprimitive-meth-priority.js b/test/built-ins/String/prototype/trimEnd/this-value-object-toprimitive-meth-priority.js new file mode 100644 index 00000000000..df52f8a2481 --- /dev/null +++ b/test/built-ins/String/prototype/trimEnd/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.trimEnd +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.trimEnd.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/trimEnd/this-value-object-toprimitive-returns-object-err.js b/test/built-ins/String/prototype/trimEnd/this-value-object-toprimitive-returns-object-err.js new file mode 100644 index 00000000000..b67fe117a45 --- /dev/null +++ b/test/built-ins/String/prototype/trimEnd/this-value-object-toprimitive-returns-object-err.js @@ -0,0 +1,38 @@ +// 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.trimEnd +description: > + Abrupt completion when Symbol.toPrimitive returns an object +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 ]) + ... + 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. + ... +features: [string-trimming, Symbol.toPrimitive] +---*/ + +var thisVal = { + [Symbol.toPrimitive]: function() { + return {}; + }, +}; + +assert.throws(TypeError, function() { + String.prototype.trimEnd.call(thisVal); +}); diff --git a/test/built-ins/String/prototype/trimEnd/this-value-object-tostring-call-err.js b/test/built-ins/String/prototype/trimEnd/this-value-object-tostring-call-err.js new file mode 100644 index 00000000000..f7ed44d1011 --- /dev/null +++ b/test/built-ins/String/prototype/trimEnd/this-value-object-tostring-call-err.js @@ -0,0 +1,51 @@ +// 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.trimEnd +description: > + Abrupt completion when getting toString method +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). +features: [string-trimming, Symbol.toPrimitive] +---*/ + +var thisVal = { + [Symbol.toPrimitive]: undefined, + get toString() { + throw new Test262Error(); + }, +}; + +assert.throws(Test262Error, function() { + String.prototype.trimEnd.call(thisVal); +}); diff --git a/test/built-ins/String/prototype/trimEnd/this-value-object-tostring-meth-err.js b/test/built-ins/String/prototype/trimEnd/this-value-object-tostring-meth-err.js new file mode 100644 index 00000000000..40c2b9bcce2 --- /dev/null +++ b/test/built-ins/String/prototype/trimEnd/this-value-object-tostring-meth-err.js @@ -0,0 +1,53 @@ +// 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.trimEnd +description: > + Abrupt completion when toString called and abrupt completes. +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). +features: [string-trimming, Symbol.toPrimitive] +---*/ + +var thisVal = { + [Symbol.toPrimitive]: undefined, + toString: function() { + throw new Test262Error(); + }, +}; + +assert.throws(Test262Error, function() { + String.prototype.trimEnd.call(thisVal); +}); diff --git a/test/built-ins/String/prototype/trimEnd/this-value-object-tostring-meth-priority.js b/test/built-ins/String/prototype/trimEnd/this-value-object-tostring-meth-priority.js new file mode 100644 index 00000000000..06d82538fe8 --- /dev/null +++ b/test/built-ins/String/prototype/trimEnd/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.trimEnd +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.trimEnd.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/trimEnd/this-value-object-tostring-returns-object-err.js b/test/built-ins/String/prototype/trimEnd/this-value-object-tostring-returns-object-err.js new file mode 100644 index 00000000000..e47539fadf2 --- /dev/null +++ b/test/built-ins/String/prototype/trimEnd/this-value-object-tostring-returns-object-err.js @@ -0,0 +1,56 @@ +// 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.trimEnd +description: > + Abrupt completion when toString called and returns an object +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. + 6. Throw a TypeError exception. +features: [string-trimming, Symbol.toPrimitive] +---*/ + + +var thisVal = { + [Symbol.toPrimitive]: undefined, + toString: function() { + return {}; + }, +}; + +assert.throws(TypeError, function() { + String.prototype.trimEnd.call(thisVal); +}); diff --git a/test/built-ins/String/prototype/trimEnd/this-value-object-valueof-call-err.js b/test/built-ins/String/prototype/trimEnd/this-value-object-valueof-call-err.js new file mode 100644 index 00000000000..ccd45167452 --- /dev/null +++ b/test/built-ins/String/prototype/trimEnd/this-value-object-valueof-call-err.js @@ -0,0 +1,52 @@ +// 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.trimEnd +description: > + Abrupt completion when getting valueOf method +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). +features: [string-trimming, Symbol.toPrimitive] +---*/ + +var thisVal = { + [Symbol.toPrimitive]: undefined, + toString: undefined, + get valueOf() { + throw new Test262Error(); + }, +}; + +assert.throws(Test262Error, function() { + String.prototype.trimEnd.call(thisVal); +}); diff --git a/test/built-ins/String/prototype/trimEnd/this-value-object-valueof-meth-err.js b/test/built-ins/String/prototype/trimEnd/this-value-object-valueof-meth-err.js new file mode 100644 index 00000000000..386398aebbd --- /dev/null +++ b/test/built-ins/String/prototype/trimEnd/this-value-object-valueof-meth-err.js @@ -0,0 +1,54 @@ +// 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.trimEnd +description: > + Abrupt completion when valueOf called and abrupt completes. +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). +features: [string-trimming, Symbol.toPrimitive] +---*/ + +var thisVal = { + [Symbol.toPrimitive]: undefined, + toString: undefined, + valueOf: function() { + throw new Test262Error(); + }, +}; + +assert.throws(Test262Error, function() { + String.prototype.trimEnd.call(thisVal); +}); diff --git a/test/built-ins/String/prototype/trimEnd/this-value-object-valueof-meth-priority.js b/test/built-ins/String/prototype/trimEnd/this-value-object-valueof-meth-priority.js new file mode 100644 index 00000000000..778846c0791 --- /dev/null +++ b/test/built-ins/String/prototype/trimEnd/this-value-object-valueof-meth-priority.js @@ -0,0 +1,91 @@ +// 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.trimEnd +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.trimEnd.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/trimEnd/this-value-object-valueof-returns-object-err.js b/test/built-ins/String/prototype/trimEnd/this-value-object-valueof-returns-object-err.js new file mode 100644 index 00000000000..c1666660749 --- /dev/null +++ b/test/built-ins/String/prototype/trimEnd/this-value-object-valueof-returns-object-err.js @@ -0,0 +1,57 @@ +// 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.trimEnd +description: > + Abrupt completion when valueOf called and returns an object +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. + 6. Throw a TypeError exception. +features: [string-trimming, Symbol.toPrimitive] +---*/ + + +var thisVal = { + [Symbol.toPrimitive]: undefined, + toString: undefined, + valueOf: function() { + return {}; + }, +}; + +assert.throws(TypeError, function() { + String.prototype.trimEnd.call(thisVal); +}); diff --git a/test/built-ins/String/prototype/trimEnd/this-value-symbol-typeerror.js b/test/built-ins/String/prototype/trimEnd/this-value-symbol-typeerror.js new file mode 100644 index 00000000000..c12f2ebf306 --- /dev/null +++ b/test/built-ins/String/prototype/trimEnd/this-value-symbol-typeerror.js @@ -0,0 +1,24 @@ +// 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.trimEnd +description: Type error when "this" value is a Symbol +info: | + Runtime Semantics: TrimString ( string, where ) + 2. Let S be ? ToString(str). + + ToString ( argument ) + Argument Type: Symbol + Result: Throw a TypeError exception +features: [string-trimming] +---*/ + +var trimEnd = String.prototype.trimEnd; +var symbol = Symbol(); + +assert.throws( + TypeError, + function() { trimEnd.call(symbol); }, + 'String.prototype.trimEnd.call(Symbol())' +); diff --git a/test/built-ins/String/prototype/trimEnd/this-value-whitespace.js b/test/built-ins/String/prototype/trimEnd/this-value-whitespace.js new file mode 100644 index 00000000000..6889a5bbf88 --- /dev/null +++ b/test/built-ins/String/prototype/trimEnd/this-value-whitespace.js @@ -0,0 +1,33 @@ +// 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.trimEnd +description: TrimEnd removes all whitespace from the end of a string. +info: | + Runtime Symantics: TrimString ( string, where ) + ... + 3. Else if where is "end", let T be a String value that is a copy of S with + trailing white space removed. + ... + + The definition of white space is the union of WhiteSpace and LineTerminator. + When determining whether a Unicode code point is in Unicode general category + “Zs”, code unit sequences are interpreted as UTF-16 encoded code point + sequences as specified in 6.1.4. + +features: [string-trimming] +---*/ + +var trimEnd = String.prototype.trimEnd; + +// A string of all valid WhiteSpace Unicode code points +var wspc = '\u0009\u000B\u000C\u0020\u00A0\FEFF\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000'; + +var str = wspc + 'a' + wspc + 'b' + wspc; +var expected = wspc + 'a' + wspc + 'b'; + +assert.sameValue( + trimEnd.call(str), + expected, +); diff --git a/test/built-ins/String/prototype/trimStart/length.js b/test/built-ins/String/prototype/trimStart/length.js new file mode 100644 index 00000000000..43373b2cdc2 --- /dev/null +++ b/test/built-ins/String/prototype/trimStart/length.js @@ -0,0 +1,31 @@ +// 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: > + String.prototype.trimStart.length is 0. +info: > + String.prototype.trimStart ( ) + + 17 ECMAScript Standard Built-in Objects: + 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. However, rest parameters shown using the form “...name” + are not included in the default argument count. + + Unless otherwise specified, the length property of a built-in Function + object has the attributes { [[Writable]]: false, [[Enumerable]]: false, + [[Configurable]]: true }. +includes: [propertyHelper.js] +features: [string-trimming] +---*/ + +verifyProperty(String.prototype.trimStart, "length", { + value: 0, + enumerable: false, + writable: false, + configurable: true, +}); diff --git a/test/built-ins/String/prototype/trimStart/name.js b/test/built-ins/String/prototype/trimStart/name.js new file mode 100644 index 00000000000..60a3442af87 --- /dev/null +++ b/test/built-ins/String/prototype/trimStart/name.js @@ -0,0 +1,28 @@ +// 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: > + String.prototype.trimStart.name is "trimStart". +info: > + String.prototype.trimStart ( ) + + 17 ECMAScript Standard Built-in Objects: + Every built-in Function object, including constructors, that is not + identified as an anonymous function has a name property whose value + is a String. + + Unless otherwise specified, the name property of a built-in Function + object, if it exists, has the attributes { [[Writable]]: false, + [[Enumerable]]: false, [[Configurable]]: true }. +includes: [propertyHelper.js] +features: [string-trimming] +---*/ + +verifyProperty(String.prototype.trimStart, "name", { + value: "trimStart", + enumerable: false, + writable: false, + configurable: true, +}); diff --git a/test/built-ins/String/prototype/trimStart/prop-desc.js b/test/built-ins/String/prototype/trimStart/prop-desc.js new file mode 100644 index 00000000000..a7f55ab691f --- /dev/null +++ b/test/built-ins/String/prototype/trimStart/prop-desc.js @@ -0,0 +1,22 @@ +// 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: > + "trimStart" property of String.prototype +info: > + 17 ECMAScript Standard Built-in Objects: + + Every other data property described in clauses 18 through 26 and in Annex B.2 + has the attributes { [[Writable]]: true, [[Enumerable]]: false, + [[Configurable]]: true } unless otherwise specified. +includes: [propertyHelper.js] +features: [string-trimming] +---*/ + +verifyProperty(String.prototype, "trimStart", { + enumerable: false, + writable: true, + configurable: true, +}); diff --git a/test/built-ins/String/prototype/trimStart/this-value-boolean.js b/test/built-ins/String/prototype/trimStart/this-value-boolean.js new file mode 100644 index 00000000000..b682cb49a75 --- /dev/null +++ b/test/built-ins/String/prototype/trimStart/this-value-boolean.js @@ -0,0 +1,31 @@ +// 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: Behavior when "this" value is a boolean. +info: | + Runtime Semantics: TrimString ( string, where ) + 2. Let S be ? ToString(str). + + ToString ( argument ) + Argument Type: Boolean + Result: + If argument is true, return "true". + If argument is false, return "false". +features: [string-trimming] +---*/ + +var trimStart = String.prototype.trimStart + +assert.sameValue( + trimStart.call(true), + 'true', + 'String.prototype.trimStart.call(true)' +); + +assert.sameValue( + String.prototype.trimStart.call(false), + 'false', + 'String.prototype.trimStart.call(false)' +); diff --git a/test/built-ins/String/prototype/trimStart/this-value-line-terminator.js b/test/built-ins/String/prototype/trimStart/this-value-line-terminator.js new file mode 100644 index 00000000000..743e714a2a7 --- /dev/null +++ b/test/built-ins/String/prototype/trimStart/this-value-line-terminator.js @@ -0,0 +1,30 @@ +// 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: TrimStart removes all line terminators from the start of a string. +info: | + Runtime Symantics: TrimString ( string, where ) + ... + 4. If where is "start", let T be a String value that is a copy of S with + trailing white space removed. + ... + + The definition of white space is the union of WhiteSpace and LineTerminator. + +features: [string-trimming] +---*/ + +var trimStart = String.prototype.trimStart; + +// A string of all valid LineTerminator Unicode code points +var lt = '\u000A\u000D\u2028\u2029'; + +var str = lt + 'a' + lt + 'b' + lt; +var expected = 'a' + lt + 'b' + lt; + +assert.sameValue( + trimStart.call(str), + expected, +); diff --git a/test/built-ins/String/prototype/trimStart/this-value-not-obj-coercible.js b/test/built-ins/String/prototype/trimStart/this-value-not-obj-coercible.js new file mode 100644 index 00000000000..f97805039e4 --- /dev/null +++ b/test/built-ins/String/prototype/trimStart/this-value-not-obj-coercible.js @@ -0,0 +1,22 @@ +// 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: The "this" value must be object-coercible +info: | + 1. Let O be ? RequireObjectCoercible(this value). +features: [string-trimming] +---*/ + +var trimStart = String.prototype.trimStart; + +assert.sameValue(typeof trimStart, 'function'); + +assert.throws(TypeError, function() { + trimStart.call(undefined); +}, 'undefined'); + +assert.throws(TypeError, function() { + trimStart.call(null); +}, 'null'); diff --git a/test/built-ins/String/prototype/trimStart/this-value-number.js b/test/built-ins/String/prototype/trimStart/this-value-number.js new file mode 100644 index 00000000000..7368d5d0379 --- /dev/null +++ b/test/built-ins/String/prototype/trimStart/this-value-number.js @@ -0,0 +1,48 @@ +// 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: Behavoir when "this" value is a number. +info: | + Runtime Semantics: TrimString ( string, where ) + 2. Let S be ? ToString(str). + + ToString ( argument ) + Argument Type: Number + Result: NumberToString(argument) +features: [string-trimming] +---*/ + +var trimStart = String.prototype.trimStart + +assert.sameValue( + trimStart.call(NaN), + 'NaN', + 'String.prototype.trimStart.call(NaN)' +); + +assert.sameValue( + trimStart.call(Infinity), + 'Infinity', + 'String.prototype.trimStart.call(Infinity)' +); + +assert.sameValue( + trimStart.call(-0), + '0', + 'String.prototype.trimStart.call(-0)' +); + +assert.sameValue( + trimStart.call(1), + '1', + 'String.prototype.trimStart.call(1)' +); + +assert.sameValue( + trimStart.call(-1), + '-1', + 'String.prototype.trimStart.call(-1)' +); + diff --git a/test/built-ins/String/prototype/trimStart/this-value-object-cannot-convert-to-primitive-err.js b/test/built-ins/String/prototype/trimStart/this-value-object-cannot-convert-to-primitive-err.js new file mode 100644 index 00000000000..286fb6908b1 --- /dev/null +++ b/test/built-ins/String/prototype/trimStart/this-value-object-cannot-convert-to-primitive-err.js @@ -0,0 +1,57 @@ +// 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: > + This value is an object which cannot be converted to a primitive +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. + 6. Throw a TypeError exception. +features: [string-trimming, Symbol.toPrimitive] +---*/ + +var thisVal = { + [Symbol.toPrimitive]: undefined, + toString: undefined, + valueOf: undefined, +}; + +// If trimStart is called on an object with neither Symbol.toPrimitive, toString +// nor valueOf defined, then a TypeError exception should be thrown. +assert.throws( + TypeError, + function() { String.prototype.trimStart.call(thisVal); }, +); diff --git a/test/built-ins/String/prototype/trimStart/this-value-object-toprimitive-call-err.js b/test/built-ins/String/prototype/trimStart/this-value-object-toprimitive-call-err.js new file mode 100644 index 00000000000..85d1dded5f5 --- /dev/null +++ b/test/built-ins/String/prototype/trimStart/this-value-object-toprimitive-call-err.js @@ -0,0 +1,34 @@ +// 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: > + Abrupt completion when getting Symbol.toPrimitive method +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). + ... +features: [string-trimming, Symbol.toPrimitive] +---*/ + +var thisVal = { + get [Symbol.toPrimitive]() { + throw new Test262Error(); + }, +}; + +assert.throws(Test262Error, function() { + String.prototype.trimStart.call(thisVal); +}); diff --git a/test/built-ins/String/prototype/trimStart/this-value-object-toprimitive-meth-err.js b/test/built-ins/String/prototype/trimStart/this-value-object-toprimitive-meth-err.js new file mode 100644 index 00000000000..4469409969a --- /dev/null +++ b/test/built-ins/String/prototype/trimStart/this-value-object-toprimitive-meth-err.js @@ -0,0 +1,36 @@ +// 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: > + Abrupt completion when Symbol.toPrimitive abrupt completes. +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 »). + ... +features: [string-trimming, Symbol.toPrimitive] +---*/ + +var thisVal = { + [Symbol.toPrimitive]: function() { + throw new Test262Error(); + }, +}; + +assert.throws(Test262Error, function() { + String.prototype.trimStart.call(thisVal); +}); 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 00000000000..a1d1d3d3991 --- /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-toprimitive-returns-object-err.js b/test/built-ins/String/prototype/trimStart/this-value-object-toprimitive-returns-object-err.js new file mode 100644 index 00000000000..becaf421b38 --- /dev/null +++ b/test/built-ins/String/prototype/trimStart/this-value-object-toprimitive-returns-object-err.js @@ -0,0 +1,38 @@ +// 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: > + Abrupt completion when Symbol.toPrimitive returns an object +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 ]) + ... + 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. + ... +features: [string-trimming, Symbol.toPrimitive] +---*/ + +var thisVal = { + [Symbol.toPrimitive]: function() { + return {}; + }, +}; + +assert.throws(TypeError, function() { + String.prototype.trimStart.call(thisVal); +}); diff --git a/test/built-ins/String/prototype/trimStart/this-value-object-tostring-call-err.js b/test/built-ins/String/prototype/trimStart/this-value-object-tostring-call-err.js new file mode 100644 index 00000000000..2a7737e86a3 --- /dev/null +++ b/test/built-ins/String/prototype/trimStart/this-value-object-tostring-call-err.js @@ -0,0 +1,51 @@ +// 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: > + Abrupt completion when getting toString method +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). +features: [string-trimming, Symbol.toPrimitive] +---*/ + +var thisVal = { + [Symbol.toPrimitive]: undefined, + get toString() { + throw new Test262Error(); + }, +}; + +assert.throws(Test262Error, function() { + String.prototype.trimStart.call(thisVal); +}); diff --git a/test/built-ins/String/prototype/trimStart/this-value-object-tostring-meth-err.js b/test/built-ins/String/prototype/trimStart/this-value-object-tostring-meth-err.js new file mode 100644 index 00000000000..98f28df24ac --- /dev/null +++ b/test/built-ins/String/prototype/trimStart/this-value-object-tostring-meth-err.js @@ -0,0 +1,53 @@ +// 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: > + Abrupt completion when toString called and abrupt completes. +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). +features: [string-trimming, Symbol.toPrimitive] +---*/ + +var thisVal = { + [Symbol.toPrimitive]: undefined, + toString: function() { + throw new Test262Error(); + }, +}; + +assert.throws(Test262Error, function() { + String.prototype.trimStart.call(thisVal); +}); 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 00000000000..862142a39d6 --- /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-tostring-returns-object-err.js b/test/built-ins/String/prototype/trimStart/this-value-object-tostring-returns-object-err.js new file mode 100644 index 00000000000..c55b81e1749 --- /dev/null +++ b/test/built-ins/String/prototype/trimStart/this-value-object-tostring-returns-object-err.js @@ -0,0 +1,56 @@ +// 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: > + Abrupt completion when toString called and returns an object +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. + 6. Throw a TypeError exception. +features: [string-trimming, Symbol.toPrimitive] +---*/ + + +var thisVal = { + [Symbol.toPrimitive]: undefined, + toString: function() { + return {}; + }, +}; + +assert.throws(TypeError, function() { + String.prototype.trimStart.call(thisVal); +}); diff --git a/test/built-ins/String/prototype/trimStart/this-value-object-valueof-call-err.js b/test/built-ins/String/prototype/trimStart/this-value-object-valueof-call-err.js new file mode 100644 index 00000000000..0d11d014a5b --- /dev/null +++ b/test/built-ins/String/prototype/trimStart/this-value-object-valueof-call-err.js @@ -0,0 +1,52 @@ +// 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: > + Abrupt completion when getting valueOf method +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). +features: [string-trimming, Symbol.toPrimitive] +---*/ + +var thisVal = { + [Symbol.toPrimitive]: undefined, + toString: undefined, + get valueOf() { + throw new Test262Error(); + }, +}; + +assert.throws(Test262Error, function() { + String.prototype.trimStart.call(thisVal); +}); diff --git a/test/built-ins/String/prototype/trimStart/this-value-object-valueof-meth-err.js b/test/built-ins/String/prototype/trimStart/this-value-object-valueof-meth-err.js new file mode 100644 index 00000000000..798bb64c616 --- /dev/null +++ b/test/built-ins/String/prototype/trimStart/this-value-object-valueof-meth-err.js @@ -0,0 +1,54 @@ +// 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: > + Abrupt completion when valueOf called and abrupt completes. +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). +features: [string-trimming, Symbol.toPrimitive] +---*/ + +var thisVal = { + [Symbol.toPrimitive]: undefined, + toString: undefined, + valueOf: function() { + throw new Test262Error(); + }, +}; + +assert.throws(Test262Error, function() { + String.prototype.trimStart.call(thisVal); +}); 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 00000000000..ae90d4be19d --- /dev/null +++ b/test/built-ins/String/prototype/trimStart/this-value-object-valueof-meth-priority.js @@ -0,0 +1,91 @@ +// 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-object-valueof-returns-object-err.js b/test/built-ins/String/prototype/trimStart/this-value-object-valueof-returns-object-err.js new file mode 100644 index 00000000000..b89e636ee82 --- /dev/null +++ b/test/built-ins/String/prototype/trimStart/this-value-object-valueof-returns-object-err.js @@ -0,0 +1,57 @@ +// 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: > + Abrupt completion when valueOf called and returns an object +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. + 6. Throw a TypeError exception. +features: [string-trimming, Symbol.toPrimitive] +---*/ + + +var thisVal = { + [Symbol.toPrimitive]: undefined, + toString: undefined, + valueOf: function() { + return {}; + }, +}; + +assert.throws(TypeError, function() { + String.prototype.trimStart.call(thisVal); +}); diff --git a/test/built-ins/String/prototype/trimStart/this-value-symbol-typeerror.js b/test/built-ins/String/prototype/trimStart/this-value-symbol-typeerror.js new file mode 100644 index 00000000000..15031e33c4d --- /dev/null +++ b/test/built-ins/String/prototype/trimStart/this-value-symbol-typeerror.js @@ -0,0 +1,24 @@ +// 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: Type error when "this" value is a Symbol +info: | + Runtime Semantics: TrimString ( string, where ) + 2. Let S be ? ToString(str). + + ToString ( argument ) + Argument Type: Symbol + Result: Throw a TypeError exception +features: [string-trimming] +---*/ + +var trimStart = String.prototype.trimStart; +var symbol = Symbol(); + +assert.throws( + TypeError, + function() { trimStart.call(symbol); }, + 'String.prototype.trimStart.call(Symbol())' +); diff --git a/test/built-ins/String/prototype/trimStart/this-value-whitespace.js b/test/built-ins/String/prototype/trimStart/this-value-whitespace.js new file mode 100644 index 00000000000..913d3a21ec9 --- /dev/null +++ b/test/built-ins/String/prototype/trimStart/this-value-whitespace.js @@ -0,0 +1,33 @@ +// 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: TrimStart removes all whitespace from the start of a string. +info: | + Runtime Symantics: TrimString ( string, where ) + ... + 3. If where is "start", let T be a String value that is a copy of S with + trailing white space removed. + ... + + The definition of white space is the union of WhiteSpace and LineTerminator. + When determining whether a Unicode code point is in Unicode general category + “Zs”, code unit sequences are interpreted as UTF-16 encoded code point + sequences as specified in 6.1.4. + +features: [string-trimming] +---*/ + +var trimStart = String.prototype.trimStart; + +// A string of all valid WhiteSpace Unicode code points +var wspc = '\u0009\u000B\u000C\u0020\u00A0\FEFF\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000'; + +var str = wspc + 'a' + wspc + 'b' + wspc; +var expected = 'a' + wspc + 'b' + wspc; + +assert.sameValue( + trimStart.call(str), + expected, +);