Skip to content

Commit a8f5191

Browse files
joyeecheungtargos
authored andcommitted
test: split test-whatwg-encoding-textdecoder.js
Split test-whatwg-encoding-textdecoder.js into: - `test-whatwg-encoding-custom-textdecoder.js` which tests Node.js-specific behaviors - `test-whatwg-encoding-custom-textdecoder-api-invalid-label.js` which is a customized version of the WPT counterpart - `test-whatwg-encoding-custom-api-basics.js` which is the part of `test-whatwg-encoding-api-basics.js` that can be run without ICU - `test-whatwg-encoding-api-basics.js` which can be replaced with WPT later. PR-URL: #25155 Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent bc66356 commit a8f5191

4 files changed

+178
-102
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
'use strict';
2+
3+
// From: https://github.com/w3c/web-platform-tests/blob/master/encoding/api-basics.html
4+
// TODO(joyeecheung): replace this with WPT
5+
6+
const common = require('../common');
7+
8+
if (!common.hasIntl)
9+
common.skip('missing Intl');
10+
11+
const assert = require('assert');
12+
13+
function testDecodeSample(encoding, string, bytes) {
14+
assert.strictEqual(
15+
new TextDecoder(encoding).decode(new Uint8Array(bytes)),
16+
string);
17+
assert.strictEqual(
18+
new TextDecoder(encoding).decode(new Uint8Array(bytes).buffer),
19+
string);
20+
}
21+
22+
// `z` (ASCII U+007A), cent (Latin-1 U+00A2), CJK water (BMP U+6C34),
23+
// G-Clef (non-BMP U+1D11E), PUA (BMP U+F8FF), PUA (non-BMP U+10FFFD)
24+
// byte-swapped BOM (non-character U+FFFE)
25+
const sample = 'z\xA2\u6C34\uD834\uDD1E\uF8FF\uDBFF\uDFFD\uFFFE';
26+
27+
{
28+
const encoding = 'utf-8';
29+
const string = sample;
30+
const bytes = [
31+
0x7A, 0xC2, 0xA2, 0xE6, 0xB0, 0xB4,
32+
0xF0, 0x9D, 0x84, 0x9E, 0xEF, 0xA3,
33+
0xBF, 0xF4, 0x8F, 0xBF, 0xBD, 0xEF,
34+
0xBF, 0xBE
35+
];
36+
const encoded = new TextEncoder().encode(string);
37+
assert.deepStrictEqual([].slice.call(encoded), bytes);
38+
assert.strictEqual(
39+
new TextDecoder(encoding).decode(new Uint8Array(bytes)),
40+
string);
41+
assert.strictEqual(
42+
new TextDecoder(encoding).decode(new Uint8Array(bytes).buffer),
43+
string);
44+
}
45+
46+
testDecodeSample(
47+
'utf-16le',
48+
sample,
49+
[
50+
0x7A, 0x00, 0xA2, 0x00, 0x34, 0x6C,
51+
0x34, 0xD8, 0x1E, 0xDD, 0xFF, 0xF8,
52+
0xFF, 0xDB, 0xFD, 0xDF, 0xFE, 0xFF
53+
]
54+
);
55+
56+
testDecodeSample(
57+
'utf-16be',
58+
sample,
59+
[
60+
0x00, 0x7A, 0x00, 0xA2, 0x6C, 0x34,
61+
0xD8, 0x34, 0xDD, 0x1E, 0xF8, 0xFF,
62+
0xDB, 0xFF, 0xDF, 0xFD, 0xFF, 0xFE
63+
]
64+
);
65+
66+
testDecodeSample(
67+
'utf-16',
68+
sample,
69+
[
70+
0x7A, 0x00, 0xA2, 0x00, 0x34, 0x6C,
71+
0x34, 0xD8, 0x1E, 0xDD, 0xFF, 0xF8,
72+
0xFF, 0xDB, 0xFD, 0xDF, 0xFE, 0xFF
73+
]
74+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
'use strict';
2+
3+
// From: https://github.com/w3c/web-platform-tests/blob/master/encoding/api-basics.html
4+
// This is the part that can be run without ICU
5+
6+
require('../common');
7+
8+
const assert = require('assert');
9+
10+
function testDecodeSample(encoding, string, bytes) {
11+
assert.strictEqual(
12+
new TextDecoder(encoding).decode(new Uint8Array(bytes)),
13+
string);
14+
assert.strictEqual(
15+
new TextDecoder(encoding).decode(new Uint8Array(bytes).buffer),
16+
string);
17+
}
18+
19+
// `z` (ASCII U+007A), cent (Latin-1 U+00A2), CJK water (BMP U+6C34),
20+
// G-Clef (non-BMP U+1D11E), PUA (BMP U+F8FF), PUA (non-BMP U+10FFFD)
21+
// byte-swapped BOM (non-character U+FFFE)
22+
const sample = 'z\xA2\u6C34\uD834\uDD1E\uF8FF\uDBFF\uDFFD\uFFFE';
23+
24+
{
25+
const encoding = 'utf-8';
26+
const string = sample;
27+
const bytes = [
28+
0x7A, 0xC2, 0xA2, 0xE6, 0xB0, 0xB4,
29+
0xF0, 0x9D, 0x84, 0x9E, 0xEF, 0xA3,
30+
0xBF, 0xF4, 0x8F, 0xBF, 0xBD, 0xEF,
31+
0xBF, 0xBE
32+
];
33+
const encoded = new TextEncoder().encode(string);
34+
assert.deepStrictEqual([].slice.call(encoded), bytes);
35+
assert.strictEqual(
36+
new TextDecoder(encoding).decode(new Uint8Array(bytes)),
37+
string);
38+
assert.strictEqual(
39+
new TextDecoder(encoding).decode(new Uint8Array(bytes).buffer),
40+
string);
41+
}
42+
43+
testDecodeSample(
44+
'utf-16le',
45+
sample,
46+
[
47+
0x7A, 0x00, 0xA2, 0x00, 0x34, 0x6C,
48+
0x34, 0xD8, 0x1E, 0xDD, 0xFF, 0xF8,
49+
0xFF, 0xDB, 0xFD, 0xDF, 0xFE, 0xFF
50+
]
51+
);
52+
53+
testDecodeSample(
54+
'utf-16',
55+
sample,
56+
[
57+
0x7A, 0x00, 0xA2, 0x00, 0x34, 0x6C,
58+
0x34, 0xD8, 0x1E, 0xDD, 0xFF, 0xF8,
59+
0xFF, 0xDB, 0xFD, 0xDF, 0xFE, 0xFF
60+
]
61+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict';
2+
// From: https://github.com/w3c/web-platform-tests/blob/master/encoding/api-invalid-label.html
3+
// With the twist that we specifically test for Node.js error codes
4+
5+
const common = require('../common');
6+
7+
[
8+
'utf-8',
9+
'unicode-1-1-utf-8',
10+
'utf8',
11+
'utf-16be',
12+
'utf-16le',
13+
'utf-16'
14+
].forEach((i) => {
15+
['\u0000', '\u000b', '\u00a0', '\u2028', '\u2029'].forEach((ws) => {
16+
common.expectsError(
17+
() => new TextDecoder(`${ws}${i}`),
18+
{
19+
code: 'ERR_ENCODING_NOT_SUPPORTED',
20+
type: RangeError
21+
}
22+
);
23+
24+
common.expectsError(
25+
() => new TextDecoder(`${i}${ws}`),
26+
{
27+
code: 'ERR_ENCODING_NOT_SUPPORTED',
28+
type: RangeError
29+
}
30+
);
31+
32+
common.expectsError(
33+
() => new TextDecoder(`${ws}${i}${ws}`),
34+
{
35+
code: 'ERR_ENCODING_NOT_SUPPORTED',
36+
type: RangeError
37+
}
38+
);
39+
});
40+
});

test/parallel/test-whatwg-encoding-textdecoder.js test/parallel/test-whatwg-encoding-custom-textdecoder.js

+3-102
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
// Flags: --expose-internals
2+
3+
// This tests Node.js-specific behaviors of TextDecoder
4+
25
'use strict';
36

47
const common = require('../common');
@@ -167,72 +170,6 @@ if (common.hasIntl) {
167170
});
168171
}
169172

170-
// From: https://github.com/w3c/web-platform-tests/blob/master/encoding/api-basics.html
171-
function testDecodeSample(encoding, string, bytes) {
172-
assert.strictEqual(
173-
new TextDecoder(encoding).decode(new Uint8Array(bytes)),
174-
string);
175-
assert.strictEqual(
176-
new TextDecoder(encoding).decode(new Uint8Array(bytes).buffer),
177-
string);
178-
}
179-
180-
// `z` (ASCII U+007A), cent (Latin-1 U+00A2), CJK water (BMP U+6C34),
181-
// G-Clef (non-BMP U+1D11E), PUA (BMP U+F8FF), PUA (non-BMP U+10FFFD)
182-
// byte-swapped BOM (non-character U+FFFE)
183-
const sample = 'z\xA2\u6C34\uD834\uDD1E\uF8FF\uDBFF\uDFFD\uFFFE';
184-
185-
{
186-
const encoding = 'utf-8';
187-
const string = sample;
188-
const bytes = [
189-
0x7A, 0xC2, 0xA2, 0xE6, 0xB0, 0xB4,
190-
0xF0, 0x9D, 0x84, 0x9E, 0xEF, 0xA3,
191-
0xBF, 0xF4, 0x8F, 0xBF, 0xBD, 0xEF,
192-
0xBF, 0xBE
193-
];
194-
const encoded = new TextEncoder().encode(string);
195-
assert.deepStrictEqual([].slice.call(encoded), bytes);
196-
assert.strictEqual(
197-
new TextDecoder(encoding).decode(new Uint8Array(bytes)),
198-
string);
199-
assert.strictEqual(
200-
new TextDecoder(encoding).decode(new Uint8Array(bytes).buffer),
201-
string);
202-
}
203-
204-
testDecodeSample(
205-
'utf-16le',
206-
sample,
207-
[
208-
0x7A, 0x00, 0xA2, 0x00, 0x34, 0x6C,
209-
0x34, 0xD8, 0x1E, 0xDD, 0xFF, 0xF8,
210-
0xFF, 0xDB, 0xFD, 0xDF, 0xFE, 0xFF
211-
]
212-
);
213-
214-
if (common.hasIntl) {
215-
testDecodeSample(
216-
'utf-16be',
217-
sample,
218-
[
219-
0x00, 0x7A, 0x00, 0xA2, 0x6C, 0x34,
220-
0xD8, 0x34, 0xDD, 0x1E, 0xF8, 0xFF,
221-
0xDB, 0xFF, 0xDF, 0xFD, 0xFF, 0xFE
222-
]
223-
);
224-
}
225-
226-
testDecodeSample(
227-
'utf-16',
228-
sample,
229-
[
230-
0x7A, 0x00, 0xA2, 0x00, 0x34, 0x6C,
231-
0x34, 0xD8, 0x1E, 0xDD, 0xFF, 0xF8,
232-
0xFF, 0xDB, 0xFD, 0xDF, 0xFE, 0xFF
233-
]
234-
);
235-
236173
{
237174
common.expectsError(
238175
() => new TextDecoder('utf-8', 1),
@@ -242,39 +179,3 @@ testDecodeSample(
242179
}
243180
);
244181
}
245-
246-
// From: https://github.com/w3c/web-platform-tests/blob/master/encoding/api-invalid-label.html
247-
[
248-
'utf-8',
249-
'unicode-1-1-utf-8',
250-
'utf8',
251-
'utf-16be',
252-
'utf-16le',
253-
'utf-16'
254-
].forEach((i) => {
255-
['\u0000', '\u000b', '\u00a0', '\u2028', '\u2029'].forEach((ws) => {
256-
common.expectsError(
257-
() => new TextDecoder(`${ws}${i}`),
258-
{
259-
code: 'ERR_ENCODING_NOT_SUPPORTED',
260-
type: RangeError
261-
}
262-
);
263-
264-
common.expectsError(
265-
() => new TextDecoder(`${i}${ws}`),
266-
{
267-
code: 'ERR_ENCODING_NOT_SUPPORTED',
268-
type: RangeError
269-
}
270-
);
271-
272-
common.expectsError(
273-
() => new TextDecoder(`${ws}${i}${ws}`),
274-
{
275-
code: 'ERR_ENCODING_NOT_SUPPORTED',
276-
type: RangeError
277-
}
278-
);
279-
});
280-
});

0 commit comments

Comments
 (0)