summaryrefslogtreecommitdiff
path: root/test/defs.test.js
diff options
context:
space:
mode:
authordaurnimator <quae@daurnimator.com>2018-04-20 21:16:24 +1000
committerdaurnimator <quae@daurnimator.com>2018-04-20 21:16:24 +1000
commita260f381d30e036b39078fee0295e539067c10a3 (patch)
tree4f3b2ecb264ec8dd29cac74d23223d15d297eb8e /test/defs.test.js
parentfe9b43374c2e8dc28df0ce460aefaf50b933aaa7 (diff)
parent9f5d4d35de0b88334bba7c11a9e960a19ee0740b (diff)
downloadfengari-a260f381d30e036b39078fee0295e539067c10a3.tar.gz
fengari-a260f381d30e036b39078fee0295e539067c10a3.tar.bz2
fengari-a260f381d30e036b39078fee0295e539067c10a3.zip
Merge branch 'test-with-jest'
Diffstat (limited to 'test/defs.test.js')
-rw-r--r--test/defs.test.js94
1 files changed, 94 insertions, 0 deletions
diff --git a/test/defs.test.js b/test/defs.test.js
new file mode 100644
index 0000000..4cc0ad9
--- /dev/null
+++ b/test/defs.test.js
@@ -0,0 +1,94 @@
+const defs = require('../src/defs.js');
+
+
+const unicode_tests = [
+ {
+ description: "Convert normal ascii string",
+ literal: "foo",
+ byte_array: defs.luastring_of("f".charCodeAt(0), "o".charCodeAt(0), "o".charCodeAt(0))
+ },
+ {
+ description: "Convert ascii string containing null byte",
+ literal: "fo\0o",
+ byte_array: defs.luastring_of("f".charCodeAt(0), "o".charCodeAt(0), 0, "o".charCodeAt(0))
+ },
+ {
+ description: "Convert string with BMP unicode chars",
+ literal: "Café",
+ byte_array: defs.luastring_of(67, 97, 102, 195, 169)
+ },
+ {
+ description: "Convert string with codepoint in PUA (U+E000 to U+F8FF)",
+ literal: "",
+ byte_array: defs.luastring_of(239, 163, 191)
+ },
+ {
+ description: "Convert string with surrogate pair",
+ literal: "❤️🍾",
+ byte_array: defs.luastring_of(226, 157, 164, 239, 184, 143, 240, 159, 141, 190)
+ },
+ {
+ description: "Convert string with broken surrogate pair",
+ literal: "\uD800a",
+ byte_array: defs.luastring_of(237, 160, 128, 97)
+ },
+ {
+ description: "Convert string with broken surrogate pair at end of string",
+ literal: "\uD823",
+ byte_array: defs.luastring_of(237, 160, 163)
+ }
+];
+
+describe('to_luastring', () => {
+ unicode_tests.forEach((v) => {
+ test(v.description, () => {
+ expect(defs.to_luastring(v.literal)).toEqual(v.byte_array);
+ });
+ });
+});
+
+describe('to_jsstring', () => {
+ unicode_tests.forEach((v) => {
+ test(v.description, () => {
+ expect(defs.to_jsstring(v.byte_array)).toEqual(v.literal);
+ });
+ });
+});
+
+
+describe('to_jsstring fails on invalid unicode', () => {
+ test("non-utf8 char", () => {
+ expect(() => defs.to_jsstring(defs.luastring_of(165)))
+ .toThrowError(RangeError);
+ });
+
+ test("invalid continuation byte", () => {
+ expect(() => defs.to_jsstring(defs.luastring_of(208, 60)))
+ .toThrowError(RangeError);
+ });
+
+ test("invalid continuation byte", () => {
+ expect(() => defs.to_jsstring(defs.luastring_of(225, 60, 145)))
+ .toThrowError(RangeError);
+ });
+
+ test("invalid continuation byte", () => {
+ expect(() => defs.to_jsstring(defs.luastring_of(225, 145, 60)))
+ .toThrowError(RangeError);
+ });
+
+ test("invalid continuation byte", () => {
+ expect(() => defs.to_jsstring(defs.luastring_of(242, 60, 145, 145)))
+ .toThrowError(RangeError);
+ });
+
+ test("invalid continuation byte", () => {
+ expect(() => defs.to_jsstring(defs.luastring_of(242, 145, 60, 145)))
+ .toThrowError(RangeError);
+ });
+
+ test("invalid continuation byte", () => {
+ expect(() => defs.to_jsstring(defs.luastring_of(242, 145, 145, 60)))
+ .toThrowError(RangeError);
+ });
+});