Skip to content

Commit

Permalink
String.prototype.isWellFormed/String.prototype.toWellFormed: add …
Browse files Browse the repository at this point in the history
…tests
  • Loading branch information
ljharb committed Sep 14, 2022
1 parent 39302ea commit 053e3f1
Show file tree
Hide file tree
Showing 15 changed files with 402 additions and 0 deletions.
5 changes: 5 additions & 0 deletions features.txt
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,11 @@ change-array-by-copy
# https://tc39.es/proposal-array-from-async/
Array.fromAsync

# 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
Expand Down
21 changes: 21 additions & 0 deletions test/built-ins/String/prototype/isWellFormed/length.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// 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: >
String.prototype.isWellFormed.length value and descriptor.
info: |
String.prototype.isWellFormed( )
17 ECMAScript Standard Built-in Objects
includes: [propertyHelper.js]
features: [String.prototype.isWellFormed]
---*/

verifyProperty(String.prototype.isWellFormed, 'length', {
configurable: true,
enumerable: false,
writable: false,
value: 0
});
21 changes: 21 additions & 0 deletions test/built-ins/String/prototype/isWellFormed/name.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// 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: >
String.prototype.isWellFormed.name value and descriptor.
info: |
String.prototype.isWellFormed( )
17 ECMAScript Standard Built-in Objects
includes: [propertyHelper.js]
features: [String.prototype.isWellFormed]
---*/

verifyProperty(String.prototype.isWellFormed, 'name', {
enumerable: false,
writable: false,
configurable: true,
value: 'isWellFormed'
});
32 changes: 32 additions & 0 deletions test/built-ins/String/prototype/isWellFormed/not-a-constructor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// 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: [String.prototype.isWellFormed, 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');
26 changes: 26 additions & 0 deletions test/built-ins/String/prototype/isWellFormed/prop-desc.js
Original file line number Diff line number Diff line change
@@ -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.iswellformed
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
});
Original file line number Diff line number Diff line change
@@ -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.iswellformed
description: >
Return abrupt from RequireObjectCoercible(this value).
info: |
String.prototype.isWellFormed( )
1. 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');
44 changes: 44 additions & 0 deletions test/built-ins/String/prototype/isWellFormed/returns-boolean.js
Original file line number Diff line number Diff line change
@@ -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.iswellformed
description: >
The method should return a boolean.
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,
'lone leading surrogates are not well-formed'
);
assert.sameValue(
('a' + trailingPoo + 'c' + trailingPoo + 'e').isWellFormed(),
false,
'lone trailing surrogates are not well-formed'
);
assert.sameValue(
('a' + trailingPoo + leadingPoo + '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');
27 changes: 27 additions & 0 deletions test/built-ins/String/prototype/isWellFormed/to-string.js
Original file line number Diff line number Diff line change
@@ -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 coerce the receiver to a string.
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'
);
21 changes: 21 additions & 0 deletions test/built-ins/String/prototype/toWellFormed/length.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// 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.towellformed
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]
---*/

verifyProperty(String.prototype.toWellFormed, 'length', {
configurable: true,
enumerable: false,
writable: false,
value: 0
});
21 changes: 21 additions & 0 deletions test/built-ins/String/prototype/toWellFormed/name.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// 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.towellformed
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]
---*/

verifyProperty(String.prototype.toWellFormed, 'name', {
enumerable: false,
writable: false,
configurable: true,
value: 'isWellFormed'
});
32 changes: 32 additions & 0 deletions test/built-ins/String/prototype/toWellFormed/not-a-constructor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// 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: [String.prototype.toWellFormed, 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');
26 changes: 26 additions & 0 deletions test/built-ins/String/prototype/toWellFormed/prop-desc.js
Original file line number Diff line number Diff line change
@@ -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.towellformed
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
});
Original file line number Diff line number Diff line change
@@ -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.towellformed
description: >
Return abrupt from RequireObjectCoercible(this value).
info: |
String.prototype.toWellFormed( )
1. 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');
Loading

0 comments on commit 053e3f1

Please sign in to comment.