From 9496643ae12a08a16159ed8134889c2b52b00358 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Tue, 13 Sep 2022 23:56:04 -0700 Subject: [PATCH] `String.prototype.isWellFormed`/`String.prototype.toWellFormed`: add tests --- features.txt | 7 ++- .../String/prototype/isWellFormed/length.js | 24 ++++++++ .../String/prototype/isWellFormed/name.js | 26 +++++++++ .../isWellFormed/not-a-constructor.js | 34 ++++++++++++ .../prototype/isWellFormed/prop-desc.js | 26 +++++++++ .../isWellFormed/return-abrupt-from-this.js | 22 ++++++++ .../prototype/isWellFormed/returns-boolean.js | 44 +++++++++++++++ .../prototype/isWellFormed/to-string.js | 27 +++++++++ .../String/prototype/toWellFormed/length.js | 24 ++++++++ .../String/prototype/toWellFormed/name.js | 26 +++++++++ .../toWellFormed/not-a-constructor.js | 34 ++++++++++++ .../prototype/toWellFormed/prop-desc.js | 26 +++++++++ .../toWellFormed/return-abrupt-from-this.js | 22 ++++++++ .../returns-well-formed-string.js | 55 +++++++++++++++++++ .../prototype/toWellFormed/to-string.js | 27 +++++++++ 15 files changed, 423 insertions(+), 1 deletion(-) create mode 100644 test/built-ins/String/prototype/isWellFormed/length.js create mode 100644 test/built-ins/String/prototype/isWellFormed/name.js create mode 100644 test/built-ins/String/prototype/isWellFormed/not-a-constructor.js create mode 100644 test/built-ins/String/prototype/isWellFormed/prop-desc.js create mode 100644 test/built-ins/String/prototype/isWellFormed/return-abrupt-from-this.js create mode 100644 test/built-ins/String/prototype/isWellFormed/returns-boolean.js create mode 100644 test/built-ins/String/prototype/isWellFormed/to-string.js create mode 100644 test/built-ins/String/prototype/toWellFormed/length.js create mode 100644 test/built-ins/String/prototype/toWellFormed/name.js create mode 100644 test/built-ins/String/prototype/toWellFormed/not-a-constructor.js create mode 100644 test/built-ins/String/prototype/toWellFormed/prop-desc.js create mode 100644 test/built-ins/String/prototype/toWellFormed/return-abrupt-from-this.js create mode 100644 test/built-ins/String/prototype/toWellFormed/returns-well-formed-string.js create mode 100644 test/built-ins/String/prototype/toWellFormed/to-string.js diff --git a/features.txt b/features.txt index 841917b83c0..9cba37ebd89 100644 --- a/features.txt +++ b/features.txt @@ -280,6 +280,11 @@ decorators # https://github.com/tc39/proposal-duplicate-named-capturing-groups regexp-duplicate-named-groups +# Well-formed Unicode strings +# https://github.com/tc39/proposal-is-usv-string +String.prototype.isWellFormed +String.prototype.toWellFormed + ## Standard language features # # Language features that have been included in a published version of the @@ -377,4 +382,4 @@ __setter__ # object, go here. IsHTMLDDA -host-gc-required +host-gc-required \ No newline at end of file diff --git a/test/built-ins/String/prototype/isWellFormed/length.js b/test/built-ins/String/prototype/isWellFormed/length.js new file mode 100644 index 00000000000..f2433317053 --- /dev/null +++ b/test/built-ins/String/prototype/isWellFormed/length.js @@ -0,0 +1,24 @@ +// Copyright (C) 2022 Jordan Harband. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-string.prototype.is-well-formed +description: > + String.prototype.isWellFormed.length value and descriptor. +info: | + String.prototype.isWellFormed( ) + + 17 ECMAScript Standard Built-in Objects + +includes: [propertyHelper.js] +features: [String.prototype.isWellFormed] +---*/ +assert.sameValue(typeof String.prototype.isWellFormed, 'function'); + +assert.sameValue( + String.prototype.isWellFormed.length, 0, + 'The value of String.prototype.isWellFormed.length is 0' +); + +verifyNotEnumerable(String.prototype.isWellFormed, 'length'); +verifyNotWritable(String.prototype.isWellFormed, 'length'); +verifyConfigurable(String.prototype.isWellFormed, 'length'); diff --git a/test/built-ins/String/prototype/isWellFormed/name.js b/test/built-ins/String/prototype/isWellFormed/name.js new file mode 100644 index 00000000000..3bcdac03076 --- /dev/null +++ b/test/built-ins/String/prototype/isWellFormed/name.js @@ -0,0 +1,26 @@ +// Copyright (C) 2022 Jordan Harband. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-string.prototype.is-well-formed +description: > + String.prototype.isWellFormed.name value and descriptor. +info: | + String.prototype.isWellFormed( ) + + 17 ECMAScript Standard Built-in Objects + +includes: [propertyHelper.js] +features: [String.prototype.isWellFormed] +---*/ +assert.sameValue(typeof String.prototype.isWellFormed, 'function'); + +assert.sameValue( + String.prototype.isWellFormed.name, 'isWellFormed', + 'The value of String.prototype.isWellFormed.name is "isWellFormed"' +); + +verifyProperty(String.prototype.isWellFormed, 'name', { + enumerable: false, + writable: false, + configurable: true +}); diff --git a/test/built-ins/String/prototype/isWellFormed/not-a-constructor.js b/test/built-ins/String/prototype/isWellFormed/not-a-constructor.js new file mode 100644 index 00000000000..06d1038df9e --- /dev/null +++ b/test/built-ins/String/prototype/isWellFormed/not-a-constructor.js @@ -0,0 +1,34 @@ +// Copyright (C) 2022 Jordan Harband. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-ecmascript-standard-built-in-objects +description: > + String.prototype.includes does not implement [[Construct]], is not new-able +info: | + ECMAScript Function Objects + + Built-in function objects that are not identified as constructors do not + implement the [[Construct]] internal method unless otherwise specified in + the description of a particular function. + + sec-evaluatenew + + ... + 7. If IsConstructor(constructor) is false, throw a TypeError exception. + ... +includes: [isConstructor.js] +features: [Reflect.construct] +---*/ + +assert.sameValue( + isConstructor(String.prototype.isWellFormed), + false, + 'isConstructor(String.prototype.isWellFormed) must return false' +); + +assert.throws(TypeError, function () { + new String.prototype.isWellFormed(); +}, '`new String.prototype.isWellFormed()` throws TypeError'); + + diff --git a/test/built-ins/String/prototype/isWellFormed/prop-desc.js b/test/built-ins/String/prototype/isWellFormed/prop-desc.js new file mode 100644 index 00000000000..694543e252f --- /dev/null +++ b/test/built-ins/String/prototype/isWellFormed/prop-desc.js @@ -0,0 +1,26 @@ +// Copyright (C) 2020 Rick Waldron. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-string.prototype.is-well-formed +description: > + Property type and descriptor. +info: | + String.prototype.isWellFormed( ) + + 17 ECMAScript Standard Built-in Objects +includes: [propertyHelper.js] +features: [String.prototype.isWellFormed] +---*/ +assert.sameValue(typeof String.prototype.isWellFormed, 'function'); + +assert.sameValue( + typeof String.prototype.isWellFormed, + 'function', + 'The value of `typeof String.prototype.isWellFormed` is "function"' +); + +verifyProperty(String.prototype, 'isWellFormed', { + enumerable: false, + writable: true, + configurable: true +}); diff --git a/test/built-ins/String/prototype/isWellFormed/return-abrupt-from-this.js b/test/built-ins/String/prototype/isWellFormed/return-abrupt-from-this.js new file mode 100644 index 00000000000..a8a988285e2 --- /dev/null +++ b/test/built-ins/String/prototype/isWellFormed/return-abrupt-from-this.js @@ -0,0 +1,22 @@ +// Copyright (C) 2022 Jordan Harband. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-string.prototype.is-well-formed +description: > + Return abrupt from RequireObjectCoercible(this value). +info: | + String.prototype.isWellFormed( ) + + Let O be ? RequireObjectCoercible(this value). + +features: [String.prototype.isWellFormed] +---*/ +assert.sameValue(typeof String.prototype.isWellFormed, 'function'); + +assert.throws(TypeError, function () { + String.prototype.isWellFormed.call(undefined); +}, '`String.prototype.isWellFormed.call(undefined)` throws TypeError'); + +assert.throws(TypeError, function () { + String.prototype.isWellFormed.call(null); +}, '`String.prototype.isWellFormed.call(null)` throws TypeError'); diff --git a/test/built-ins/String/prototype/isWellFormed/returns-boolean.js b/test/built-ins/String/prototype/isWellFormed/returns-boolean.js new file mode 100644 index 00000000000..377cc539805 --- /dev/null +++ b/test/built-ins/String/prototype/isWellFormed/returns-boolean.js @@ -0,0 +1,44 @@ +// Copyright (C) 2022 Jordan Harband. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-String.prototype.at +description: > + The method should return an Iterator instance. +info: | + String.prototype.isWellFormed ( ) + + 1. Let O be ? RequireObjectCoercible(this value). + 2. Let S be ? ToString(O). + 3. Return IsStringWellFormedUnicode(S). + +features: [String.prototype.isWellFormed] +---*/ +assert.sameValue(typeof String.prototype.isWellFormed, 'function'); + +var leadingPoo = '\uD83D'; +var trailingPoo = '\uDCA9'; +var wholePoo = leadingPoo + trailingPoo; + +assert.sameValue( + ('a' + leadingPoo + 'c' + leadingPoo + 'e').isWellFormed(), + false, + 'leading lone surrogates are not well-formed' +); +assert.sameValue( + ('a' + trailingPoo + 'c' + trailingPoo + 'e').isWellFormed(), + false, + 'trailing lone surrogates are not well-formed' +); +assert.sameValue( + ('a' + leadingPoo + trailingPoo + 'd').isWellFormed(), + false, + 'a wrong-ordered surrogate pair is not well-formed' +) + +assert.sameValue('a💩c'.isWellFormed(), true, 'a surrogate pair using a literal code point is well-formed'); +assert.sameValue('a\uD83D\uDCA9c'.isWellFormed(), true, 'a surrogate pair formed by escape sequences is well-formed'); +assert.sameValue(('a' + leadingPoo + trailingPoo + 'd').isWellFormed(), true, 'a surrogate pair formed by concatenation is well-formed'); +assert.sameValue(wholePoo.slice(0, 1).isWellFormed(), false, 'a surrogate pair sliced to the leading surrogate is not well-formed'); +assert.sameValue(wholePoo.slice(1).isWellFormed(), false, 'a surrogate pair sliced to the trailing surrogate is not well-formed'); +assert.sameValue('abc'.isWellFormed(), true, 'a latin-1 string is well-formed'); +assert.sameValue('a\u25A8c'.isWellFormed(), true, 'a non-ASCII character is well-formed'); \ No newline at end of file diff --git a/test/built-ins/String/prototype/isWellFormed/to-string.js b/test/built-ins/String/prototype/isWellFormed/to-string.js new file mode 100644 index 00000000000..c6c9c5325db --- /dev/null +++ b/test/built-ins/String/prototype/isWellFormed/to-string.js @@ -0,0 +1,27 @@ +// Copyright (C) 2022 Jordan Harband. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-String.prototype.isWellFormed +description: > + The method should return an Iterator instance. +info: | + String.prototype.isWellFormed ( ) + + 1. Let O be ? RequireObjectCoercible(this value). + 2. Let S be ? ToString(O). + 3. Return IsStringWellFormedUnicode(S). + +features: [String.prototype.isWellFormed] +---*/ + +var obj = { + toString: function () { + throw new Test262Error('calls ToString'); + } +}; + +asserts.throws( + function () { String.prototype.isWellFormed.call(obj); }, + Test262Error, + 'coerces the receiver to a string' +); \ No newline at end of file diff --git a/test/built-ins/String/prototype/toWellFormed/length.js b/test/built-ins/String/prototype/toWellFormed/length.js new file mode 100644 index 00000000000..16b7fcd7f8f --- /dev/null +++ b/test/built-ins/String/prototype/toWellFormed/length.js @@ -0,0 +1,24 @@ +// Copyright (C) 2022 Jordan Harband. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-string.prototype.to-well-formed +description: > + String.prototype.toWellFormed.length value and descriptor. +info: | + String.prototype.toWellFormed( ) + + 17 ECMAScript Standard Built-in Objects + +includes: [propertyHelper.js] +features: [String.prototype.toWellFormed] +---*/ +assert.sameValue(typeof String.prototype.toWellFormed, 'function'); + +assert.sameValue( + String.prototype.toWellFormed.length, 0, + 'The value of String.prototype.toWellFormed.length is 0' +); + +verifyNotEnumerable(String.prototype.toWellFormed, 'length'); +verifyNotWritable(String.prototype.toWellFormed, 'length'); +verifyConfigurable(String.prototype.toWellFormed, 'length'); diff --git a/test/built-ins/String/prototype/toWellFormed/name.js b/test/built-ins/String/prototype/toWellFormed/name.js new file mode 100644 index 00000000000..62c06a70b8d --- /dev/null +++ b/test/built-ins/String/prototype/toWellFormed/name.js @@ -0,0 +1,26 @@ +// Copyright (C) 2022 Jordan Harband. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-string.prototype.to-well-formed +description: > + String.prototype.toWellFormed.name value and descriptor. +info: | + String.prototype.toWellFormed( ) + + 17 ECMAScript Standard Built-in Objects + +includes: [propertyHelper.js] +features: [String.prototype.toWellFormed] +---*/ +assert.sameValue(typeof String.prototype.toWellFormed, 'function'); + +assert.sameValue( + String.prototype.toWellFormed.name, 'isWellFormed', + 'The value of String.prototype.toWellFormed.name is "toWellFormed"' +); + +verifyProperty(String.prototype.toWellFormed, 'name', { + enumerable: false, + writable: false, + configurable: true +}); diff --git a/test/built-ins/String/prototype/toWellFormed/not-a-constructor.js b/test/built-ins/String/prototype/toWellFormed/not-a-constructor.js new file mode 100644 index 00000000000..b452d3df185 --- /dev/null +++ b/test/built-ins/String/prototype/toWellFormed/not-a-constructor.js @@ -0,0 +1,34 @@ +// Copyright (C) 2022 Jordan Harband. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-ecmascript-standard-built-in-objects +description: > + String.prototype.includes does not implement [[Construct]], is not new-able +info: | + ECMAScript Function Objects + + Built-in function objects that are not identified as constructors do not + implement the [[Construct]] internal method unless otherwise specified in + the description of a particular function. + + sec-evaluatenew + + ... + 7. If IsConstructor(constructor) is false, throw a TypeError exception. + ... +includes: [isConstructor.js] +features: [Reflect.construct] +---*/ + +assert.sameValue( + isConstructor(String.prototype.toWellFormed), + false, + 'isConstructor(String.prototype.toWellFormed) must return false' +); + +assert.throws(TypeError, function () { + new String.prototype.toWellFormed(); +}, '`new String.prototype.toWellFormed()` throws TypeError'); + + diff --git a/test/built-ins/String/prototype/toWellFormed/prop-desc.js b/test/built-ins/String/prototype/toWellFormed/prop-desc.js new file mode 100644 index 00000000000..361e8aef591 --- /dev/null +++ b/test/built-ins/String/prototype/toWellFormed/prop-desc.js @@ -0,0 +1,26 @@ +// Copyright (C) 2020 Rick Waldron. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-string.prototype.is-well-formed +description: > + Property type and descriptor. +info: | + String.prototype.toWellFormed( ) + + 17 ECMAScript Standard Built-in Objects +includes: [propertyHelper.js] +features: [String.prototype.toWellFormed] +---*/ +assert.sameValue(typeof String.prototype.toWellFormed, 'function'); + +assert.sameValue( + typeof String.prototype.isWellFormed, + 'function', + 'The value of `typeof String.prototype.toWellFormed` is "function"' +); + +verifyProperty(String.prototype, 'toWellFormed', { + enumerable: false, + writable: true, + configurable: true +}); diff --git a/test/built-ins/String/prototype/toWellFormed/return-abrupt-from-this.js b/test/built-ins/String/prototype/toWellFormed/return-abrupt-from-this.js new file mode 100644 index 00000000000..e4730a023dc --- /dev/null +++ b/test/built-ins/String/prototype/toWellFormed/return-abrupt-from-this.js @@ -0,0 +1,22 @@ +// Copyright (C) 2022 Jordan Harband. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-string.prototype.to-well-formed +description: > + Return abrupt from RequireObjectCoercible(this value). +info: | + String.prototype.toWellFormed( ) + + Let O be ? RequireObjectCoercible(this value). + +features: [String.prototype.toWellFormed] +---*/ +assert.sameValue(typeof String.prototype.toWellFormed, 'function'); + +assert.throws(TypeError, function () { + String.prototype.toWellFormed.call(undefined); +}, '`String.prototype.toWellFormed.call(undefined)` throws TypeError'); + +assert.throws(TypeError, function () { + String.prototype.toWellFormed.call(null); +}, '`String.prototype.toWellFormed.call(null)` throws TypeError'); diff --git a/test/built-ins/String/prototype/toWellFormed/returns-well-formed-string.js b/test/built-ins/String/prototype/toWellFormed/returns-well-formed-string.js new file mode 100644 index 00000000000..2fff28503a1 --- /dev/null +++ b/test/built-ins/String/prototype/toWellFormed/returns-well-formed-string.js @@ -0,0 +1,55 @@ +// Copyright (C) 2022 Jordan Harband. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-String.prototype.at +description: > + The method should return an Iterator instance. +info: | + String.prototype.toWellFormed ( ) + + 1. Let O be ? RequireObjectCoercible(this value). + 2. Let S be ? ToString(O). + 3. Let strLen be the length of S. + 4. Let k be 0. + 5. Let result be the empty String. + 6. Repeat, while k < strLen, + a. Let cp be CodePointAt(S, k). + b. If cp.[[IsUnpairedSurrogate]] is true, then + i. Set result to the string-concatenation of result and 0xFFFD (REPLACEMENT CHARACTER). + c. Else, + i. Set result to the string-concatenation of result and UTF16EncodeCodePoint(cp.[[CodePoint]]). + d. Set k to k + cp.[[CodeUnitCount]]. + 7. Return result. + +features: [String.prototype.toWellFormed] +---*/ +assert.sameValue(typeof String.prototype.toWellFormed, 'function'); + +var replacementChar = '\uFFFD'; +var leadingPoo = '\uD83D'; +var trailingPoo = '\uDCA9'; +var wholePoo = leadingPoo + trailingPoo; + +assert.sameValue( + ('a' + leadingPoo + 'c' + leadingPoo + 'e').toWellFormed(), + 'a' + replacementChar + 'c' + replacementChar + 'e', + 'leading lone surrogates are replaced with the expected replacement character' +); +assert.sameValue( + ('a' + trailingPoo + 'c' + trailingPoo + 'e').toWellFormed(), + 'a' + replacementChar + 'c' + replacementChar + 'e', + 'trailing lone surrogates are replaced with the expected replacement character' +); +assert.sameValue( + ('a' + leadingPoo + trailingPoo + 'd').toWellFormed(), + 'a' + replacementChar + replacementChar + 'd', + 'a wrong-ordered surrogate pair is replaced with two replacement characters' +) + +assert.sameValue('a💩c'.toWellFormed(), 'a💩c', 'a surrogate pair using a literal code point is already well-formed'); +assert.sameValue('a\uD83D\uDCA9c'.toWellFormed(), 'a\uD83D\uDCA9c', 'a surrogate pair formed by escape sequences is already well-formed'); +assert.sameValue(('a' + leadingPoo + trailingPoo + 'd').toWellFormed(), 'a' + wholePoo + 'd', 'a surrogate pair formed by concatenation is already well-formed'); +assert.sameValue(wholePoo.slice(0, 1).toWellFormed(), replacementChar, 'a surrogate pair sliced to the leading surrogate is replaced with the expected replacement character'); +assert.sameValue(wholePoo.slice(1).toWellFormed(), replacementChar, 'a surrogate pair sliced to the trailing surrogate is replaced with the expected replacement character'); +assert.sameValue('abc'.toWellFormed(), 'abc', 'a latin-1 string is already well-formed'); +assert.sameValue('a\u25A8c'.toWellFormed(), 'a\u25A8c', 'a string with a non-ASCII character is already well-formed'); \ No newline at end of file diff --git a/test/built-ins/String/prototype/toWellFormed/to-string.js b/test/built-ins/String/prototype/toWellFormed/to-string.js new file mode 100644 index 00000000000..c6c9c5325db --- /dev/null +++ b/test/built-ins/String/prototype/toWellFormed/to-string.js @@ -0,0 +1,27 @@ +// Copyright (C) 2022 Jordan Harband. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-String.prototype.isWellFormed +description: > + The method should return an Iterator instance. +info: | + String.prototype.isWellFormed ( ) + + 1. Let O be ? RequireObjectCoercible(this value). + 2. Let S be ? ToString(O). + 3. Return IsStringWellFormedUnicode(S). + +features: [String.prototype.isWellFormed] +---*/ + +var obj = { + toString: function () { + throw new Test262Error('calls ToString'); + } +}; + +asserts.throws( + function () { String.prototype.isWellFormed.call(obj); }, + Test262Error, + 'coerces the receiver to a string' +); \ No newline at end of file