-
Notifications
You must be signed in to change notification settings - Fork 5
/
browser-test.js
92 lines (76 loc) · 2.24 KB
/
browser-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
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
const fs = require('fs');
const readline = require('readline');
const {extractRange, tryReadJSON} = require('./tests/util');
const cases = [];
const caseRoot = `${__dirname}/tests/cases`;
for (const dir of fs.readdirSync(caseRoot)) {
const {text, index} = extractRange(
fs.readFileSync(`${caseRoot}/${dir}/text.txt`, 'utf8').replace(/\r/g, '')
);
const metadata = tryReadJSON(`${caseRoot}/${dir}/metadata.json`);
const error = tryReadJSON(`${caseRoot}/${dir}/error.json`);
cases.push({
name: dir,
metadata,
text,
error,
errorIndex: index
});
}
const runTest = (usercssMeta, cases) => {
function deepEqual(a, b) {
if (typeof a !== typeof b) {
return false;
}
if (!a || !b || typeof a !== 'object' || typeof b !== 'object') {
return a === b;
}
const bKeys = new Set(Object.keys(b));
for (const key of Object.keys(a)) {
if (!bKeys.has(key)) {
return false;
}
bKeys.delete(key);
if (!deepEqual(a[key], b[key])) {
return false;
}
}
if (bKeys.size) {
return false;
}
return true;
}
for (const case_ of cases) {
console.log(`%c${case_.name}`, 'color: green');
try {
const {metadata} = usercssMeta.parse(case_.text, {mandatoryKeys: []});
console.assert(deepEqual(metadata, case_.metadata), 'metadata mismatch');
} catch (err) {
console.assert(case_.error, 'unexpected error');
if (!case_.error) {
console.error(err);
}
for (const key of Object.keys(case_.error)) {
console.assert(deepEqual(case_.error[key], err[key]), `error ${key} mismatch`);
}
if (err.index != null) {
console.assert(err.index === case_.errorIndex, 'index mismatch');
}
}
}
console.log('done');
};
fs.writeFileSync('browser-test.html', `
<script src="dist/usercss-meta.js"></script>
<script>(${runTest})(usercssMeta, ${JSON.stringify(cases, null, 2)})</script>
`, 'utf8');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('Open browser-test.html and check if all tests have passed (y/N): ', ans => {
if (!/y/i.test(ans)) {
process.exit(1); // eslint-disable-line unicorn/no-process-exit
}
rl.close();
});