forked from bard/mozrepl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_case.js
360 lines (316 loc) · 9.37 KB
/
test_case.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
/*
* Copyright (C) 2006 by Massimiliano Mirra
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author: Massimiliano Mirra, <bard [at] hyperstruct [dot] net>
*
*/
/**
* Invocation:
* var case = new TestCase('Widget tests');
* var case = new TestCase('Widget tests', {runStrategy: 'async'});
*
* Use async run strategy when test cases mustn't be run immediately
* after test setup, for example when during setup a document is
* loaded into the browser and the browser will signal when the
* document has finished loading through a callback.
*
* Note: code inside tests will still run sequentially. In some
* cases, e.g. testing interfaces, this means you will do something to
* affect the interface and then test the effect, before the interface
* thread has had a chance to apply your request. Control of flow
* inside tests is work in progress.
*
* Alias:
* var spec = new Specification();
*
*/
function constructor(title, opts) {
opts = opts || {};
this._title = title;
this._runStrategy = opts.runStrategy;
this._tests = [];
this._reportHandler = _defaultReportHandler;
this.__defineSetter__(
'tests', function(hash) {
this.setTests(hash);
});
this.__defineSetter__(
'stateThat', function(hash) {
this.setTests(hash);
});
this.__defineSetter__(
'reportHandler', function(callback) {
this._reportHandler = callback;
});
this.__defineGetter__(
'title', function() {
return this._title;
});
}
/**
* Define test cases, optionally with setup and teardown.
*
* var case = new TestCase();
* case.tests = {
* setUp: function() {
* this.plusFactor = 4;
* },
*
* testOperation: function() {
* assert.equals(8, 2+2+this.plusFactor);
* },
*
* tearDown: function() {
* // release resources if necessary
* }
* }
*
* Every test is run in a context created ex-novo and accessible from
* the test itself via the 'this' identifier.
*
* Aliases: setTests(), 'stateThat'. 'setUp' is also aliased to
* 'given'. 'stateThat' and 'given' allow a more Behaviour-Driven
* Development style.
*
* var spec = new Specification();
* spec.stateThat = {
* given: function() {
* this.plusFactor = 4;
* },
*
* 'Adding two and two and plus factor yields eight': function() {
* assert.equals(8, 2+2+this.plusFactor);
* },
*
* tearDown: function() {
* // release resources if necessary
* }
* }
*/
function setTests(hash) {
for(var desc in hash)
if(desc == 'setUp' || desc == 'given')
this._setUp = hash[desc];
else if(desc == 'tearDown')
this._tearDown = hash[desc];
else
this._tests.push({
desc: desc,
code: hash[desc]});
}
/**
* Runs tests with strategy defined at construction time.
*
* var case = new TestCase();
* case.tests = { ... };
* case.run();
*
*/
function run() {
this[this._runStrategy == 'async' ? '_asyncRun1' : '_syncRun1'](
this._tests, this._setUp, this._tearDown, this._reportHandler);
}
/**
* Alternative style for defining setup.
*
*/
function setUp(fn) {
this._setUp = fn;
}
/**
* Alternative style for defining tests. Can be called multiple
* times.
*
*/
function test(desc, code) {
this._tests.push([desc, code]);
}
/**
* Alternative style for defining teardown.
*
*/
function tearDown(fn) {
this._tearDown = fn;
}
/**
* BDD-style alias for run().
*
* var spec = new Specification();
* spec.stateThat = { ... };
* spec.verify();
*
*/
function verify() {
this.run();
}
/**
* BDD-alias for setUp().
*
*/
function given(fn) {
this.setUp(fn);
}
/**
* BDD-style alias for test().
*
*/
function states(desc, fn) {
this.test(desc, fn);
}
/* Side effect-free functions. They're the ones who do the real job. :-) */
function _formatStackTrace1(exception) {
function comesFromFramework(call) {
return (call.match(/@chrome:\/\/mozlab\/content\/lib\/fsm\.js:/) ||
call.match(/@chrome:\/\/mozlab\/content\/mozunit\/test_case\.js:/) ||
// Following is VERY kludgy
call.match(/\(function \(exitResult\) \{if \(eventHandlers/))
}
var trace = '';
if(exception.stack) {
var calls = exception.stack.split('\n');
for each(var call in calls) {
if(call.length > 0 && !comesFromFramework(call)) {
call = call.replace(/\\n/g, '\n');
if(call.length > 200)
call =
call.substr(0, 100) + ' [...] ' +
call.substr(call.length - 100) + '\n';
trace += call + '\n';
}
}
}
return trace;
}
function _exec1(code, setUp, tearDown, context) {
var report = {
result: undefined,
exception: undefined
};
try {
if(setUp)
setUp.call(context);
code.call(context);
if(tearDown)
tearDown.call(context);
report.result = 'success';
} catch(exception if exception.name == 'AssertionFailed') {
report.result = 'failure';
report.exception = exception;
} catch(exception) {
report.result = 'error';
report.exception = exception;
}
return report;
}
function _syncRun1(tests, setUp, tearDown, reportHandler) {
var test, context, report;
for(var i=0, l=tests.length; i<l; i++) {
test = tests[i];
context = {};
report = _exec1(test.code, setUp, tearDown, context);
report.testOwner = this;
report.testDescription = test.desc;
report.testCode = test.code;
report.testIndex = i+1;
report.testCount = l;
reportHandler(report);
}
}
if(false) {
// asynchronous running is not ready, don't make it available.
function _asyncRun1(tests, setUp, tearDown, reportHandler, onTestRunFinished) {
var testIndex = 0;
var context;
var report;
var stateTransitions = {
start: { ok: 'doSetUp' },
doSetUp: { ok: 'doTest', ko: 'doReport' },
doTest: { ok: 'doReport' },
doReport: { ok: 'doTearDown' },
doTearDown: { ok: 'nextTest', ko: 'nextTest' },
nextTest: { ok: 'doSetUp', ko: 'finished' },
finished: { }
}
_this = this;
var stateHandlers = {
start: function(continuation) {
continuation('ok')
},
doSetUp: function(continuation) {
context = {};
report = {};
try {
setUp.call(context, continuation);
} catch(e) {
report.result = 'error';
report.exception = e;
report.testDescription = 'Setup';
continuation('ko');
}
},
doTest: function(continuation) {
var test;
test = tests[testIndex];
report = _exec1(test.code, null, null, context);
report.testDescription = test.desc;
continuation('ok');
},
doReport: function(continuation) {
report.testOwner = _this;
report.testIndex = testIndex + 1;
report.testCount = tests.length;
reportHandler(report);
continuation('ok');
},
doTearDown: function(continuation) { // exceptions in setup/teardown are not reported correctly
try {
// perhaps should pass continuation to tearDown as well
tearDown.call(context);
continuation('ok');
} catch(e) {
continuation('ko');
}
},
nextTest: function(continuation) {
testIndex += 1;
tests[testIndex] ? continuation('ok') : continuation('ko');
},
finished: function(continuation) {
if(onTestRunFinished)
onTestRunFinished();
}
}
fsm.go('start', {}, stateHandlers, stateTransitions, []);
}
}
function _defaultReportHandler(report) {
if(report.result == 'success')
return;
var printout = '';
printout += 'Test ' + report.testIndex + '/' + report.testCount + ': ';
printout += report.testDescription + '\n';
printout += report.result.toUpperCase();
if(report.exception) {
printout += ': ' + report.exception + '\n';
printout += _formatStackTrace1(report.exception);
}
printout += '\n';
if(typeof(repl) == 'object')
repl.print(printout);
else
dump(printout);
}