diff options
author | Joyee Cheung <joyeec9h3@gmail.com> | 2018-12-20 14:35:58 +0800 |
---|---|---|
committer | Joyee Cheung <joyeec9h3@gmail.com> | 2018-12-29 19:20:24 +0800 |
commit | 05ee814f08f570268547a9c0141c87de38c0f389 (patch) | |
tree | b8eef7636eec89613596b25dde516663977d4006 /test/parallel/test-whatwg-encoding-custom-interop.js | |
parent | 2b91c2b5ee01114e8ea435cb6e8211f9bf2f2f0a (diff) | |
download | android-node-v8-05ee814f08f570268547a9c0141c87de38c0f389.tar.gz android-node-v8-05ee814f08f570268547a9c0141c87de38c0f389.tar.bz2 android-node-v8-05ee814f08f570268547a9c0141c87de38c0f389.zip |
test: rename custom encoding tests that cannot be replaced by WPT
PR-URL: https://github.com/nodejs/node/pull/25155
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'test/parallel/test-whatwg-encoding-custom-interop.js')
-rw-r--r-- | test/parallel/test-whatwg-encoding-custom-interop.js | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/test/parallel/test-whatwg-encoding-custom-interop.js b/test/parallel/test-whatwg-encoding-custom-interop.js new file mode 100644 index 0000000000..0ea732a03d --- /dev/null +++ b/test/parallel/test-whatwg-encoding-custom-interop.js @@ -0,0 +1,64 @@ +// Flags: --expose-internals + +// This tests interoperability between TextEncoder and TextDecoder with +// Node.js util.inspect and Buffer APIs + +'use strict'; + +const common = require('../common'); + +const assert = require('assert'); +const { customInspectSymbol: inspect } = require('internal/util'); + +const encoded = Buffer.from([0xef, 0xbb, 0xbf, 0x74, 0x65, + 0x73, 0x74, 0xe2, 0x82, 0xac]); + +// Make Sure TextEncoder exists +assert(TextEncoder); + +// Test TextEncoder +{ + const enc = new TextEncoder(); + assert.strictEqual(enc.encoding, 'utf-8'); + assert(enc); + const buf = enc.encode('\ufefftest€'); + assert.strictEqual(Buffer.compare(buf, encoded), 0); +} + +{ + const enc = new TextEncoder(); + const buf = enc.encode(); + assert.strictEqual(buf.length, 0); +} + +{ + const enc = new TextEncoder(); + const buf = enc.encode(undefined); + assert.strictEqual(buf.length, 0); +} + +{ + 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' + }; + + inspectFn.call(instance, Infinity, {}); + encodeFn.call(instance); + 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); + }); +} |