assertErr

This commit is contained in:
Rowan 2025-04-18 03:36:54 -05:00
parent 708d44f121
commit cbf48ff3b1

View file

@ -34,6 +34,24 @@ export const assertEq = (a, b, message = `assertion error: ${a} !== ${b}`) => {
throw new AssertionError(message) throw new AssertionError(message)
} }
} }
const test = (re, str = '') => re instanceof RegExp ? re.test(str) : new RegExp(re).test(str)
export const assertErr = (f, err) => {
try {
f()
} catch (e) {
if (typeof err === 'string' || err instanceof RegExp) {
assert(test(err, e.message), `${err} did not match ${e.message}`)
} else if (typeof err === 'function') {
assert(e.constructor === err)
}
return
}
const expected = err ? `"${err}" ` : ""
assert(false, `expecting error ${expected}but function did not throw`)
}
export const assertCallback = () => { export const assertCallback = () => {
let result = false let result = false