Skip to content

Commit 2b13fda

Browse files
Sho Miyamotoevanlucas
Sho Miyamoto
authored andcommitted
test: add assertions for TextEncoder/Decoder
PR-URL: #18132 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Tiancheng "Timothy" Gu <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent aee8be9 commit 2b13fda

File tree

2 files changed

+51
-24
lines changed

2 files changed

+51
-24
lines changed

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

+29-12
Original file line numberDiff line numberDiff line change
@@ -91,18 +91,35 @@ if (common.hasIntl) {
9191
}
9292

9393
{
94-
const fn = TextDecoder.prototype[inspect];
95-
assert.doesNotThrow(() => {
96-
fn.call(new TextDecoder(), Infinity, {});
97-
});
98-
99-
[{}, [], true, 1, '', new TextEncoder()].forEach((i) => {
100-
common.expectsError(() => fn.call(i, Infinity, {}),
101-
{
102-
code: 'ERR_INVALID_THIS',
103-
type: TypeError,
104-
message: 'Value of "this" must be of type TextDecoder'
105-
});
94+
const inspectFn = TextDecoder.prototype[inspect];
95+
const decodeFn = TextDecoder.prototype.decode;
96+
const {
97+
encoding: { get: encodingGetter },
98+
fatal: { get: fatalGetter },
99+
ignoreBOM: { get: ignoreBOMGetter },
100+
} = Object.getOwnPropertyDescriptors(TextDecoder.prototype);
101+
102+
const instance = new TextDecoder();
103+
104+
const expectedError = {
105+
code: 'ERR_INVALID_THIS',
106+
type: TypeError,
107+
message: 'Value of "this" must be of type TextDecoder'
108+
};
109+
110+
assert.doesNotThrow(() => inspectFn.call(instance, Infinity, {}));
111+
assert.doesNotThrow(() => decodeFn.call(instance));
112+
assert.doesNotThrow(() => encodingGetter.call(instance));
113+
assert.doesNotThrow(() => fatalGetter.call(instance));
114+
assert.doesNotThrow(() => ignoreBOMGetter.call(instance));
115+
116+
const invalidThisArgs = [{}, [], true, 1, '', new TextEncoder()];
117+
invalidThisArgs.forEach((i) => {
118+
common.expectsError(() => inspectFn.call(i, Infinity, {}), expectedError);
119+
common.expectsError(() => decodeFn.call(i), expectedError);
120+
common.expectsError(() => encodingGetter.call(i), expectedError);
121+
common.expectsError(() => fatalGetter.call(i), expectedError);
122+
common.expectsError(() => ignoreBOMGetter.call(i), expectedError);
106123
});
107124
}
108125

test/parallel/test-whatwg-encoding-textencoder.js

+22-12
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,27 @@ assert(TextEncoder);
3535
}
3636

3737
{
38-
const fn = TextEncoder.prototype[inspect];
39-
assert.doesNotThrow(() => {
40-
fn.call(new TextEncoder(), Infinity, {});
41-
});
42-
43-
[{}, [], true, 1, '', new TextDecoder()].forEach((i) => {
44-
common.expectsError(() => fn.call(i, Infinity, {}),
45-
{
46-
code: 'ERR_INVALID_THIS',
47-
type: TypeError,
48-
message: 'Value of "this" must be of type TextEncoder'
49-
});
38+
const inspectFn = TextEncoder.prototype[inspect];
39+
const encodeFn = TextEncoder.prototype.encode;
40+
const encodingGetter =
41+
Object.getOwnPropertyDescriptor(TextEncoder.prototype, 'encoding').get;
42+
43+
const instance = new TextEncoder();
44+
45+
const expectedError = {
46+
code: 'ERR_INVALID_THIS',
47+
type: TypeError,
48+
message: 'Value of "this" must be of type TextEncoder'
49+
};
50+
51+
assert.doesNotThrow(() => inspectFn.call(instance, Infinity, {}));
52+
assert.doesNotThrow(() => encodeFn.call(instance));
53+
assert.doesNotThrow(() => encodingGetter.call(instance));
54+
55+
const invalidThisArgs = [{}, [], true, 1, '', new TextDecoder()];
56+
invalidThisArgs.forEach((i) => {
57+
common.expectsError(() => inspectFn.call(i, Infinity, {}), expectedError);
58+
common.expectsError(() => encodeFn.call(i), expectedError);
59+
common.expectsError(() => encodingGetter.call(i), expectedError);
5060
});
5161
}

0 commit comments

Comments
 (0)