/
noodleTest.js
105 lines (93 loc) · 3.15 KB
/
noodleTest.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
/* ansi-color */
module.exports = (function(config){
var events = require('./events');
var EventEmitter = events.EventEmitter;
var cons = require('./noodleTestConsole');
if (!config) config = {};
var main = new EventEmitter();
var sys = require('sys');
if (!config.timeout) config.timeout = 4000;
var Assertion = function(testCase, assertMethod, args, tester) {
this.testCase = testCase;
this.assertMethod = assertMethod;
this.args = args;
this.tester = tester;
this.passed = null;
var numberOfStackLinesToSkip = 1 /* First line which is just the name of the fake error we created: "Error" */
+ 1 /* Call to here */
+ 1 /* The caller (Test#assert) */;
this.stack = (new Error).stack.split("\n").map(function(line){
return line.trim();
}).slice(numberOfStackLinesToSkip);
};
Assertion.prototype.execute = function() {
this.passed = !!this.tester();
};
Assertion.prototype.failureMessage = function() {
return "Expected first argument to evaluate to true";
};
Assertion.prototype.callString = function() {
return this.assertMethod + '(' + sys.inspect(this.args[0]) + ')'
};
var testQueue = (function() {
var begun = false;
var q = {};
q.array = [];
q.put = function(test) {
q.array.push(test);
if (!begun) {
begun = true;
q.next();
}
};
q.next = function() {
if (q.array.length > 0) {
var top = q.array[0];
q.array = q.array.slice(1);
top.on('testDone', function(){
q.next();
});
top._call();
} else {
begun = false;
}
};
return q;
})();
var Test = require('./test')(Assertion, testQueue, config);
/* Relay events emitted by Test instances to the main object */
Test.on('new', function(t){
events.relayEvents(t, main, ['assertionPassed', 'assertionFailed', 'testFlunk', 'testStarted', 'testTimeout', 'testDone']);
});
var Context = require('./context')(Test);
/* Relay events emitted by Context instances to the main object */
Context.on('new', function(ctx){
events.relayEvents(ctx, main, ['pushContext', 'popContext']);
});
var topLevelContexts = [];
main.context = function(name, callback) {
var ctx = new Context(name, null);
topLevelContexts.push(ctx);
main.emit('pushContext', {name: name, context: ctx});
callback.apply(ctx);
main.emit('popContext', {name: name, context: ctx});
};
var seen = false;
var seenFailure = function() {
seen = true;
};
main.on('assertionFailed', seenFailure);
main.on('testFlunk', seenFailure);
main.onFailureExitNonZero = function() {
process.on('exit', function(a) {
if (seen) {
/* Hack */
process.kill(process.pid, 'SIGHUP');
}
});
};
if (!config['quiet']) {
cons(main);
}
return main;
});