Skip to content

Commit e42ab10

Browse files
Sho MiyamotoMylesBorins
Sho Miyamoto
authored andcommitted
test: add assertions for TextEncoder/Decoder
Backport-PR-URL: #19358 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 a0a45fc commit e42ab10

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
@@ -90,18 +90,35 @@ if (common.hasIntl) {
9090
}
9191

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

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-
assert.throws(() => fn.call(i, Infinity, {}),
45-
common.expectsError({
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)