-
-
Notifications
You must be signed in to change notification settings - Fork 227
/
test.js
52 lines (46 loc) · 1.29 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
'use strict';
const assert = require('node:assert').strict;
const COLOR_ERROR = '\x1b[0;31m';
const COLOR_INFO = '\x1b[1;37m';
const COLOR_NORM = '\x1b[0m';
const logger = (color) => (s) => {
console.log(color + s + COLOR_NORM);
};
logger.error = logger(COLOR_ERROR);
logger.info = logger(COLOR_INFO);
const serialize = (args) => [...args].map((x) => {
const type = typeof x;
if (type === 'object') return JSON.stringify(x);
if (type === 'string') return `'${x}'`;
if (type === 'number') return x.toString();
return x.toString();
}).join(', ');
module.exports = (cases) => (fn) => {
let passed = 0;
for (const [args, expected] of cases) {
const msg = `Case: ${fn.name}(${serialize(args)}) ->`;
let res, result;
try {
res = fn(...args);
result = JSON.stringify(res);
} catch (err) {
logger.error(`${msg} ${result}, exception: ${err.stack}`);
continue;
}
if (typeof expected === 'function') {
if (!expected(res)) {
logger.error(`${msg} ${result}`);
}
passed++;
continue;
}
try {
assert.deepEqual(res, expected);
passed++;
} catch {
const ex = JSON.stringify(expected);
logger.error(`${msg} ${result}, expected: ${ex}`);
}
}
logger.info(`Passed: ${passed} of ${cases.length}`);
};