-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.test.ts
49 lines (38 loc) · 1.64 KB
/
run.test.ts
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
import { parseReplayFromRepString } from './src/techmino-replay-parser';
import { type GameReplayData } from './src/types';
import { readFileSync, readdirSync } from 'fs';
const replayFiles = readdirSync('./tests', {
withFileTypes: false
}) as string[];
console.log(`Running ${replayFiles.length} tests...`);
const promises: Promise<string>[] = replayFiles.map(async (filename) => {
const test = JSON.parse(readFileSync(`./tests/${filename}`).toString()) as Record<string, any>;
const replayStr = test.replay;
const expected = test.expected as GameReplayData;
const replay = await parseReplayFromRepString(replayStr);
for(const key in expected) {
switch(typeof expected[key]) {
case 'number':
case 'string':
case 'boolean':
if (expected[key] !== replay[key]) {
return `FAIL: ${filename} - ${key} is ${replay[key]} but expected ${expected[key]}`;
}
break;
case 'object':
if (JSON.stringify(expected[key]) !== JSON.stringify(replay[key])) {
return `FAIL: ${filename} - ${key} is ${JSON.stringify(replay[key])} but expected ${JSON.stringify(expected[key])}`;
}
break;
}
}
return `PASS: ${filename}`;
})
const results: string[] = await Promise.all(promises);
const fails = results.filter((r) => r.startsWith('FAIL'));
if (fails.length > 0) {
console.error(fails.join('\n'));
console.error('\n\n\n');
throw new Error(`${fails.length} tests failed\n${results.length - fails.length} tests passed`);
}
console.log('All tests passed!');