-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
347 lines (320 loc) · 7.6 KB
/
index.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
/**
* Testing library.
* (C) 2013 Alex Fernández.
*/
import {runAll} from './lib/runner.js'
import util from 'util'
const GREEN = '\u001b[32m';
const RED = '\u001b[1;31m';
const BLACK = '\u001b[0m';
let errors = 0;
/**
* Reports a success for the current test. Parameters:
* - message (optional): the message to show. Default: true.
* - callback (optional): function to call. If not present, just show the message.
* The function must be in node.js style: callback(error, result).
* In this case, callback(null, message).
*/
export function success(message, callback) {
const parameters = processParameters(arguments);
message = parameters.message || true;
callback = parameters.callback;
if (callback)
{
return callback(null, message);
}
if (errors)
{
// previous errors detected
error('With errors: ' + message);
}
else
{
notice(message);
}
}
/**
* Reports a failure for the current test. Parameters:
* - message (optional): the message to show. Default: '.'.
* - callback (optional): function to call. If not present, just show the message.
* The function must be in node.js style: callback(error, result).
* In this case, callback(message).
*/
export function failure(message, callback) {
errors += 1;
const parameters = processParameters(arguments);
message = parameters.message || 'Failure';
callback = parameters.callback;
if (callback)
{
return callback(message);
}
error(message);
}
export function fail(...args) {
return failure(...args)
}
export function removeError() {
errors -= 1
}
/**
* Find a callback in any parameter, extract the message. Parameters:
* - args: an array like, will be sanitized before util.format() is used to get the message.
*/
function processParameters(args)
{
const parameters = {};
if (!arguments[0])
{
return parameters;
}
const reargs = [];
for (const i in args)
{
const arg = args[i];
if (typeof arg == 'function')
{
parameters.callback = arg;
}
else
{
reargs.push(arg);
}
}
parameters.message = util.format.apply(util, reargs);
return parameters;
}
/**
* Assert a condition, and show a failure otherwise.
*/
export function verify(condition, message, callback) {
if (condition)
{
return;
}
delete arguments[0];
const parameters = processParameters(arguments);
message = parameters.message || 'Assertion error';
callback = parameters.callback;
// show failure with the given arguments
failure(message, callback);
}
export function assert(...args) {
return verify(...args)
}
/**
* Assert that two values are equal, and show a failure otherwise.
*/
export function equals(actual, expected, message, callback) {
if (actual == expected)
{
return;
}
if (JSON.stringify(actual) == JSON.stringify(expected))
{
// equal JSON => equal inputs
return;
}
delete arguments[0];
delete arguments[1];
const parameters = processParameters(arguments);
message = parameters.message || 'Assertion for equality error';
message = util.format('%s: expected %s but got %s', message, util.inspect(expected), util.inspect(actual));
callback = parameters.callback;
failure(message, callback);
}
export function assertEquals(...args) {
return equals(...args);
}
/**
* Assert that two values are *not* equal, and show a failure otherwise.
*/
export function notEquals(actual, unexpected, message, callback) {
if (actual != unexpected)
{
if (JSON.stringify(actual) != JSON.stringify(unexpected))
{
// different JSON => different inputs
return;
}
}
delete arguments[0];
delete arguments[1];
const parameters = processParameters(arguments);
message = parameters.message || 'Assertion for inequality error';
message = util.format('%s: expected %s different from %s', message, util.inspect(actual), util.inspect(unexpected));
callback = parameters.callback;
failure(message, callback);
}
export function assertNotEquals(...args) {
return notEquals(...args);
}
export function contains(container, piece, message, callback) {
if (typeof container == 'string')
{
if (container.indexOf(piece) != -1)
{
return;
}
}
else if (Array.isArray(container))
{
for (let i = 0; i < container.length; i++)
{
if (container[i] == piece)
{
return;
}
}
}
else
{
message = 'Invalid container ' + typeof container + ', should be string or array, cannot check ' + message;
return failure(message, callback);
}
delete arguments[0];
delete arguments[1];
const parameters = processParameters(arguments);
message = parameters.message || 'Assertion for equality error';
message = util.format('%s: %s does not contain %s', message, util.inspect(container), util.inspect(piece));
failure(message, parameters.callback);
}
/**
* Check that the error is falsy, show a failure otherwise.
*/
export function check(error, message, callback) {
if (!error)
{
return;
}
delete arguments[0];
const parameters = processParameters(arguments);
let description = util.inspect(error);
if (error.stack)
{
description = error.stack;
}
message = parameters.message + ': ' + description;
callback = parameters.callback;
// show failure with the given arguments
failure(message, callback);
}
/**
* Run a set of tests. Parameters:
* - tests: an object with an attribute for every test function,
* an array or a single function.
* - timeout: an optional timeout to consider tests as failed.
* - callback: an optional function to call after tests have finished.
*/
export function run(tests, timeout, callback) {
if (typeof timeout == 'function')
{
callback = timeout;
timeout = 0;
}
if (!callback)
{
callback = show
}
if (typeof tests == 'function')
{
tests = [tests];
}
let nTests = 0;
for (const key in tests)
{
if (Object.hasOwn(tests, key))
{
nTests += 1;
}
}
// if no timeout, give each test one second
timeout = timeout || 1000 * nTests;
// start the timer
const running = setTimeout(function()
{
const message = 'Package tests did not call back';
error(message);
if (callback)
{
return callback(message);
}
}, timeout);
// run the tests
runAll(tests, function(error, result)
{
clearTimeout(running);
if (callback)
{
return callback(error, result);
}
});
}
/**
* Show the result of some tests. Parameters:
* - error: when tests have failed, an error message.
* - result: when tests have succeeded, the whole results.
*/
export function show(error, result) {
showResults(error, result);
}
/**
* Show the complete hierarchical error results.
*/
export function showComplete(error, result) {
console.log('Complete test results: %s', result);
showResults(error, result);
}
/**
* Show test results.
*/
function showResults(error, result)
{
if (error)
{
failure(error);
process.exit(1);
return;
}
let printable = 'No test result';
if (typeof result == 'string')
{
printable = result;
}
else if (result && result.getSummary)
{
printable = result.getSummary();
}
const elapsed = result.elapsedMs / 1000
console.log(`All tests run in ${elapsed.toFixed(1)} seconds with ${printable}`);
if (result.failure)
{
process.exit(1);
}
}
/**
* Pass a tester callback whose results you want shown.
* Returns a function that runs the tests, shows the results and invokes the callback.
*/
export function toShow(tester) {
return function(callback)
{
tester(function(error, result)
{
showResults(error, result);
callback(error, result);
});
};
}
function error(message) {
console.error(RED + message + BLACK);
}
function notice(message) {
console.log(GREEN + message + BLACK);
}
const testing = {
success, failure, fail, removeError, verify, assert,
equals, assertEquals, notEquals, assertNotEquals,
contains, check, run, show, showComplete, toShow,
}
export default testing