Skip to content

Commit

Permalink
tests: added test-browser static files
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed May 6, 2023
1 parent 25ab579 commit 96d5e7b
Show file tree
Hide file tree
Showing 7 changed files with 21,359 additions and 14 deletions.
107 changes: 107 additions & 0 deletions misc/test-browser/assert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@

function throwError(message, info) {
const error = new Error(`AssertionError: ${ message }`);
error.code = "ERR_ASSERTION";
for (const key of info) { error[key] = info[key]; }
throw error;
}

export function equal(actual, expected, reason) {
if (actual != expected) {
if (reason == null) { reason = `${ actual } == ${ expected }`; }

throwError(reason, { actual, expected, operator: "==" });
}
}

function isDeepEqual(actual, expected, memo) {
if (actual === expected) {
return true;
}

// One or both things aren't objects
if (actual === null || typeof(expected) !== 'object') {
if (expected === null || typeof(expected) !== 'object') {
return actual == expected;
}

return false;

} else if (expected === null || typeof(expected) !== 'object') {
return false;
}

if (Array.isArray(actual)) {
if (!Array.isArray(expected) || actual.length !== expected.length) {
return false;
}

for (let i = 0; i < actual.length; i++) {
if (!isDeepEqual(actual[i], expected[i])) { return false; }
}

return true;
}

// Object
const keysActual = Object.keys(actual).sort(), keysExpected = Object.keys(expected).sort();
if (!isDeepEqual(keysActual, keysExpected)) { return false; }
for (const key of keysActual) {
if (!isDeepEqual(actual[key], expected[key], memo)) { return false; }
}

return true;
}

export function deepEqual(actual, expected, reason) {
const memo = [ ];
const isOk = isDeepEqual(actual, expected, memo);
if (!isOk) {
equal(actual, expected, reason);
}
}

export function ok(check, reason) {
equal(!!check, true, reason);
}

export function throws(func, checkFunc, reason) {
try {
func();

} catch (e) {
if (checkFunc(e)) { return true; }

throwError(`The expected exception validation function returned false`, {
actual: e,
expected: checkFunc,
operation: "throws"
});
}

throwError("Missing expected exception", {
operator: "throws"
});
}

export async function rejects(func, checkFunc, reason) {
try {
await func();
} catch (e) {
if (checkFunc(e)) { return true; }

throwError(`The rejection validation function returned false`, {
actual: e,
expected: checkFunc,
operation: "throws"
});
}

throwError("Missing rejection", {
operator: "rejects"
});
}

export default {
equal, deepEqual, ok, rejects, throws
};
26 changes: 26 additions & 0 deletions misc/test-browser/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<html>
<body>
<h1>Hello World!!</h1>
<div>Please check the console for test output...</div>

<div id="mocha"></div>

<script type="module">
// Must import Mocha first; completely
await import("./static/mocha.js");

// Load our custom Reporter (after importing mocha)
import { MyReporter } from "/static/reporter.js";

// Setup the global environment and set out reporter
mocha.setup({ ui: 'bdd' });
mocha.reporter(MyReporter);

// Import the tests
await import("./tests/index.js");

// Run Mocha!
mocha.run();
</script>
</body>
</html>
Loading

0 comments on commit 96d5e7b

Please sign in to comment.