initial commit
This commit is contained in:
commit
96ce863f7f
2 changed files with 60 additions and 0 deletions
12
package.json
Normal file
12
package.json
Normal file
|
@ -0,0 +1,12 @@
|
|||
{
|
||||
"name": "folktest",
|
||||
"version": "1.0.0",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"description": ""
|
||||
}
|
48
src/index.js
Normal file
48
src/index.js
Normal file
|
@ -0,0 +1,48 @@
|
|||
export class AssertionError extends Error {
|
||||
constructor(message) {
|
||||
super(message)
|
||||
}
|
||||
}
|
||||
|
||||
export const assert = (value, message = `assertion error: ${value} is not true`) => {
|
||||
assertEq(value, true, message)
|
||||
}
|
||||
|
||||
export const assertEq = (a, b, message = `assertion error: ${a} !== ${b}`) => {
|
||||
if (a === b) {
|
||||
return
|
||||
}
|
||||
else if (Array.isArray(b) && a.length === b.length) {
|
||||
b.forEach((n, i) => assertEq(a[i], n))
|
||||
return
|
||||
} else {
|
||||
throw new AssertionError(message)
|
||||
}
|
||||
}
|
||||
|
||||
export const assertCallback = () => {
|
||||
let result = false
|
||||
setTimeout(() => assert(result), 0)
|
||||
return () => result = true
|
||||
}
|
||||
|
||||
export const it = (description, test) => {
|
||||
return () => {
|
||||
try {
|
||||
test()
|
||||
|
||||
return {
|
||||
success: true,
|
||||
description,
|
||||
error: undefined
|
||||
}
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
description,
|
||||
error
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Add table
Reference in a new issue