-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: added test-browser static files
- Loading branch information
Showing
7 changed files
with
21,359 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Oops, something went wrong.