From fd7bdaca33c7e62d76e8ef0dfe975e5ff20490cb Mon Sep 17 00:00:00 2001 From: daurnimator Date: Sun, 12 Nov 2017 14:41:25 +1100 Subject: tests/defs.js: Also test to_jsstring --- tests/defs.js | 88 ++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 48 insertions(+), 40 deletions(-) diff --git a/tests/defs.js b/tests/defs.js index 8c86588..07e863b 100644 --- a/tests/defs.js +++ b/tests/defs.js @@ -3,48 +3,56 @@ const test = require('tape'); global.WEB = false; const defs = require('../src/defs.js'); -test('to_luastring', function (t) { - t.deepEqual( - defs.to_luastring("foo"), - ["f".charCodeAt(0), "o".charCodeAt(0), "o".charCodeAt(0)], - "Convert normal ascii string" - ); - - t.deepEqual( - defs.to_luastring("fo\0o"), - ["f".charCodeAt(0), "o".charCodeAt(0), 0, "o".charCodeAt(0)], - "Convert ascii string containing null byte" - ); - - t.deepEqual( - defs.to_luastring("Café"), - [67, 97, 102, 195, 169], - "Convert string with BMP unicode chars" - ); +const unicode_tests = [ + { + description: "Convert normal ascii string", + literal: "foo", + byte_array: ["f".charCodeAt(0), "o".charCodeAt(0), "o".charCodeAt(0)] + }, + { + description: "Convert ascii string containing null byte", + literal: "fo\0o", + byte_array: ["f".charCodeAt(0), "o".charCodeAt(0), 0, "o".charCodeAt(0)] + }, + { + description: "Convert string with BMP unicode chars", + literal: "Café", + byte_array: [67, 97, 102, 195, 169] + }, + { + description: "Convert string with codepoint in PUA (U+E000 to U+F8FF)", + literal: "", + byte_array: [239, 163, 191] + }, + { + description: "Convert string with surrogate pair", + literal: "❤️🍾", + byte_array: [226, 157, 164, 239, 184, 143, 240, 159, 141, 190] + }, + { + description: "Convert string with broken surrogate pair", + literal: "\uD800a", + byte_array: [237, 160, 128, 97] + }, + { + description: "Convert string with broken surrogate pair at end of string", + literal: "\uD823", + byte_array: [237, 160, 163] + } +]; - t.deepEqual( - defs.to_luastring(""), - [239, 163, 191], - "Convert string with codepoint in PUA (U+E000 to U+F8FF)" - ); - - t.deepEqual( - defs.to_luastring("❤️🍾"), - [226, 157, 164, 239, 184, 143, 240, 159, 141, 190], - "Convert string with surrogate pair" - ); +test('to_luastring', function (t) { + t.plan(unicode_tests.length); - t.deepEqual( - defs.to_luastring("\uD800a"), - [237, 160, 128, 97], - "Convert string with broken surrogate pair" - ); + unicode_tests.forEach(function(v) { + t.deepEqual(defs.to_luastring(v.literal), v.byte_array, v.description); + }); +}); - t.deepEqual( - defs.to_luastring("\uD823"), - [237, 160, 163], - "Convert string with broken surrogate pair at end of string" - ); +test('to_jsstring', function (t) { + t.plan(unicode_tests.length); - t.end(); + unicode_tests.forEach(function(v) { + t.deepEqual(defs.to_jsstring(v.byte_array), v.literal, v.description); + }); }); -- cgit v1.2.3-70-g09d2