Skip to content

Commit d93f028

Browse files
author
Caolan McMahon
committed
first commit
0 parents  commit d93f028

File tree

6 files changed

+454
-0
lines changed

6 files changed

+454
-0
lines changed

README.md

Whitespace-only changes.

index.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// This file is just added for convenience so this repository can be
2+
// directly checked out into a project's deps folder
3+
module.exports = require('./lib/nodeunit');

lib/nodeunit.js

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
var assert = require('assert');
2+
3+
4+
var Assertion = function(method, message, error){
5+
return {
6+
method: method,
7+
message: message || '',
8+
error: error,
9+
passed: function(){return !error;},
10+
failed: function(){return Boolean(error);}
11+
};
12+
};
13+
14+
var TestEnv = function(options){
15+
var expecting;
16+
var assertions = [];
17+
options.log = options.log || function(){}; // log callback optional
18+
19+
var wrapAssert = function(new_method, assert_method){
20+
return function(){
21+
try {
22+
assert[assert_method].apply(global, arguments);
23+
var message = arguments[arguments.length-1];
24+
var assertion = new Assertion(new_method, message);
25+
}
26+
catch (e){
27+
var assertion = new Assertion('', e.message || '', e);
28+
}
29+
assertions.push(assertion);
30+
process.nextTick(function(){options.log(assertion)});
31+
};
32+
};
33+
34+
return {
35+
done: function(){
36+
if(expecting !== undefined && expecting != assertions.length){
37+
var err = new Error(
38+
'Expected ' + expecting + ' assertions, ' +
39+
assertions.length + ' ran'
40+
);
41+
var assertion = new Assertion('expect', err.message, err);
42+
assertions.push(assertion);
43+
process.nextTick(function(){options.log(assertion);});
44+
}
45+
var failures = assertions.reduce(function(a,x){
46+
return x.failed() ? a+1 : a;
47+
}, 0);
48+
process.nextTick(function(){
49+
options.testDone(failures, assertions.length);
50+
});
51+
},
52+
ok: wrapAssert('ok', 'ok'),
53+
same: wrapAssert('same', 'deepEqual'),
54+
equals: wrapAssert('equals', 'equal'),
55+
expect: function(num){
56+
expecting = num;
57+
}
58+
};
59+
};
60+
61+
62+
exports.runTest = function(fn, options){
63+
options.log = options.log || function(){}; // log callback optional
64+
try {
65+
fn(new TestEnv(options));
66+
}
67+
catch (e){
68+
var assertion = new Assertion('', e.message || '', e);
69+
process.nextTick(function(){options.log(assertion)});
70+
process.nextTick(function(){options.testDone(1, 1)});
71+
}
72+
};
73+
74+
75+
exports.runModule = function(mod, options){
76+
var m_failures = 0;
77+
var m_total = 0;
78+
var i = 0;
79+
var tests = Object.keys(mod).map(function(k){return mod[k];});
80+
var _fn = function(test){
81+
(options.testStart || function(){})(test);
82+
exports.runTest(test, {
83+
log: options.log,
84+
testDone: function(failures, total){
85+
86+
m_failures += failures;
87+
m_total += total;
88+
(options.testDone || function(){})(failures, total);
89+
90+
i++;
91+
if(i < tests.length){
92+
_fn(tests[i]);
93+
}
94+
else {
95+
(options.moduleDone || function(){})(m_failures, m_total);
96+
}
97+
}
98+
});
99+
};
100+
_fn(tests[0]);
101+
};

test.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/local/bin/node
2+
3+
var path = require('path'),
4+
fs = require('fs');
5+
6+
require.paths.push(process.cwd());
7+
require.paths.push(path.join(process.cwd(), 'deps'));
8+
require.paths.push(path.join(process.cwd(), 'lib'));
9+
10+
fs.readdirSync('test').filter(function(filename){
11+
return /\.js$/.exec(filename);
12+
}).forEach(function(filename){
13+
require('test/' + filename.replace(/\.js$/,''));
14+
});

test/test-runmodule.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
var assert = require('assert'),
2+
sys = require('sys'),
3+
nodeunit = require('nodeunit');
4+
5+
6+
7+
var call_order = [];
8+
var testmodule = {
9+
test1: function(test){
10+
call_order.push('test1');
11+
test.ok(true, 'ok true');
12+
test.done();
13+
},
14+
test2: function(test){
15+
call_order.push('test2');
16+
test.ok(false, 'ok false');
17+
test.ok(false, 'ok false');
18+
test.done();
19+
},
20+
test3: function(test){
21+
call_order.push('test3');
22+
test.done();
23+
}
24+
};
25+
nodeunit.runModule(testmodule, {
26+
log: function(assertion){
27+
call_order.push('log');
28+
},
29+
testStart: function(name){
30+
call_order.push('testStart');
31+
},
32+
testDone: function(failures, total){
33+
call_order.push('testDone');
34+
},
35+
moduleDone: function(failures, total){
36+
assert.equal(failures, 2);
37+
assert.equal(total, 3);
38+
call_order.push('moduleDone');
39+
}
40+
});
41+
42+
43+
// callbacks are async, so test call order after callbacks have executed
44+
setTimeout(function(){
45+
assert.deepEqual(call_order, [
46+
'testStart', 'test1', 'log', 'testDone',
47+
'testStart', 'test2', 'log', 'log', 'testDone',
48+
'testStart', 'test3', 'testDone',
49+
'moduleDone'
50+
]);
51+
sys.puts('test-runmodule OK');
52+
}, 100);

0 commit comments

Comments
 (0)