mirror of
https://github.com/bellard/quickjs.git
synced 2025-05-29 01:49:18 +08:00
44 lines
1.1 KiB
JavaScript
44 lines
1.1 KiB
JavaScript
export function assert(actual, expected, message) {
|
|
if (arguments.length === 1)
|
|
expected = true;
|
|
|
|
if (typeof actual === typeof expected) {
|
|
if (Object.is(actual, expected))
|
|
return;
|
|
if (typeof actual === 'object') {
|
|
if (actual !== null && expected !== null
|
|
&& actual.constructor === expected.constructor
|
|
&& actual.toString() === expected.toString())
|
|
return;
|
|
}
|
|
}
|
|
throw Error("assertion failed: got |" + actual + "|" +
|
|
", expected |" + expected + "|" +
|
|
(message ? " (" + message + ")" : ""));
|
|
}
|
|
|
|
export function assertThrows(err, func)
|
|
{
|
|
var ex;
|
|
ex = false;
|
|
try {
|
|
func();
|
|
} catch(e) {
|
|
ex = true;
|
|
assert(e instanceof err);
|
|
}
|
|
assert(ex, true, "exception expected");
|
|
}
|
|
|
|
export function assertArrayEquals(a, b)
|
|
{
|
|
if (!Array.isArray(a) || !Array.isArray(b))
|
|
return assert(false);
|
|
|
|
assert(a.length, b.length);
|
|
|
|
a.forEach((value, idx) => {
|
|
assert(b[idx], value);
|
|
});
|
|
}
|