Skip to content

Commit

Permalink
test: add assertions for TextEncoder/Decoder
Browse files Browse the repository at this point in the history
PR-URL: #18132
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information
Sho Miyamoto authored and evanlucas committed Jan 30, 2018
1 parent aee8be9 commit 2b13fda
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 24 deletions.
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 @@ -91,18 +91,35 @@ if (common.hasIntl) {
}

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

[{}, [], true, 1, '', new TextEncoder()].forEach((i) => {
common.expectsError(() => fn.call(i, Infinity, {}),
{
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) => {
common.expectsError(() => fn.call(i, Infinity, {}),
{
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);
});
}

0 comments on commit 2b13fda

Please sign in to comment.