forked from judell/hlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tinytest.js
53 lines (39 loc) · 1.14 KB
/
tinytest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// inspired by https://github.com/joewalnes/jstinytest
// promisified for this project
const red = '#ff9999';
let testName
function log(msg) {
document.getElementById('log').innerHTML += `<div>${msg}</div>`
}
function logError(msg) {
document.getElementById('log').innerHTML += `<div style="background-color:${red}">${msg}</div>`
}
const TinyTest = {
run: async function(tests) {
const testNames = Object.keys(tests)
for (i = 0; i < testNames.length; i++) {
const testName = testNames[i]
await tests[testName]()
log(testName)
}
log('done')
},
assert: function(value) {
if (!value) {
let msg = `${testName}: ${value}`
console.error(msg)
logError(msg)
}
},
assertEquals: function(expected, actual) {
if (expected != actual) {
let msg = `${testName}: expected ${expected}, actual ${actual}`
console.error(msg)
logError(msg)
}
},
};
const assert = TinyTest.assert,
assertEquals = TinyTest.assertEquals,
eq = TinyTest.assertEquals, // alias for assertEquals
tests = TinyTest.run;