9ca85d5b1e001815aebb6009132f88f6207b989c.svn-base
6.55 KB
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
/*
* This module is not a plain nodeunit test suite, but instead uses the
* assert module to ensure a basic level of functionality is present,
* allowing the rest of the tests to be written using nodeunit itself.
*
* THIS FILE SHOULD BE BROWSER-COMPATIBLE JS!
* You can use @REMOVE_LINE_FOR_BROWSER to remove code from the browser build.
* Only code on that line will be removed, its mostly to avoid requiring code
* that is node specific
*/
var assert = require('assert'), // @REMOVE_LINE_FOR_BROWSER
async = require('../deps/async'), // @REMOVE_LINE_FOR_BROWSER
nodeunit = require('../lib/nodeunit'); // @REMOVE_LINE_FOR_BROWSER
// NOT A TEST - util function to make testing faster.
// retries the assertion until it passes or the timeout is reached,
// at which point it throws the assertion error
var waitFor = function (fn, timeout, callback, start) {
start = start || new Date().getTime();
callback = callback || function () {};
try {
fn();
callback();
}
catch (e) {
if (e instanceof assert.AssertionError) {
var now = new Date().getTime();
if (now - start >= timeout) {
throw e;
}
else {
async.nextTick(function () {
waitFor(fn, timeout, callback, start);
});
}
}
else {
throw e;
}
}
};
// TESTS:
// Are exported tests actually run? - store completed tests in this variable
// for checking later
var tests_called = {};
// most basic test that should run, the tests_called object is tested
// at the end of this module to ensure the tests were actually run by nodeunit
exports.testCalled = function (test) {
tests_called.testCalled = true;
test.done();
};
// generates test functions for nodeunit assertions
var makeTest = function (method, args_pass, args_fail) {
return function (test) {
var test1_called = false;
var test2_called = false;
// test pass
nodeunit.runTest(
'testname',
function (test) {
test[method].apply(test, args_pass);
test.done();
},
{testDone: function (name, assertions) {
assert.equal(assertions.length, 1);
assert.equal(assertions.failures(), 0);
}},
function () {
test1_called = true;
}
);
// test failure
nodeunit.runTest(
'testname',
function (test) {
test[method].apply(test, args_fail);
test.done();
},
{testDone: function (name, assertions) {
assert.equal(assertions.length, 1);
assert.equal(assertions.failures(), 1);
}},
function () {
test2_called = true;
}
);
// ensure tests were run
waitFor(function () {
assert.ok(test1_called);
assert.ok(test2_called);
tests_called[method] = true;
}, 500, test.done);
};
};
// ensure basic assertions are working:
exports.testOk = makeTest('ok', [true], [false]);
exports.testEquals = makeTest('equals', [1, 1], [1, 2]);
exports.testSame = makeTest('same',
[{test: 'test'}, {test: 'test'}],
[{test: 'test'}, {monkey: 'penguin'}]
);
// from the assert module:
exports.testEqual = makeTest('equal', [1, 1], [1, 2]);
exports.testNotEqual = makeTest('notEqual', [1, 2], [1, 1]);
exports.testDeepEqual = makeTest('deepEqual',
[{one: 1}, {one: 1}], [{one: 1}, {two: 2}]
);
exports.testNotDeepEqual = makeTest('notDeepEqual',
[{one: 1}, {two: 2}], [{one: 1}, {one: 1}]
);
exports.testStrictEqual = makeTest('strictEqual', [1, 1], [1, true]);
exports.testNotStrictEqual = makeTest('notStrictEqual', [true, 1], [1, 1]);
exports.testThrows = makeTest('throws',
[function () {
throw new Error('test');
}],
[function () {
return;
}]
);
exports.testThrowsWithReGex = makeTest('throws',
[function () {
throw new Error('test');
}, /test/],
[function () {
throw new Error('test');
}, /fail/]
);
exports.testThrowsWithErrorValidation = makeTest('throws',
[function () {
throw new Error('test');
}, function(err) {
return true;
}],
[function () {
throw new Error('test');
}, function(err) {
return false;
}]
);
exports.testDoesNotThrows = makeTest('doesNotThrow',
[function () {
return;
}],
[function () {
throw new Error('test');
}]
);
exports.testIfError = makeTest('ifError', [false], [new Error('test')]);
exports.testExpect = function (test) {
var test1_called = false,
test2_called = false,
test3_called = false;
// correct number of tests run
nodeunit.runTest(
'testname',
function (test) {
test.expect(2);
test.ok(true);
test.ok(true);
test.done();
},
{testDone: function (name, assertions) {
test.equals(assertions.length, 2);
test.equals(assertions.failures(), 0);
}},
function () {
test1_called = true;
}
);
// no tests run
nodeunit.runTest(
'testname',
function (test) {
test.expect(2);
test.done();
},
{testDone: function (name, assertions) {
test.equals(assertions.length, 1);
test.equals(assertions.failures(), 1);
}},
function () {
test2_called = true;
}
);
// incorrect number of tests run
nodeunit.runTest(
'testname',
function (test) {
test.expect(2);
test.ok(true);
test.ok(true);
test.ok(true);
test.done();
},
{testDone: function (name, assertions) {
test.equals(assertions.length, 4);
test.equals(assertions.failures(), 1);
}},
function () {
test3_called = true;
}
);
// ensure callbacks fired
waitFor(function () {
assert.ok(test1_called);
assert.ok(test2_called);
assert.ok(test3_called);
tests_called.expect = true;
}, 1000, test.done);
};
// tests are async, so wait for them to be called
waitFor(function () {
assert.ok(tests_called.testCalled);
assert.ok(tests_called.ok);
assert.ok(tests_called.equals);
assert.ok(tests_called.same);
assert.ok(tests_called.expect);
}, 10000);