Skip to content

Commit

Permalink
[Tests] add test cases from test262
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Sep 15, 2022
1 parent 62e3f55 commit 6b21eb7
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@ var replacementChar = '\uFFFD';

module.exports = function (toWellFormed, t) {
t.test('well-formed strings', function (st) {
forEach(v.nonStrings.concat(v.strings, wholePoo), function (value) {
forEach(v.nonStrings.concat(
v.strings,
wholePoo, // a concatenated surrogate pair
'abc', // a latin-1 string
'a💩c', // a surrogate pair using a literal code point
'a\uD83D\uDCA9c', // a surrogate pair formed by escape sequences
'a' + leadingPoo + trailingPoo + 'd', // a surrogate pair formed by concatenation
'a\u25A8c' // a non-ASCII character
), function (value) {
if (value != null) { // eslint-disable-line eqeqeq
var string = typeof value === 'symbol' ? SymbolDescriptiveString(value) : String(value);
st.equal(
Expand All @@ -29,16 +37,21 @@ module.exports = function (toWellFormed, t) {
});

t.test('not well-formed strings', function (st) {
st.equal(
toWellFormed(leadingPoo),
replacementChar,
'a string with a leading surrogate but no trailing surrogate has the lone surrogate replaced as expected'
);
st.equal(
toWellFormed(trailingPoo),
replacementChar,
'a string with a trailing surrogate but no leading surrogate has the lone surrogate replaced as expected'
);
forEach([
[leadingPoo, replacementChar, 'a string with a leading surrogate but no trailing surrogate'],
[trailingPoo, replacementChar, 'a string with a trailing surrogate but no leading surrogate'],
['a' + leadingPoo + 'c' + leadingPoo + 'e', 'a' + replacementChar + 'c' + replacementChar + 'e', 'leading lone surrogates'],
['a' + trailingPoo + 'c' + trailingPoo + 'e', 'a' + replacementChar + 'c' + replacementChar + 'e', 'trailing lone surrogates'],
['a' + trailingPoo + leadingPoo + 'd', 'a' + replacementChar + replacementChar + 'd', 'a wrong-ordered surrogate pair'],
[wholePoo.slice(0, 1), replacementChar, 'a surrogate pair sliced to the leading surrogate'],
[wholePoo.slice(1), replacementChar, 'a surrogate pair sliced to the trailing surrogate is not well-formed']
], function (arr) {
var str = arr[0];
var exp = arr[1];
var msg = arr[2];

st.equal(toWellFormed(str), exp, msg + ' is not well-formed, and lone surrogates are replaced as expected');
});

st.end();
});
Expand Down

0 comments on commit 6b21eb7

Please sign in to comment.