forked from cho45/jsdeferred
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-node.js
executable file
·97 lines (83 loc) · 2.04 KB
/
test-node.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
#!node
var sys = require('sys');
var fs = require('fs');
var Deferred = require('./jsdeferred.js').Deferred;
var Global = global;
Deferred.define();
var data;
data = fs.readFileSync('./test-jsdeferred.js', 'ascii');
data = data.match(/\/\/ ::Test::Start::([\s\S]+)::Test::End::/)[1];
var testfuns = []; data.replace(/(ok|expect)\(.+/g, function (m) {
testfuns.push(m);
return m;
});
var expects = testfuns.length;
function uneval (obj) {
return sys.inspect(obj);
}
function show (msg, expect, result) {
var okng = this;
var out = [];
out.push(color(46, "[", [expects - testfuns.length, expects].join("/"), "]"));
if (okng == "skip") {
out.push(" ", color(33, "skipped " + expect + " tests: " + msg));
console.log(out.join(""));
while (expect--) testfuns.pop();
} else
if (okng == "ng") {
testfuns.pop();
expect = (typeof expect == "function") ? uneval(expect).match(/[^{]+/)+"..." : uneval(expect);
result = (typeof result == "function") ? uneval(result).match(/[^{]+/)+"..." : uneval(result);
out.push(["NG Test::", msg, expect, result].join("\n"));
console.log(out.join(""));
process.exit(1);
} else {
testfuns.pop();
out.push(" ", color(32, "ok"));
console.log(out.join(""));
}
}
function msg (m) {
console.log(m);
}
log = msg;
print = msg;
function ok () {
show.apply("ok", arguments);
return true;
}
function ng () {
show.apply("ng", arguments);
return true;
}
function skip () {
show.apply("skip", arguments);
return true;
}
function expect (msg, expect, result) {
if (expect == result) {
show.apply("ok", arguments);
} else {
show.apply("ng", arguments);
}
return true;
}
function color (code) {
var str = "";
for (var i = 1; i < arguments.length; i++) str += arguments[i];
return [
String.fromCharCode(27), "[", code, "m",
str,
String.fromCharCode(27), "[0m"
].join("");
}
// run tests
eval(data);
process.on('exit', function () {
if (expects - testfuns.length == expects) {
print(color(32, "All tests passed"));
} else {
print(color(31, "Some tests failed..."));
process.exit(1);
}
});