Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v8.x-backport] test: add assertions for TextEncoder/Decoder #19358

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 29 additions & 12 deletions test/parallel/test-whatwg-encoding-textdecoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,35 @@ if (common.hasIntl) {
}

{
const fn = TextDecoder.prototype[inspect];
assert.doesNotThrow(() => {
fn.call(new TextDecoder(), Infinity, {});
});

[{}, [], true, 1, '', new TextEncoder()].forEach((i) => {
assert.throws(() => fn.call(i, Infinity, {}),
common.expectsError({
code: 'ERR_INVALID_THIS',
type: TypeError,
message: 'Value of "this" must be of type TextDecoder'
}));
const inspectFn = TextDecoder.prototype[inspect];
const decodeFn = TextDecoder.prototype.decode;
const {
encoding: { get: encodingGetter },
fatal: { get: fatalGetter },
ignoreBOM: { get: ignoreBOMGetter },
} = Object.getOwnPropertyDescriptors(TextDecoder.prototype);

const instance = new TextDecoder();

const expectedError = {
code: 'ERR_INVALID_THIS',
type: TypeError,
message: 'Value of "this" must be of type TextDecoder'
};

assert.doesNotThrow(() => inspectFn.call(instance, Infinity, {}));
assert.doesNotThrow(() => decodeFn.call(instance));
assert.doesNotThrow(() => encodingGetter.call(instance));
assert.doesNotThrow(() => fatalGetter.call(instance));
assert.doesNotThrow(() => ignoreBOMGetter.call(instance));

const invalidThisArgs = [{}, [], true, 1, '', new TextEncoder()];
invalidThisArgs.forEach((i) => {
common.expectsError(() => inspectFn.call(i, Infinity, {}), expectedError);
common.expectsError(() => decodeFn.call(i), expectedError);
common.expectsError(() => encodingGetter.call(i), expectedError);
common.expectsError(() => fatalGetter.call(i), expectedError);
common.expectsError(() => ignoreBOMGetter.call(i), expectedError);
});
}

Expand Down
34 changes: 22 additions & 12 deletions test/parallel/test-whatwg-encoding-textencoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,27 @@ assert(TextEncoder);
}

{
const fn = TextEncoder.prototype[inspect];
assert.doesNotThrow(() => {
fn.call(new TextEncoder(), Infinity, {});
});

[{}, [], true, 1, '', new TextDecoder()].forEach((i) => {
assert.throws(() => fn.call(i, Infinity, {}),
common.expectsError({
code: 'ERR_INVALID_THIS',
type: TypeError,
message: 'Value of "this" must be of type TextEncoder'
}));
const inspectFn = TextEncoder.prototype[inspect];
const encodeFn = TextEncoder.prototype.encode;
const encodingGetter =
Object.getOwnPropertyDescriptor(TextEncoder.prototype, 'encoding').get;

const instance = new TextEncoder();

const expectedError = {
code: 'ERR_INVALID_THIS',
type: TypeError,
message: 'Value of "this" must be of type TextEncoder'
};

assert.doesNotThrow(() => inspectFn.call(instance, Infinity, {}));
assert.doesNotThrow(() => encodeFn.call(instance));
assert.doesNotThrow(() => encodingGetter.call(instance));

const invalidThisArgs = [{}, [], true, 1, '', new TextDecoder()];
invalidThisArgs.forEach((i) => {
common.expectsError(() => inspectFn.call(i, Infinity, {}), expectedError);
common.expectsError(() => encodeFn.call(i), expectedError);
common.expectsError(() => encodingGetter.call(i), expectedError);
});
}