From d45cf0d708bc739b6478e741c964da2e64e3d874 Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Mon, 21 Oct 2024 07:39:46 -0300 Subject: Init work on offline support and tests --- tests/browser-driver.js | 54 +++++++++++++++++++++++++++++++++++++++++++++++ tests/driver.html | 56 +++++++++++++++++++++++++++++++++++++++++++++++++ tests/driver.js | 34 ++++++++++++++++++++++++++++++ tests/node-driver.js | 52 +++++++++++++++++++++++++++++++++++++++++++++ tests/papo.js | 19 +++++++++++++++++ tests/sw.js | 26 +++++++++++++++++++++++ 6 files changed, 241 insertions(+) create mode 100644 tests/browser-driver.js create mode 100644 tests/driver.html create mode 100644 tests/driver.js create mode 100644 tests/node-driver.js create mode 100644 tests/papo.js create mode 100644 tests/sw.js (limited to 'tests') diff --git a/tests/browser-driver.js b/tests/browser-driver.js new file mode 100644 index 0000000..529d824 --- /dev/null +++ b/tests/browser-driver.js @@ -0,0 +1,54 @@ +import { runTests } from "./driver.js"; + + + +class AssertionError extends Error {} + + + +const panel = document.querySelector("#panel"); +const err = s => panel.innerHTML += s; + +const assert = { + equal: (given, expected) => { + if (given !== expected) { + throw new AssertionError( + `given ${given}; expected ${expected}` + ); + } + }, + deepEqual: (x, y) => { + }, +}; + +// FIXME +const escape = s => s; + +const red = s => `${escape(s)}`; +const green = s => `${escape(s)}`; +const yellow = s => `${escape(s)}`; + +const conf = { + err, + assert, + colors: { + red, + green, + yellow, + }, +}; + +const TEST_PATHS = [ + "tests/papo.js", + "tests/sw.js", +]; +export const main = (paths = TEST_PATHS) => { + document.addEventListener("DOMContentLoaded", async _ => { + for (const path of paths) { + const module = await import("../" + path); + conf.err(path + "\n"); + await runTests(conf, module.allTests); + conf.err("\n"); + } + }); +}; diff --git a/tests/driver.html b/tests/driver.html new file mode 100644 index 0000000..e4508ec --- /dev/null +++ b/tests/driver.html @@ -0,0 +1,56 @@ + + + + + + + Papo | webapp tests + + + + +
+

+    
+ + diff --git a/tests/driver.js b/tests/driver.js new file mode 100644 index 0000000..d273c5d --- /dev/null +++ b/tests/driver.js @@ -0,0 +1,34 @@ +const isAsync = f => + typeof f.then === 'function' && + f[Symbol.toStringTag] === 'Promise'; + +const t = ({ colors, err, assert }) => ({ + assert, + tap: x => { + err(`tap: ${x}\n`); + return x; + }, + start: msg => { + err(`${msg}:\n`); + }, + test: async (msg, fn) => { + err(`${colors.yellow("testing")}: ${msg}... `); + try { + if (isAsync(fn)) { + await fn(); + } else { + fn(); + } + } catch (e) { + err(`${colors.red("ERR")}.\n`); + throw e; + } + err(`${colors.green("OK")}.\n`); + }, +}); + +export const runTests = async (conf, tests) => { + for (const testFn of tests) { + await testFn(t(conf)); + } +}; diff --git a/tests/node-driver.js b/tests/node-driver.js new file mode 100644 index 0000000..3393dca --- /dev/null +++ b/tests/node-driver.js @@ -0,0 +1,52 @@ +import assert from "node:assert/strict"; +import process from "node:process"; + + + +const red = s => `\x1b[31m${s}\x1b[0m`; +const green = s => `\x1b[32m${s}\x1b[0m`; +const yellow = s => `\x1b[33m${s}\x1b[0m`; + +const isAsync = f => + typeof f.then === 'function' && + f[Symbol.toStringTag] === 'Promise'; + +const t = { + assert, + tap: x => { + process.stderr.write(`tap: ${x}\n`); + return x; + }, + start: msg => { + process.stderr.write(`${msg}:\n`); + }, + test: async (msg, fn) => { + process.stderr.write(`${yellow("testing")}: ${msg}... `); + try { + if (isAsync(fn)) { + await fn(); + } else { + fn(); + } + } catch (e) { + process.stderr.write(`${red("ERR")}.\n}`); + throw e; + } + process.stderr.write(`${green("OK")}.\n`); + }, +}; + +const runTests = async tests => { + for (const testFn of tests) { + await testFn(t); + } +}; + +const main = async (path = process.argv[2]) => { + const module = await import("../" + path); + await runTests(module.allTests); +}; + + + +await main(); diff --git a/tests/papo.js b/tests/papo.js new file mode 100644 index 0000000..188ca67 --- /dev/null +++ b/tests/papo.js @@ -0,0 +1,19 @@ +import { + f1, +} from "../src/content/papo.exported.js"; + + + +const test_f1 = async t => { + t.start("f1()"); + t.test("addition", () => { + t.assert.equal(f1(1), 2); + // t.assert.equal(f1(1), 3); + }); +}; + + + +export const allTests = [ + test_f1, +]; diff --git a/tests/sw.js b/tests/sw.js new file mode 100644 index 0000000..cff37bc --- /dev/null +++ b/tests/sw.js @@ -0,0 +1,26 @@ +import { + leafValues, +} from "../src/content/sw.exported.js"; + + + +const test_leafValues = async t => { + t.start("leafValues()"); + + t.test("noop on empty object", () => { + t.assert.deepEqual(leafValues({}), []); + }); + + t.test("flat array for flat tree", () => { + }); + + t.test("array of empty arrays for leafless tree", () => { + t.assert.deepEqual(leafValues({ a: {} }), [[]]); + }); +}; + + + +export const allTests = [ + test_leafValues, +]; -- cgit v1.2.3