-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselftests.js
341 lines (317 loc) · 7.23 KB
/
selftests.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
// TODO write a test that verifies that the test run in the order as added.
// Write tets for setUp and tearDown
//*
doh.register("Synchronously written tests.",
[
//
// assertTrue
//
{
name:"fail: assertTrue",
test:function(t){
t.assertTrue(false);
}
},{
name:"success: assertTrue",
test:function(t){
t.assertTrue(true);
}
},
//
// assertFalse
//
{
name:"fail: assertFalse",
test:function(t){
t.assertFalse(true);
}
},{
name:"success: assertFalse",
test:function(t){
t.assertFalse(false);
}
},
//
// assertEqual
//
{
name:"fail: assertEqual bools",
test:function(t){
t.assertEqual(true, false);
}
},{
name:"success: assertFalse bools",
test:function(t){
t.assertEqual(true, true);
}
},
{
name:"fail: assertEqual numbers",
test:function(t){
t.assertEqual(1, "2");
}
},{
name:"success: assertEqual numbers",
test:function(t){
t.assertEqual(1, "1");
}
},
{
name:"fail: assertEqual arrays",
test:function(t){
t.assertEqual([1,2], [2,1]);
}
},{
name:"success: assertEqual arrays",
test:function(t){
t.assertEqual([2,3], [2,3]);
}
},
{
// A missing assert call.
name:"error: timeout, assert() missing",
test:function(t){
}
},{
// This test will fail, because it has no implementation of the test method.
name:"error: test() missing"
}
]
);
doh.register("Asynchronous tests.",
[
{
// Inside of an asynch test case you can (and should) still use the assert() methods.
// No return necessary anymore!!!
name:"fail: simple asynch",
timeout:2000,
test:function(t){
setTimeout(function(){
t.assertTrue(false);
}, 1000);
}
},
{
name:"success: simple asynch",
timeout:2000,
test:function(t){
setTimeout(function(){
t.assertTrue(true);
}, 1000);
}
},
{
name:"error: timeout",
timeout:100, // This timeout is shorter than the setTimeout below, this test should fail.
test:function(t){
setTimeout(function(){
t.assertTrue(true);
}, 1000);
}
},
]
);
var timeDiff;
doh.register("Test doh.pause()",
[
{
// Test that calling pause() from inside a test does pause the test
// suite and do also test that run() does continue.
name:"success: pause after this test (and run again)",
test:function(t){
t.assertTrue(true);
doh.pause();
timeDiff = new Date().getTime();
setTimeout(function(){doh.run()}, 3500);
}
},
{
// This test basically measures the time that the test before had
// paused the test execution and it should be between 3-4 secs.
name:"success: measure paused time",
test:function(t){
var diff = new Date().getTime() - timeDiff
t.assertTrue(diff > 3000 && diff < 4000, "The pause should be between 3-4 seconds.");
}
},
]
);
//
// Test "config" parameter.
//
doh.register("Config parameter tests",
[
{
name:"success: 'testFunctionName'",
setUp:function(){
this._actualTestFunctionName = doh.config.testFunctionName;
doh.config.testFunctionName = "myTestFunctionName";
},
myTestFunctionName:function(t){
t.assertTrue(true);
},
tearDown:function(){
doh.config.testFunctionName = this._actualTestFunctionName;
}
},
{
name:"success: reset 'testFunctionName'",
test:function(t){
t.assertTrue(true);
}
}
]
);
// When writing a GUI for the tests it happens that you also want to show the
// test cases that succeeded and maybe with what value, that is what the return
// values are for.
// E.g.
// test:function(){
// assertEqual(expectedGeoLocation, actualGeoLocation);
// return actualGeoLocation;
// }
// Returning the actual value allows the doh.ui methods to show this value to the user.
(function(){
var testObject = {
// We will check if this object has the return value set after the test.
name:"success: Simple return",
test:function(t){
t.assertTrue(true);
return "jojo";
}
};
doh.register("Return values",
[
testObject,
{
name:"success: Verify return value from last test",
test:function(t){
t.assertEqual(testObject.result, "jojo");
}
},
]
);
// Test a bug that was in the tests, which didn't pass the value returned by a test
// to the doh.ui.testFinished() function, because that function was called BEFORE
// the returned value was set into the test data. A pretty tricky asynch
// problem. The following is the test to verify that it is fixed.
var resultValue = null,
oldTestFinished;
doh.register("Return value, asynch bug",
[
{
name:"success: Verify return value from last test",
setUp:function(){
// We have to override the testFinished() before we call assert()
// because with the bug assert() triggered the testFinished()
// before return was executed.
oldTestFinished = doh.ui.testFinished; // Backup the old testFinished().
doh.ui.testFinished = function(group, test){
resultValue = test.result;
oldTestFinished.apply(doh.ui, arguments);
}
},
test:function(t){
t.assertTrue(true);
return "EXPECT ME";
}
},
{
name:"success: Verify result from last test.",
setUp:function(){
doh.ui.testFinished = oldTestFinished;
},
test:function(t){
t.assertEqual("EXPECT ME", resultValue);
}
}
]
);
})();
// If the test contains asynch parts you can set the "result" property of the test explicitly instead
// of returning a value, like so.
(function(){
var testObject = {
// We will check if this object has the return value set after the test.
name:"success: Simple return",
test:function(t){
setTimeout(function(){
t.assertTrue(true);
t.result = "jaja";
}, 100);
}
};
doh.register("Return values in asynch test",
[
testObject,
{
name:"success: Verify result value from last test",
test:function(t){
t.assertEqual(testObject.result, "jaja");
}
},
]
);
})();
//*/
/*
doh.register("Multiple asserts",
[
{
name:"success: some asserts",
test:function(t){
t.assertTrue(true);
t.assertEqual(1, 1);
t.assertError(new Error());
}
},
{
name:"fail: last assert fails",
test:function(t){
t.assertTrue(true);
t.assertEqual(1, 1);
t.assertError(new Error());
// FIXXXME multiple asserts dont work yet :(
t.assertTrue(false);
}
},
{
name:"fail: last assert fails",
timeout:12*1000,
test:function(t){
t.assertTrue(true);
setTimeout(function(){
t.numAsserts++;
t.assertTrue(false);
}, 10 * 1000);
}
},
]
);
write a test which tests that the test is aborted in the place where the first failure occurs, e.g.
assertTrue(false);
window = undefined; // this should NEVER be executed because the test function should be aborted above!!!!!!
assert(undefined, window);
//*/
(function(){
doh.register("setUp()/tearDown() throw an error", [
{
name:"error: setUp() throws an error, test won't execute.",
setUp:function(){
throw new Error("Nasty error.");
},
test:function(t){
t.assertTrue(true);
}
},
{
name:"success: tearDown() throws an error ... not sure what to do",
test:function(t){
t.assertTrue(true);
},
tearDown:function(){
throw new Error("Nasty error.");
}
}
]);
})();