From 5b5f5b1b322cd4024694d5768f888990594cbd93 Mon Sep 17 00:00:00 2001 From: Sho Miyamoto Date: Sat, 13 Jan 2018 22:05:26 +0900 Subject: [PATCH] test: add assertions for TextEncoder/Decoder PR-URL: https://github.com/nodejs/node/pull/18132 Reviewed-By: James M Snell Reviewed-By: Luigi Pinca Reviewed-By: Tiancheng "Timothy" Gu Reviewed-By: Colin Ihrig --- .../test-whatwg-encoding-textdecoder.js | 41 +++++++++++++------ .../test-whatwg-encoding-textencoder.js | 34 +++++++++------ 2 files changed, 51 insertions(+), 24 deletions(-) diff --git a/test/parallel/test-whatwg-encoding-textdecoder.js b/test/parallel/test-whatwg-encoding-textdecoder.js index c4f919289adf2f..5f9499930a07a0 100644 --- a/test/parallel/test-whatwg-encoding-textdecoder.js +++ b/test/parallel/test-whatwg-encoding-textdecoder.js @@ -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); }); } diff --git a/test/parallel/test-whatwg-encoding-textencoder.js b/test/parallel/test-whatwg-encoding-textencoder.js index fba5445c81941c..4096a02432e900 100644 --- a/test/parallel/test-whatwg-encoding-textencoder.js +++ b/test/parallel/test-whatwg-encoding-textencoder.js @@ -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); }); }