-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathtest.js
305 lines (251 loc) · 9.87 KB
/
test.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
/*jslint laxbreak: true, eqeqeq: true, undef: true, regexp: false */
/*global require, process, exports */
var sys = require('sys');
var AssertFailedException = function (msg) {
this.message = msg;
};
var isEqual = function (expected, actual) {
var key, i;
if (typeof actual !== typeof expected) { return false; }
if (actual instanceof RegExp) { return actual.source === expected.source; }
if (actual instanceof Date) { return actual.getTime() === expected.getTime(); }
if (actual instanceof Array) {
if (actual.length !== expected.length) {
return false;
}
for (i = 0; i < expected.length; i++) {
if (!isEqual(expected[i], actual[i])) {
return false;
}
}
return true;
}
// Objects are compared in a sort of "at least equal to"-way, that is, the
// actual object must have the expected properties with the expected
// values, but it is still considered equal if it has some properties on it
// that are not on the expected object.
if (typeof expected === 'object') {
for (key in expected) {
if (expected.hasOwnProperty(key)) {
if (!isEqual(actual[key], expected[key])) { return false; }
}
}
return true;
}
return actual === expected;
};
var testcases = [];
var async_test_has_failed = false;
exports.dsl = {
testcase: function (name) {
testcases.unshift({ name: name, tests: [] });
},
test: function (name, func) {
testcases[0].tests.push({
name: name,
body: function (context, callback) {
try {
func(context);
callback();
} catch (e) {
callback(e);
}
}
});
},
test_async: function (name, func) {
testcases[0].tests.push({ name: name, body: func });
},
setup: function (func) {
testcases[0].setup = func;
},
teardown: function (func) {
testcases[0].teardown = func;
},
run: function (stop_on_error) {
var count = 0, error_cnt = 0, failed_cnt = 0;
var testcase_idx = testcases.length - 1;
(function run_testcases() {
var idx = 0;
var testcase = testcases[testcase_idx--];
if (testcase) {
sys.puts('\n[Testcase: ' + testcase.name + ']');
(function run_tests() {
var test = testcase.tests[idx];
if (test) {
idx = idx + 1;
count = count + 1;
var context = testcase.setup ? testcase.setup() : {};
async_test_has_failed = false;
function handle_result(error) {
if (error) {
async_test_has_failed = true;
if (error instanceof AssertFailedException) {
sys.puts(' [--] ' + test.name + ': failed. ' + error.message);
failed_cnt++;
} else {
sys.print(' [!!] ' + test.name + ': error. ');
if (error.stack && error.type) {
sys.puts(error.type + '\n' + error.stack);
} else {
sys.puts(JSON.stringify(error, 0, 2));
}
error_cnt++;
}
if (stop_on_error) {
sys.puts('stopping on first error');
testcase_idx = -1;
idx = testcase.tests.length;
}
} else {
sys.puts(' [OK] ' + test.name + ': passed');
}
if (testcase.teardown) { testcase.teardown(context); }
process.nextTick(run_tests);
}
try {
test.body(context, handle_result);
} catch (e) {
handle_result(e);
}
} else {
// no more tests
sys.puts('----');
process.nextTick(run_testcases);
}
})();
} else {
// no more testcases
sys.puts('\nTotal: ' + count + ', Failures: ' + failed_cnt + ', Errors: ' + error_cnt + '');
}
})();
},
assertEquals: function (actual, expected, callback) {
if (async_test_has_failed) { return; }
if (!isEqual(actual, expected)) {
var exception = new AssertFailedException(
'\nExpected: ' + sys.inspect(actual) + '\nActual: ' + sys.inspect(expected) + '\n'
);
if (callback) { callback(exception); } else { throw exception; }
};
},
assertIsTrue: function (actual, callback) {
if (async_test_has_failed) { return; }
if (!actual) {
var exception = new AssertFailedException('\nExpected ' + sys.inspect(actual) + ' to be true\n');
if (callback) { callback(exception); } else { throw exception; }
}
},
assertIsFalse: function (actual, callback) {
if (async_test_has_failed) { return; }
if (actual) {
var exception = new AssertFailedException('\nExpected ' + sys.inspect(actual) + ' to be false\n');
if (callback) { callback(exception); } else { throw exception; }
}
},
shouldThrow: function (func, args, this_context, callback) {
if (async_test_has_failed) { return; }
try {
func.apply(this_context, args);
} catch (e) {
var passed = true;
}
if (!passed) {
var exception = new AssertFailedException('No exception was thrown');
if (callback) { callback(exception); } else { throw exception; }
}
},
shouldNotThrow: function (func, args, this_context, callback) {
if (async_test_has_failed) { return; }
try {
func.apply(this_context, args);
} catch (e) {
var exception = new AssertFailedException('Caught <' + e + '>');
if (callback) { callback(exception); } else { throw exception; }
}
},
fail: function (message, callback) {
if (async_test_has_failed) { return; }
var exception = new AssertFailedException(message);
if (callback) { callback(exception); } else { throw exception; }
},
end_async_test: function (callback) {
if (!async_test_has_failed) { callback(); }
}
};
/*
function broken(s) {
return tobis.hest;
}
with (exports.dsl) {
testcase('Testing testsystem')
test('two should equal two', function () {
assertEquals(2, 2);
shouldNotThrow( assertEquals, [2,2] );
})
test('four should not equal two', function () {
shouldThrow( assertEquals, [2,4] );
})
test('broken function should throw', function () {
shouldThrow( broken );
})
test('arrays should be equal', function () {
assertEquals([1,2,3,4], [1,2,3,4]);
})
test('different arrays should not be equal', function () {
shouldThrow( assertEquals, [ [1,2,3,4], [1,2,3] ]);
})
test('regexes should be equal', function () {
assertEquals( /abc/, /abc/ );
})
test('different regexes should not be equal', function () {
shouldThrow( assertEquals, [ /abc/, /edf/ ]);
})
test('dates should be equal', function () {
assertEquals( new Date(376110000000) , new Date(376110000000) );
})
test('different dates should not be equal', function () {
shouldThrow( assertEquals, [ new Date(376110000000), new Date() ]);
})
test('objects should be equal', function () {
assertEquals( {a: 'hest', b: 5, c: [1,2,3]}, {a: 'hest', b: 5, c: [1,2,3]} );
})
test('different objects should not be equal', function () {
shouldThrow( assertEquals, [ {a: 'hest', b: 5, c: [1,2,3]}, {a: 'hest', b: 5, c: [2,3]} ]);
})
testcase('Testing async tests');
test_async('wait then success', function(context, callback) {
setTimeout(callback, 500);
});
test_async('wait then failure', function(context, callback) {
setTimeout(function () { callback("error - but it's okay :-)")}, 500);
});
test_async("this test throws, and should always fail", function () {
throw "it's okay :-)";
});
test_async('wait with assert', function (context, callback) {
setTimeout(function () {
assertEquals('hest', 'hest', callback);
assertEquals(2, 2, callback);
shouldThrow(assertEquals, [2,4], null, callback)
end_async_test( callback );
});
});
testcase('Testing setup and teardown');
var teardown_result = 0;
setup(function () {
return { hest: 1 }
});
teardown(function (context) {
teardown_result = context.hest;
});
test('setup should run before test', function (context) {
assertEquals({ hest: 1 }, context);
context.hest += 1;
});
test('teardown should run after test', function (context) {
assertEquals(2, teardown_result);
});
run();
}
//*/