-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnodeunit.js
More file actions
204 lines (185 loc) · 6.47 KB
/
nodeunit.js
File metadata and controls
204 lines (185 loc) · 6.47 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
var assert = require('assert'),
fs = require('fs'),
util = require('util');
exports.testrunner = require('./testrunner');
var assertion = function(obj){
return {
method: obj.method || '',
message: obj.message || (obj.error && obj.error.message) || '',
error: obj.error,
passed: function(){return !this.error;},
failed: function(){return Boolean(this.error);}
};
};
var assertions = function(arr, duration){
var that = arr || [];
that.__defineGetter__('failures', function(){
return this.reduce(function(a,x){
return x.failed() ? a+1 : a;
}, 0);
});
that.duration = duration || 0;
return that;
};
var testEnv = function(start, options){
var expecting;
var a_list = [];
options.log = options.log || function(){}; // log callback optional
var wrapAssert = function(new_method, assert_method){
return function(){
try {
assert[assert_method].apply(global, arguments);
var message = arguments[arguments.length-1];
var a = assertion({method:new_method, message:message});
}
catch (e){
// deepEqual error message is a bit sucky, lets improve it!
// e.actual and e.expected could be null or undefined, so
// using getOwnPropertyDescriptor to see if they exist:
if(Object.getOwnPropertyDescriptor(e,'actual')
&& Object.getOwnPropertyDescriptor(e,'expected')){
var actual = util.inspect(e.actual).replace(/\n$/,'');
var expected = util.inspect(e.expected).replace(/\n$/,'');
var multiline = (
actual.indexOf('\n') != -1 ||
expected.indexOf('\n') != -1
);
var spacing = (multiline ? '\n' : ' ');
e._message = e.message;
e.stack = (
e.name + ':' + spacing +
actual + spacing + e.operator + spacing +
expected + '\n' +
e.stack.split('\n').slice(1).join('\n')
);
}
var a = assertion({method:new_method, error:e});
}
a_list.push(a);
process.nextTick(function(){options.log(a)});
};
};
return {
done: function(){
if(expecting !== undefined && expecting != a_list.length){
var err = new Error(
'Expected ' + expecting + ' assertions, ' +
a_list.length + ' ran'
);
var a = assertion({method:'expect', error:err});
a_list.push(a);
process.nextTick(function(){options.log(a);});
}
var end = new Date().getTime();
process.nextTick(function(){
options.testDone(options.name, assertions(a_list, end-start));
});
},
ok: wrapAssert('ok', 'ok'),
same: wrapAssert('same', 'deepEqual'),
equals: wrapAssert('equals', 'equal'),
expect: function(num){
expecting = num;
}
};
};
exports.runTest = function(fn, options){
options.log = options.log || function(){}; // log callback optional
var start = new Date().getTime();
try {
fn(testEnv(start, options));
}
catch (e){
var end = new Date().getTime();
var a = assertion({error:e});
process.nextTick(function(){
options.log(a);
options.testDone(options.name, assertions([a], end-start));
});
}
};
exports.runModule = function(mod, options){
var m_assertions = [];
var start = new Date().getTime();
var i = 0;
var tests = Object.keys(mod);
if(tests.length){
var _fn = function(testname){
(options.testStart || function(){})(testname);
exports.runTest(mod[testname], {
name: testname,
log: options.log,
testDone: function(name, a_list){
m_assertions = m_assertions.concat(a_list);
(options.testDone || function(){})(name, a_list);
i++;
if(i < tests.length){
_fn(tests[i]);
}
else {
var end = new Date().getTime();
(options.moduleDone || function(){})(
options.name, assertions(m_assertions, end-start)
);
}
}
});
};
_fn(tests[0] || {});
}
else {
var end = new Date().getTime();
(options.moduleDone || function(){})(
options.name, assertions([], end-start)
);
}
};
exports.runFiles = function(paths, options){
var all_assertions = [];
var start = new Date().getTime();
if(!paths.length){
return options.done(assertions(all_assertions));
}
var files = paths.reduce(function(a,p){
var stats = fs.statSync(p);
if(stats.isFile()){
return a.concat([p]);
}
else if(stats.isDirectory()){
return a.concat(fs.readdirSync(p).filter(function(filename){
return /\.js$/.exec(filename);
}).map(function(filename){
return [p, filename].join('/');
}));
}
}, []);
if(!files.length){
var end = new Date().getTime();
(options.done || function(){})(assertions([], end-start));
return;
}
var i = 0;
var _fn = (function(file){
options.moduleStart(file);
exports.runModule(require(file.replace(/\.js$/, '')), {
name: file,
log: options.log,
moduleStart: options.moduleStart,
testStart: options.testStart,
testDone: options.testDone,
moduleDone: function(name, a_list){
all_assertions = all_assertions.concat(a_list);
(options.moduleDone || function(){}).apply(global, arguments);
i++;
if(i < files.length){
_fn(files[i]);
}
else {
var end = new Date().getTime();
options.done(assertions(all_assertions, end-start));
}
}
});
});
_fn(files[0]);
};