-
Notifications
You must be signed in to change notification settings - Fork 1
/
cupoftea.js
367 lines (312 loc) · 9.88 KB
/
cupoftea.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
var _ = require('underscore');
var util = require('util');
var argv = require('optimist').argv;
var debug = function (msg) {
//console.log(msg);
};
var d = function (msg) {
console.log(msg);
};
var SpecDefinition = function (desc, parentSpec, index) {
var childSpecs = [];
var specsRun = 0;
var firstRun = true;
var findChildSpecByDescription = function (desc) {
debug('childSpecs:');
_(childSpecs).each(function (spec) {
debug('spec: ' + spec.description() + ' === ' + desc + ': ' + (spec.description() === desc));
});
return _(childSpecs).detect(function (spec) {
return spec.description() === desc;
});
};
return {
addSpec: function (desc, definition) {
var existingSpec = findChildSpecByDescription(desc);
if (existingSpec) {
if (firstRun) {
throw new Error('this spec seems to be a duplicate: ' + desc);
}
debug('found spec: ' + existingSpec.description());
debug('specs run: ' + specsRun);
debug('spec index: ' + existingSpec.index());
if (existingSpec.index() === specsRun) {
currentRunStack.pushCurrentSpec(existingSpec, definition);
}
} else {
var childSpec = new SpecDefinition(desc, this, childSpecs.length);
childSpecs.push(childSpec);
debug('adding spec: ' + childSpec.description());
debug('specs run: ' + specsRun);
debug('spec index: ' + childSpec.index());
if (childSpec.index() === specsRun) {
currentRunStack.pushCurrentSpec(childSpec, definition);
}
}
},
fullDescription: function () {
var getParentDesc = function () {
var parentDesc = parentSpec.fullDescription();
if (parentDesc) {
return parentDesc + ' ';
} else {
return '';
}
};
return getParentDesc() + desc;
},
description: function () {
return desc;
},
parent: function () {
return parentSpec;
},
isFinished: function () {
var finished = _(childSpecs).all(function (spec) {
return spec.isFinished();
});
debug('spec: ' + desc + ' is finished: ' + finished);
return finished && specsRun === childSpecs.length;
},
end: function () {
firstRun = false;
if (childSpecs.length > specsRun && childSpecs[specsRun].isFinished()) {
specsRun++;
}
},
index: function () {
return index;
}
};
};
var TopSpec = function (runStack) {
return {
addSpec: function (desc, definition) {
debug('starting with spec: ' + desc);
var spec = new SpecDefinition(desc, this, 0);
var timesRun = 0;
do {
timesRun++;
debug('times run: ' + timesRun);
runStack.runSpec(function () {
runStack.pushCurrentSpec(spec, definition);
},function (spec, exception) {
results.print(spec, exception);
});
} while (!spec.isFinished());
},
end: function () {
},
fullDescription: function () {
return undefined;
},
description: function () {
return 'top level';
}
}
};
var OutstandingCallbacks = function () {
var currentCallbackId = 0;
var callbacks = {};
this.add = function (error) {
callbacks[currentCallbackId] = error;
return currentCallbackId++;
};
this.remove = function (id) {
delete callbacks[id];
};
this.isEmpty = function () {
return _.isEmpty(callbacks);
};
this.first = function () {
return callbacks[0];
};
};
var Callbacks = function (runStack) {
var outstandingCallbacks = new OutstandingCallbacks();
var currentSpecResultsCalled = false;
var hasCallbacks = false;
var results = function (exception) {
if (!currentSpecResultsCalled) {
runStack.results(exception);
currentSpecResultsCalled = true;
}
};
this.shouldNotCall = function () {
hasCallbacks = true;
var error = new Error("shouldn't be called");
return function () {
results(error);
};
};
this.shouldCall = function (f) {
hasCallbacks = true;
var callbackId = outstandingCallbacks.add(new Error('not called'));
var runStack = currentRunStack;
return function () {
var oldRunStack = currentRunStack;
currentRunStack = runStack;
outstandingCallbacks.remove(callbackId);
try {
var result = f.apply(this, arguments);
} catch (e) {
results(e);
expectedExceptions.push(e);
throw e;
}
if (outstandingCallbacks.isEmpty()) {
results();
}
currentRunStack = oldRunStack;
return result;
};
};
this.hasCallbacks = function () {
return hasCallbacks;
};
this.assertCallbacks = function () {
if (!outstandingCallbacks.isEmpty()) {
results(outstandingCallbacks.first());
} else if (hasCallbacks) {
results();
}
};
};
var expectedExceptions = [];
var RunStack = function () {
var callbacks = new Callbacks(this);
var deepestSpec;
var currentSpec;
this.spec = function (desc, definition) {
currentSpec.addSpec(desc, definition);
};
this.pushCurrentSpec = function (newSpec, definition) {
var oldCurrentSpec = currentSpec;
deepestSpec = currentSpec = newSpec;
try {
definition();
} finally {
currentSpec.end();
currentSpec = oldCurrentSpec;
}
};
this.run = function (spec, definition) {
try {
this.pushCurrentSpec(spec, definition);
if (!callbacks.hasCallbacks()) {
this.results();
}
} catch (e) {
this.results(e);
}
};
this.shouldCall = function (f) {
return callbacks.shouldCall(f);
};
this.shouldNotCall = function () {
return callbacks.shouldNotCall();
};
this.assertAllCallbacks = function () {
callbacks.assertCallbacks();
};
this.results = function (exception) {
results.print(deepestSpec, exception);
};
};
var SimpleResults = function () {
this.print = function (spec, exception) {
var desc = spec.fullDescription();
if (!exception) {
process.stdout.write(desc + " [0;32mOK[0m\n");
} else {
process.stdout.write(desc + " [0;31mFAILED[0m\n");
if (exception.stack) {
process.stdout.write(exception.stack + "\n");
} else {
process.stdout.write(exception + "\n");
}
process.stdout.write("\n");
}
};
this.wrapup = function () {
};
};
var RspecResults = function () {
var exceptions = [];
var passes = 0;
this.print = function (spec, exception) {
if (!exception) {
process.stdout.write('.');
passes++;
} else {
process.stdout.write('F');
exceptions.push({spec: spec, exception: exception});
}
};
var indent = function (text) {
return text.replace(/\n/g, '\n ');
};
var printExceptions = function () {
_(exceptions).each (function (ex) {
process.stdout.write('\n');
process.stdout.write(ex.spec.fullDescription() + ' [0;31mFAILED[0m\n\n');
if (ex.exception.stack) {
var stack = indent(ex.exception.stack);
process.stdout.write(' ' + stack + "\n");
} else {
var msg = util.inspect(ex.exception);
process.stdout.write(' ' + msg + "\n");
}
});
};
this.wrapup = function () {
process.stdout.write("\n");
printExceptions();
process.stdout.write("\n");
process.stdout.write('Specs: ' + (exceptions.length + passes) + ' Passed: ' + passes + ' Failed: ' + exceptions.length + '\n');
};
};
var resultFormatters = {
'simple': SimpleResults,
'rspec': RspecResults
};
var results = new (resultFormatters[argv.format || 'rspec'])();
var TopLevelRunStack = function () {
this.spec = function (desc, definition) {
var spec = new SpecDefinition(desc, new TopSpec(this), 0);
do {
currentRunStack = new RunStack();
runStacks.push(currentRunStack);
currentRunStack.run(spec, definition);
} while (!spec.isFinished());
currentRunStack = this;
results.wrapup();
};
};
var currentRunStack = new TopLevelRunStack();
var runStacks = [];
spec = function (desc, definition) {
currentRunStack.spec(desc, definition);
};
unused_spec = function () {
};
shouldNotCall = function () {
return currentRunStack.shouldNotCall();
};
shouldCall = function (f) {
return currentRunStack.shouldCall(f);
};
process.addListener('exit', function () {
_(runStacks).each(function (runStack) {
runStack.assertAllCallbacks();
});
});
process.on('uncaughtException', function(err) {
if (!_(expectedExceptions).contains(err)) {
if (err.stack) {
console.log(err.stack);
} else {
console.log(err);
}
}
});