summaryrefslogtreecommitdiff
path: root/tests/node_compat
diff options
context:
space:
mode:
authorSatya Rohith <me@satyarohith.com>2024-10-02 13:53:14 +0530
committerGitHub <noreply@github.com>2024-10-02 08:23:14 +0000
commit32c12787361b65bbc55a7b9c1fe43689cb0a8b98 (patch)
tree047f47b5146b9192d74bd8ef1d9af9d183b7aad1 /tests/node_compat
parent620e6b43a66c2af44ae4aea62417af408309f61c (diff)
feat(ext/node): buffer.transcode() (#25972)
Closes https://github.com/denoland/deno/issues/25911
Diffstat (limited to 'tests/node_compat')
-rw-r--r--tests/node_compat/config.jsonc1
-rw-r--r--tests/node_compat/runner/TODO.md1
-rw-r--r--tests/node_compat/test/parallel/test-icu-transcode.js97
3 files changed, 98 insertions, 1 deletions
diff --git a/tests/node_compat/config.jsonc b/tests/node_compat/config.jsonc
index 2f94fa2f2..bc9bf476b 100644
--- a/tests/node_compat/config.jsonc
+++ b/tests/node_compat/config.jsonc
@@ -406,6 +406,7 @@
"test-http-outgoing-settimeout.js",
"test-http-url.parse-https.request.js",
"test-http-url.parse-only-support-http-https-protocol.js",
+ "test-icu-transcode.js",
"test-net-access-byteswritten.js",
"test-net-better-error-messages-listen-path.js",
"test-net-better-error-messages-path.js",
diff --git a/tests/node_compat/runner/TODO.md b/tests/node_compat/runner/TODO.md
index 24e827182..99258f5a5 100644
--- a/tests/node_compat/runner/TODO.md
+++ b/tests/node_compat/runner/TODO.md
@@ -1632,7 +1632,6 @@ NOTE: This file should not be manually edited. Please edit `tests/node_compat/co
- [parallel/test-icu-minimum-version.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-icu-minimum-version.js)
- [parallel/test-icu-punycode.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-icu-punycode.js)
- [parallel/test-icu-stringwidth.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-icu-stringwidth.js)
-- [parallel/test-icu-transcode.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-icu-transcode.js)
- [parallel/test-inspect-address-in-use.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-inspect-address-in-use.js)
- [parallel/test-inspect-async-hook-setup-at-inspect.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-inspect-async-hook-setup-at-inspect.js)
- [parallel/test-inspect-publish-uid.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-inspect-publish-uid.js)
diff --git a/tests/node_compat/test/parallel/test-icu-transcode.js b/tests/node_compat/test/parallel/test-icu-transcode.js
new file mode 100644
index 000000000..1f5aeb535
--- /dev/null
+++ b/tests/node_compat/test/parallel/test-icu-transcode.js
@@ -0,0 +1,97 @@
+// deno-fmt-ignore-file
+// deno-lint-ignore-file
+
+// Copyright Joyent and Node contributors. All rights reserved. MIT license.
+// Taken from Node 18.12.1
+// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually.
+
+'use strict';
+
+const common = require('../common');
+
+if (!common.hasIntl)
+ common.skip('missing Intl');
+
+const buffer = require('buffer');
+const assert = require('assert');
+const orig = Buffer.from('těst ☕', 'utf8');
+
+// Test Transcoding
+const tests = {
+ 'latin1': [0x74, 0x3f, 0x73, 0x74, 0x20, 0x3f],
+ 'ascii': [0x74, 0x3f, 0x73, 0x74, 0x20, 0x3f],
+ 'ucs2': [0x74, 0x00, 0x1b, 0x01, 0x73,
+ 0x00, 0x74, 0x00, 0x20, 0x00,
+ 0x15, 0x26]
+};
+
+for (const test in tests) {
+ const dest = buffer.transcode(orig, 'utf8', test);
+ assert.strictEqual(dest.length, tests[test].length, `utf8->${test} length`);
+ for (let n = 0; n < tests[test].length; n++)
+ assert.strictEqual(dest[n], tests[test][n], `utf8->${test} char ${n}`);
+}
+
+{
+ const dest = buffer.transcode(Buffer.from(tests.ucs2), 'ucs2', 'utf8');
+ assert.strictEqual(dest.toString(), orig.toString());
+}
+
+{
+ const utf8 = Buffer.from('€'.repeat(4000), 'utf8');
+ const ucs2 = Buffer.from('€'.repeat(4000), 'ucs2');
+ const utf8_to_ucs2 = buffer.transcode(utf8, 'utf8', 'ucs2');
+ const ucs2_to_utf8 = buffer.transcode(ucs2, 'ucs2', 'utf8');
+ assert.deepStrictEqual(utf8, ucs2_to_utf8);
+ assert.deepStrictEqual(ucs2, utf8_to_ucs2);
+ assert.strictEqual(ucs2_to_utf8.toString('utf8'),
+ utf8_to_ucs2.toString('ucs2'));
+}
+
+assert.throws(
+ () => buffer.transcode(null, 'utf8', 'ascii'),
+ {
+ name: 'TypeError',
+ code: 'ERR_INVALID_ARG_TYPE',
+ message: 'The "source" argument must be an instance of Buffer ' +
+ 'or Uint8Array. Received null'
+ }
+);
+
+assert.throws(
+ () => buffer.transcode(Buffer.from('a'), 'b', 'utf8'),
+ /^Error: Unable to transcode Buffer \[U_ILLEGAL_ARGUMENT_ERROR\]/
+);
+
+assert.throws(
+ () => buffer.transcode(Buffer.from('a'), 'uf8', 'b'),
+ /^Error: Unable to transcode Buffer \[U_ILLEGAL_ARGUMENT_ERROR\]$/
+);
+
+assert.deepStrictEqual(
+ buffer.transcode(Buffer.from('hi', 'ascii'), 'ascii', 'utf16le'),
+ Buffer.from('hi', 'utf16le'));
+assert.deepStrictEqual(
+ buffer.transcode(Buffer.from('hi', 'latin1'), 'latin1', 'utf16le'),
+ Buffer.from('hi', 'utf16le'));
+assert.deepStrictEqual(
+ buffer.transcode(Buffer.from('hä', 'latin1'), 'latin1', 'utf16le'),
+ Buffer.from('hä', 'utf16le'));
+
+// Test that Uint8Array arguments are okay.
+{
+ const uint8array = new Uint8Array([...Buffer.from('hä', 'latin1')]);
+ assert.deepStrictEqual(
+ buffer.transcode(uint8array, 'latin1', 'utf16le'),
+ Buffer.from('hä', 'utf16le'));
+}
+
+{
+ const dest = buffer.transcode(new Uint8Array(), 'utf8', 'latin1');
+ assert.strictEqual(dest.length, 0);
+}
+
+// Test that it doesn't crash
+{
+ buffer.transcode(new buffer.SlowBuffer(1), 'utf16le', 'ucs2');
+}