-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtinytest.js
50 lines (44 loc) · 1.51 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
// Very simple unit-test library, with zero deps. Results logged to console.
// -jwalnes
var failures = 0;
var TinyTest = {
run: function(suiteName, tests) {
for (var testName in tests) {
var testAction = tests[testName];
try {
testAction.apply(this);
console.log('Suite:', suiteName, 'Test:', testName, 'OK');
} catch (e) {
failures++;
console.error('Suite:', suiteName, 'Test:', testName, 'FAILED');
console.error(e.stack);
}
}
setTimeout(function() { // Give document a chance to complete
if (window.document && document.body) {
document.body.style.backgroundColor = (failures == 0 ? '#99ff99' : '#ff9999');
}
}, 0);
},
fail: function(msg) {
throw new Error('fail(): ' + msg);
},
assert: function(value, msg) {
if (!value) {
throw new Error('assert(): ' + msg);
}
},
assertEquals: function(expected, actual) {
if (expected != actual) {
throw new Error('assertEquals() "' + expected + '" != "' + actual + '"');
}
},
assertStrictEquals: function(expected, actual) {
if (expected !== actual) {
throw new Error('assertStrictEquals() "' + expected + '" !== "' + actual + '"');
}
},
assertStructure: function(expected, actual) {
this.assertEquals(JSON.stringify(expected), JSON.stringify(actual));
},
};