/
sink.js
181 lines (162 loc) · 3.56 KB
/
sink.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*!
* Sink - Browser & Headless JavaScript Unit Tester
* copyright Dustin Diaz & Jacob Thornton
*
*/
!function(context) {
var total = 0,
fail = false,
tests = [],
item,
setPasses = true,
beforeMethods = [],
afterMethods = [],
currentSetName,
isHeadless = false;
function reset() {
total = 0;
fail = false;
init();
}
function each(items, fn) {
for (var i = 0; i < items.length; i++) {
fn(items[i]);
}
}
function failure(li, check) {
setPasses = false;
if (!isHeadless) {
check.innerHTML = '✗';
li.className = 'fail';
}
reset();
}
function pass(li, check) {
if (!isHeadless) {
check.innerHTML = '✓';
li.className = 'pass';
}
reset();
}
function before(fn) {
fn ? beforeMethods.push(fn) : each(beforeMethods, function (f) {
f();
});
}
function after(fn) {
fn ? afterMethods.push(fn) : each(afterMethods, function (f) {
f();
});
}
function bind(li) {
li.onclick = function() {
this.getElementsByTagName('ul')[0].className = 'show';
};
}
function _test(name, expect, fn) {
before();
total = expect;
var li, check;
if (!isHeadless) {
li = document.createElement('li');
li.innerHTML = name + ' ... <span>o</span><ul></ul>';
item = li.getElementsByTagName('ul')[0];
bind(li);
check = li.getElementsByTagName('span')[0];
document.getElementById('tests').appendChild(li);
} else {
console.log((name + '...').yellow);
}
var start = +new Date;
fn();
setTimeout(function() {
if (+new Date - start > 10000) {
failure(li, check);
after();
} else {
if (fail) {
failure(li, check);
after();
} else if (!total) {
after();
pass(li, check);
} else {
setTimeout(arguments.callee, 10);
}
}
}, 10);
}
function test(name, expect, fn) {
tests.push({
name: name,
expect: expect,
fn: fn
});
}
function init() {
if (tests.length) {
var o = tests.shift();
_test(o.name, o.expect, o.fn);
} else {
setPasses = true;
start();
}
}
function ok(b, message) {
if (isHeadless) {
if (b) {
console.log((message + ' ✓').green);
} else {
console.log((message + ' ✗').red);
}
} else {
var li = document.createElement('li');
li.innerHTML = message + ' ' + (b ? '✓' : '✗');
item.appendChild(li);
}
if (b) {
total--;
} else {
fail = true;
}
}
var modules = [];
function sink(name, fn) {
modules.push({
name: name,
fn: fn
});
}
function nextGroup(name, fn) {
beforeMethods = [];
afterMethods = [];
console.log('MODULE: ' + name);
fn(test, ok, before, after);
currentSetName = name;
init();
}
function start() {
var current = modules.shift();
current ? nextGroup(current.name, current.fn) : !function () {
var message = [
'Congratulations! All tests have passed!',
'There were some errors! The suite has failed.'
];
if (isHeadless) {
message = message[setPasses ? 0 : 1].toUpperCase();
if (setPasses) {
console.log(message.rainbow);
} else {
console.log(message.red);
}
}
}();
}
if (isHeadless) {
exports.sink = sink;
exports.start = start;
} else {
context.sink = sink;
context.start = start;
}
}(this);