This repository was archived by the owner on Jul 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodetest.js
132 lines (112 loc) · 2.97 KB
/
nodetest.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
var mjsunit = require('mjsunit');
var sys = require('sys');
sys.red = function(message) { sys.puts("\033[0;31m" + message + "\033[1;37m"); }
sys.green = function(message) { sys.puts("\033[0;32m" + message + "\033[1;37m"); }
function TestCase(name, test) {
this.name = name;
this.test = test;
this.tests = [];
}
TestCase.prototype = {
run: function() {
var self = this;
if (typeof this.test.setupOnce == 'function')
this.test.setupOnce();
for (var name in this.test) {
if (name.match(/^test/)) {
var t = new Test(name, this.test[name]);
this.tests.push(t);
t.run(this.test);
}
}
process.addListener("exit", function() {
var totalAssertions = 0;
var failed = [], passed = [];
self.tests.forEach(function(t) {
if (t.passed) {
totalAssertions += t.assertionsPassed;
passed.push(t);
} else {
failed.push(t);
}
});
sys.puts('');
if (failed.length > 0) {
sys.red(self.name + ' Tests Failed');
failed.forEach(function(fail) {
sys.puts(fail.name + ': ' + fail.reason);
});
} else {
sys.green(self.name + ' Tests Passed');
}
sys.puts("\n" + (passed.length + failed.length) + " tests, " + totalAssertions + ' assertions ran');
sys.puts(passed.length + ' tests passed, ' + failed.length + ' failed');
});
}
}
function Test(name, body) {
this.name = name;
this.body = body;
this.assertionsPassed = 0;
this.passed = true;
}
/* Wrap the MJSUnit tests */
for (var method in mjsunit) {
if (/^assert/.test(method)) {
(function() {
var m = method;
Test.prototype[m] = function() {
try {
mjsunit[m].apply(this, arguments);
this.assertionsPassed += 1;
} catch(e) {
this.reason = e.message;
this.passed = false;
}
}
sys.print('.');
})();
}
}
Test.prototype.run = function(context) {
if (typeof context.setup == 'function') context.setup(this);
this.body.call(context, this);
}
process.mixin(exports, {
testcase: function(name, testcase) {
var test = new TestCase(name, testcase);
test.run();
}
});
test = exports;
test.testcase("Awesome", {
setupOnce: function() {
this.a = 0;
this.a++;
},
setup: function() {
this.x = 123;
},
"test should run setup": function(test) {
test.assertTrue(this.x == 123);
},
"test should run setupOnce only once (I shall say zees only waance)": function(test) {
test.assertTrue(this.a == 1);
},
testConfusion: function(test) {
test.assertTrue(true);
test.assertTrue(true, 'blah blah');
setTimeout(function() {
test.assertTrue(true);
}, 200);
},
/* "context something else": {
setup: function() {
this.y = 456;
},
testThing: function(test) {
assertTrue(this.x == 123);
assertTrue(this.y == 456);
}
} */
});