-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathUnitTest.js
More file actions
79 lines (74 loc) · 2.07 KB
/
UnitTest.js
File metadata and controls
79 lines (74 loc) · 2.07 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
// @ts-nocheck
const TEST_ENTRIES = new Map();
test('godot', typeof godot === 'object', 'core');
test('new Object', () => {
let obj = new godot.Object();
let ok = obj && obj instanceof godot.Object;
obj.free();
return ok;
}, 'core');
test('new Resource', () => {
let obj = new godot.Resource();
let ok = obj && obj.get_class() === 'Resource';
return ok;
}, 'core');
// --------------------------- Unit Test Implementation ------------------------
function test(description, blcok, group = 'default') {
const entries = TEST_ENTRIES.get(group) || [];
entries.push({ description, blcok });
TEST_ENTRIES.set(group, entries);
}
function runEntry(entry) {
return new Promise((resolve, reject) => {
switch (typeof (entry.blcok)) {
case 'boolean':
return resolve(entry.blcok);
case 'function': {
try {
resolve(entry.blcok());
} catch (error) {
console.error(error);
resolve(false);
}
}
break;
case 'object':
if (entry.blcok instanceof Promise) {
entry.blcok.then(() => {
resolve(true);
}).catch(err => {
resolve(false);
});
}
break;
default:
return resolve(false);
}
});
}
async function run() {
let count = 0;
let passed = 0;
for (const [group, entries] of TEST_ENTRIES) {
console.log(`Start test ${group}`);
count += entries.length;
let groupCount = 0;
for (let i = 0; i < entries.length; i++) {
const entry = entries[i];
const ret = await runEntry(entry);
if (ret) {
passed++;
groupCount++;
console.log(`\t[${i + 1}/${entries.length}][ √ ] ${entry.description}`);
}
else {
console.error(`\t[${i + 1}/${entries.length}][ X ] ${entry.description}`);
}
}
const logFunc2 = groupCount == entries.length ? console.log : console.warn;
logFunc2(`\tTest ${group} finished: ${groupCount}/${entries.length} passed`);
}
const logFunc = passed == count ? console.log : console.warn;
logFunc(`Test complete: ${passed}/${count} passed`);
}
const _ = run(); // QuickJS BUG ? 返回的是一个 Promise 对象,不赋给临时变量,会导致 Promise 对象无法被 GC