forked from caolan/nodeunit
/
core.js
207 lines (177 loc) · 5.35 KB
/
core.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*!
* Nodeunit
* Copyright (c) 2010 Caolan McMahon
* MIT Licensed
*
* THIS FILE SHOULD BE BROWSER-COMPATIBLE JS!
* You can use @REMOVE_LINE_FOR_BROWSER to remove code from the browser build.
* Only code on that line will be removed, its mostly to avoid requiring code
* that is node specific
*/
/**
* Module dependencies
*/
var async = require('../deps/async'), //@REMOVE_LINE_FOR_BROWSER
types = require('./types'); //@REMOVE_LINE_FOR_BROWSER
/**
* Added for browser compatibility
*/
var _keys = function(obj){
if(Object.keys) return Object.keys(obj);
var keys = [];
for(var k in obj){
if(obj.hasOwnProperty(k)) keys.push(k);
}
return keys;
};
/**
* Runs a test function (fn) from a loaded module. After the test function
* calls test.done(), the callback is executed with an assertionList as its
* second argument.
*
* @param {String} name
* @param {Function} fn
* @param {Object} opt
* @param {Function} callback
* @api public
*/
exports.runTest = function (name, fn, opt, callback) {
var options = types.options(opt);
options.testStart(name);
var start = new Date().getTime();
var test = types.test(name, start, options, callback);
try {
fn(test);
}
catch (e) {
test.done(e);
}
};
/**
* Takes an object containing test functions or other test suites as properties
* and runs each in series. After all tests have completed, the callback is
* called with a list of all assertions as the second argument.
*
* If a name is passed to this function it is prepended to all test and suite
* names that run within it.
*
* @param {String} name
* @param {Object} suite
* @param {Object} opt
* @param {Function} callback
* @api public
*/
exports.runSuite = function (name, suite, opt, callback) {
var keys = _keys(suite);
async.concatSeries(keys, function (k, cb) {
var prop = suite[k], _name;
_name = name ? [].concat(name, k) : [k];
_name.toString = function () {
// fallback for old one
return this.join(' - ');
};
if (typeof prop === 'function') {
exports.runTest(_name, suite[k], opt, cb);
}
else {
exports.runSuite(_name, suite[k], opt, cb);
}
}, callback);
};
/**
* Run each exported test function or test suite from a loaded module.
*
* @param {String} name
* @param {Object} mod
* @param {Object} opt
* @param {Function} callback
* @api public
*/
exports.runModule = function (name, mod, opt, callback) {
var options = types.options(opt);
options.moduleStart(name);
var start = new Date().getTime();
exports.runSuite(null, mod, opt, function (err, a_list) {
var end = new Date().getTime();
var assertion_list = types.assertionList(a_list, end - start);
options.moduleDone(name, assertion_list);
callback(null, a_list);
});
};
/**
* Treats an object literal as a list of modules keyed by name. Runs each
* module and finished with calling 'done'. You can think of this as a browser
* safe alternative to runFiles in the nodeunit module.
*
* @param {Object} modules
* @param {Object} opt
* @api public
*/
// TODO: add proper unit tests for this function
exports.runModules = function (modules, opt) {
var all_assertions = [];
var options = types.options(opt);
var start = new Date().getTime();
async.concatSeries(_keys(modules), function (k, cb) {
exports.runModule(k, modules[k], options, cb);
},
function (err, all_assertions) {
var end = new Date().getTime();
options.done(types.assertionList(all_assertions, end - start));
});
};
/**
* Utility for wrapping a suite of test functions with setUp and tearDown
* functions.
*
* @param {Object} suite
* @return {Object}
* @api public
*/
exports.testCase = function (suite) {
var tests = {};
var setUp = suite.setUp;
var tearDown = suite.tearDown;
delete suite.setUp;
delete suite.tearDown;
var keys = _keys(suite);
var tests = {};
for (var i=0; i<keys.length; i++) {
(function (k) {
tests[k] = function (test) {
var context = {};
if (tearDown) {
var done = test.done;
test.done = function (err) {
try {
tearDown.call(context, function (err2) {
if (err && err2) {
test._assertion_list.push(
types.assertion({error: err})
);
return done(err2);
}
done(err || err2);
});
}
catch (e) {
done(e);
}
};
}
if (setUp) {
setUp.call(context, function (err) {
if (err) {
return test.done(err);
}
suite[k].call(context, test);
});
}
else {
suite[k].call(context, test);
}
};
}(keys[i]));
}
return tests;
};