-
Notifications
You must be signed in to change notification settings - Fork 5
/
tinytest.js
80 lines (59 loc) · 1.73 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// inspired by https://github.com/joewalnes/jstinytest
// promisified for this project
const red = '#ff9999';
let testName
const testGroup = '4mBXKB2X'
localStorage.setItem('h_group', testGroup)
function logError(msg) {
document.body.innerHTML += `<div>${msg}</div`
document.body.style.backgroundColor = red
}
function log(msg) {
document.body.innerHTML += `<div class="logMessage">${msg}</div>`
}
window.alert = function(str) {
localStorage.setItem('h_alert', str)
}
async function delay(seconds) {
return new Promise(resolve => setTimeout(resolve, seconds * 1000))
}
const TinyTest = {
run: async function(tests) {
await delay(3)
const testNames = Object.keys(tests)
for (i = 0; i < testNames.length; i++) {
const testName = testNames[i]
log(testName)
await tests[testName]()
}
log('done')
},
assert: function(value) {
if (!value) {
let msg = `${testName}: 'no 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)
}
},
assertNotEquals: function(expected, actual) {
if (expected == actual) {
let msg = `${testName}: expected ${expected} !== ${actual}`
console.error(msg)
logError(msg)
}
},
};
const fail = TinyTest.fail,
assert = TinyTest.assert,
assertEquals = TinyTest.assertEquals,
eq = TinyTest.assertEquals, // alias for assertEquals
assertNotEquals = TinyTest.assertNotEquals,
assertStrictEquals = TinyTest.assertStrictEquals,
tests = TinyTest.run;