From c36bf8e3577da31cf6d575879c7e92d3e9c7e4f1 Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Fri, 23 Feb 2024 06:05:19 -0300 Subject: Big cleanup - delete all SQLite Node-API code: we'll use the C++ one instead; - implement hero.mjs, with tests! - use ESM all over. --- tests/js/escape.mjs | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 tests/js/escape.mjs (limited to 'tests/js/escape.mjs') diff --git a/tests/js/escape.mjs b/tests/js/escape.mjs new file mode 100644 index 0000000..2cad16a --- /dev/null +++ b/tests/js/escape.mjs @@ -0,0 +1,62 @@ +import assert from "node:assert/strict"; +import { runTests } from "../runner.mjs"; +import { escape } from "../../src/escape.mjs"; + +const test_escape = t => { + t.start("escape()"); + + t.test("numbers", () => { + assert.equal(escape(0), "0"); + assert.equal(escape(42), "42"); + assert.equal(escape(-1), "-1"); + }); + + t.test("object", () => { + assert.equal(escape({}), "[object Object]"); + assert.equal(escape({ k: "v" }), "[object Object]"); + }); + + t.test("string with special chars", () => { + assert.strictEqual(escape(`"`), """); + assert.strictEqual(escape(`"bar`), ""bar"); + assert.strictEqual(escape(`foo"`), "foo""); + assert.strictEqual(escape(`foo"bar`), "foo"bar"); + assert.strictEqual(escape(`foo""bar`), "foo""bar"); + + assert.strictEqual(escape("&"), "&"); + assert.strictEqual(escape("&bar"), "&bar"); + assert.strictEqual(escape("foo&"), "foo&"); + assert.strictEqual(escape("foo&bar"), "foo&bar"); + assert.strictEqual(escape("foo&&bar"), "foo&&bar"); + + assert.strictEqual(escape("'"), "'"); + assert.strictEqual(escape("'bar"), "'bar"); + assert.strictEqual(escape("foo'"), "foo'"); + assert.strictEqual(escape("foo'bar"), "foo'bar"); + assert.strictEqual(escape("foo''bar"), "foo''bar"); + + assert.strictEqual(escape("<"), "<"); + assert.strictEqual(escape(""), ">"); + assert.strictEqual(escape(">bar"), ">bar"); + assert.strictEqual(escape("foo>"), "foo>"); + assert.strictEqual(escape("foo>bar"), "foo>bar"); + assert.strictEqual(escape("foo>>bar"), "foo>>bar"); + }); + + t.test("the combination of all special characters", () => { + assert.strictEqual( + escape(`foo, "bar", 'baz' & `), + "foo, "bar", 'baz' & <quux>", + ); + }); +}; + + +await runTests([ + test_escape, +]); -- cgit v1.2.3