-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
75 lines (75 loc) · 2.46 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import {addTypeChecks, expandType} from "@runtime-type-inspector/transpiler";
/**
* @param {string} a - Left text.
* @param {string} b - Right text.
* @returns {object} AceDiff editor instance.
*/
function addDiff(a, b) {
const element = document.createElement('div');
document.getElementById("here").innerHTML = '';
document.getElementById("here").append(element);
return new AceDiff({
element,
left: {
content: a,
},
right: {
content: b,
},
});
}
/**
* @param {*} input - Source code to normalize.
* @returns {string} Normalized output.
*/
function normalize(input) {
// Remove multiple newlines into one
// I would rather not do it, but Stringifier needs a bit more love in other areas:
// - multiline array output when elements surpass a max-col option
let output = input.replace(/\n+/g, '\n').trim();
/** @todo Remove bunch of whitespaces from test outputs */
output = output
.split('\n')
.filter(_ => _.trim().length)
.map(_ => _.trim())
.join('\n');
return output;
}
async function main() {
const resp = await fetch("../test/typechecking.json");
const json = await resp.json();
const buttonsDiv = document.getElementById('buttons');
if (!(buttonsDiv instanceof HTMLDivElement)) {
throw new Error("buttons isn't a <div>");
}
const aceDiffer = addDiff('1\n2\n3\n4\n', '1\n2\n33\n4\n');
/** @type {HTMLButtonElement[]} */
const buttons = [];
for (const {input, output} of json) {
const button = document.createElement('button');
buttonsDiv.append(button);
button.innerText = input;
const res = await fetch('../' + input);
const txtInput = await res.text();
const res2 = await fetch('../' + output);
const txtOutput = await res2.text();
const actualResult = addTypeChecks(txtInput, {expandType, addHeader: false});
const success = normalize(actualResult) === normalize(txtOutput);
button.style.backgroundColor = success ? 'lime' : 'red';
button.onclick = async () => {
aceDiffer.editors.left.ace.setValue(txtInput);
aceDiffer.editors.left.ace.clearSelection();
let rightText = txtOutput;
if (!success) {
rightText += `\n\n// Actual result:\n${actualResult}`;
}
aceDiffer.editors.right.ace.setValue(rightText);
aceDiffer.editors.right.ace.clearSelection();
//console.log({input, output});
};
buttons.push(button);
}
buttons[1].onclick();
Object.assign(window, {aceDiffer, json});
}
main();