diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/browser-driver.js | 54 | ||||
-rw-r--r-- | tests/driver.html | 56 | ||||
-rw-r--r-- | tests/driver.js | 34 | ||||
-rw-r--r-- | tests/node-driver.js | 52 | ||||
-rw-r--r-- | tests/papo.js | 19 | ||||
-rw-r--r-- | tests/sw.js | 26 |
6 files changed, 241 insertions, 0 deletions
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 => `<span class="red" >${escape(s)}</span>`; +const green = s => `<span class="green" >${escape(s)}</span>`; +const yellow = s => `<span class="yellow">${escape(s)}</span>`; + +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 @@ +<!DOCTYPE html> +<html lang="en"> + <head> + <meta charset="UTF-8" /> + <meta name="viewport" content="width=device-width, initial-scale=1" /> + <link rel="shortcut icon" type="image/svg+xml" href="../src/content/img/favicon.svg" /> + <title>Papo | webapp tests</title> + <style> + :root { + --color-fg: black; + --color-bg: white; + --color-red: red; + --color-green: green; + --color-yellow: goldenrod; + } + + @media(prefers-color-scheme: dark) { + :root { + --color-fg: white; + --color-bg: black; + --color-red: orangered; + --color-green: lightgreen; + --color-yellow: yellow; + } + } + + body { + color: var(--color-fg); + background-color: var(--color-bg); + margin: 0 auto; + max-width: 800px; + } + + .red { + color: var(--color-red); + } + + .green { + color: var(--color-green); + } + + .yellow { + color: var(--color-yellow); + } + </style> + <script type="module"> + import { main } from "./browser-driver.js"; + main(); + </script> + </head> + <body> + <main> + <pre id="panel"></pre> + </main> + </body> +</html> 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, +]; |