diff options
author | 2022-08-05 19:42:52 +0200 | |
---|---|---|
committer | 2022-08-05 10:42:52 -0700 | |
commit | 1cb8f0fa73bb9c17a06305ac3fa06b4268494d2a (patch) | |
tree | 95f28d4e558a8771ca649e28cc2fbdd6452a8059 /test/bun.js | |
parent | 5bca8a1d47ca6643ecafbd9d0f5ad624540154b0 (diff) | |
download | bun-1cb8f0fa73bb9c17a06305ac3fa06b4268494d2a.tar.gz bun-1cb8f0fa73bb9c17a06305ac3fa06b4268494d2a.tar.zst bun-1cb8f0fa73bb9c17a06305ac3fa06b4268494d2a.zip |
feat(util): support for util.TextDecoder (#990)
* build:(landing) automated website build
* Revert "build:(landing) automated website build"
This reverts commit ddee8485fd8b76160962c410b885e17aaff95b4e.
* feat(util): support for util.TextDecoder
* tests(util): add TextDecoder
* tests: seperate text-decoder
Co-authored-by: xHyroM <xHyroM@users.noreply.github.com>
Diffstat (limited to 'test/bun.js')
-rw-r--r-- | test/bun.js/text-decoder.test.js | 69 | ||||
-rw-r--r-- | test/bun.js/text-encoder.test.js | 55 | ||||
-rw-r--r-- | test/bun.js/util.test.js | 8 |
3 files changed, 77 insertions, 55 deletions
diff --git a/test/bun.js/text-decoder.test.js b/test/bun.js/text-decoder.test.js new file mode 100644 index 000000000..f132fa7da --- /dev/null +++ b/test/bun.js/text-decoder.test.js @@ -0,0 +1,69 @@ +import { expect, it, describe } from "bun:test"; +import { gc as gcTrace } from "./gc"; + +const getByteLength = (str) => { + // returns the byte length of an utf8 string + var s = str.length; + for (var i = str.length - 1; i >= 0; i--) { + var code = str.charCodeAt(i); + if (code > 0x7f && code <= 0x7ff) s++; + else if (code > 0x7ff && code <= 0xffff) s += 2; + if (code >= 0xdc00 && code <= 0xdfff) i--; //trail surrogate + } + return s; +}; + +describe("TextDecoder", () => { + it("should decode ascii text", () => { + const decoder = new TextDecoder("latin1"); + gcTrace(true); + expect(decoder.encoding).toBe("windows-1252"); + gcTrace(true); + expect(decoder.decode(new Uint8Array([0x41, 0x42, 0x43]))).toBe("ABC"); + gcTrace(true); + const result = [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33]; + gcTrace(true); + expect(decoder.decode(Uint8Array.from(result))).toBe( + String.fromCharCode(...result) + ); + gcTrace(true); + }); + + it("should decode unicode text", () => { + const decoder = new TextDecoder(); + gcTrace(true); + var text = `❤️ Red Heart`; + + const bytes = [ + 226, 157, 164, 239, 184, 143, 32, 82, 101, 100, 32, 72, 101, 97, 114, 116, + ]; + const decoded = decoder.decode(Uint8Array.from(bytes)); + expect(decoder.encoding).toBe("utf-8"); + + gcTrace(true); + + for (let i = 0; i < text.length; i++) { + expect(decoded.charCodeAt(i)).toBe(text.charCodeAt(i)); + } + expect(decoded).toHaveLength(text.length); + gcTrace(true); + }); + + it("should decode unicode text with multiple consecutive emoji", () => { + const decoder = new TextDecoder(); + const encoder = new TextEncoder(); + gcTrace(true); + var text = `❤️❤️❤️❤️❤️❤️ Red Heart`; + + text += ` ✨ Sparkles 🔥 Fire 😀 😃 😄 😁 😆 😅 😂 🤣 🥲 ☺️ 😊 😇 🙂 🙃 😉 😌 😍 🥰 😘 😗 😙 😚 😋 😛 😝 😜 🤪 🤨 🧐 🤓 😎 🥸 🤩 🥳 😏 😒 😞 😔 😟 😕 🙁 ☹️ 😣 😖 😫 😩 🥺 😢 😭 😤 😠 😡 🤬 🤯 😳 🥵 🥶 😱 😨 😰`; + gcTrace(true); + expect(decoder.decode(encoder.encode(text))).toBe(text); + gcTrace(true); + const bytes = new Uint8Array(getByteLength(text) * 8); + gcTrace(true); + const amount = encoder.encodeInto(text, bytes); + gcTrace(true); + expect(decoder.decode(bytes.subarray(0, amount.written))).toBe(text); + gcTrace(true); + }); +}); diff --git a/test/bun.js/text-encoder.test.js b/test/bun.js/text-encoder.test.js index 5f8778bde..2c8f9acf4 100644 --- a/test/bun.js/text-encoder.test.js +++ b/test/bun.js/text-encoder.test.js @@ -13,61 +13,6 @@ const getByteLength = (str) => { return s; }; -describe("TextDecoder", () => { - it("should decode ascii text", () => { - const decoder = new TextDecoder("latin1"); - gcTrace(true); - expect(decoder.encoding).toBe("windows-1252"); - gcTrace(true); - expect(decoder.decode(new Uint8Array([0x41, 0x42, 0x43]))).toBe("ABC"); - gcTrace(true); - const result = [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33]; - gcTrace(true); - expect(decoder.decode(Uint8Array.from(result))).toBe( - String.fromCharCode(...result) - ); - gcTrace(true); - }); - - it("should decode unicode text", () => { - const decoder = new TextDecoder(); - gcTrace(true); - var text = `❤️ Red Heart`; - - const bytes = [ - 226, 157, 164, 239, 184, 143, 32, 82, 101, 100, 32, 72, 101, 97, 114, 116, - ]; - const decoded = decoder.decode(Uint8Array.from(bytes)); - expect(decoder.encoding).toBe("utf-8"); - - gcTrace(true); - - for (let i = 0; i < text.length; i++) { - expect(decoded.charCodeAt(i)).toBe(text.charCodeAt(i)); - } - expect(decoded).toHaveLength(text.length); - gcTrace(true); - }); - - it("should decode unicode text with multiple consecutive emoji", () => { - const decoder = new TextDecoder(); - const encoder = new TextEncoder(); - gcTrace(true); - var text = `❤️❤️❤️❤️❤️❤️ Red Heart`; - - text += ` ✨ Sparkles 🔥 Fire 😀 😃 😄 😁 😆 😅 😂 🤣 🥲 ☺️ 😊 😇 🙂 🙃 😉 😌 😍 🥰 😘 😗 😙 😚 😋 😛 😝 😜 🤪 🤨 🧐 🤓 😎 🥸 🤩 🥳 😏 😒 😞 😔 😟 😕 🙁 ☹️ 😣 😖 😫 😩 🥺 😢 😭 😤 😠 😡 🤬 🤯 😳 🥵 🥶 😱 😨 😰`; - gcTrace(true); - expect(decoder.decode(encoder.encode(text))).toBe(text); - gcTrace(true); - const bytes = new Uint8Array(getByteLength(text) * 8); - gcTrace(true); - const amount = encoder.encodeInto(text, bytes); - gcTrace(true); - expect(decoder.decode(bytes.subarray(0, amount.written))).toBe(text); - gcTrace(true); - }); -}); - describe("TextEncoder", () => { it("should encode latin1 text with non-ascii latin1 characters", () => { var text = "H©ell©o Wor©ld!"; diff --git a/test/bun.js/util.test.js b/test/bun.js/util.test.js index 481eea2ed..15d3cd221 100644 --- a/test/bun.js/util.test.js +++ b/test/bun.js/util.test.js @@ -263,4 +263,12 @@ describe("util", () => { expect(util.TextEncoder === globalThis.TextEncoder).toBe(true); }); }); + + describe("TextDecoder", () => { + // test/bun.js/text-decoder.test.js covers test cases for TextDecoder + // here we test only if we use the same via util.TextDecoder + it("is same as global TextDecoder", () => { + expect(util.TextDecoder === globalThis.TextDecoder).toBe(true); + }); + }); }); |