-
Notifications
You must be signed in to change notification settings - Fork 19
/
test.html
208 lines (201 loc) · 5.77 KB
/
test.html
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<!doctype html>
<html>
<head>
<style>
body {
font-family: sans-serif;
}
.hexy {
font-family: monospace;
}
#totals {
font-weight: bold;
padding: 1em;
text-align: center;
}
.whole {
border: 2px solid;
border-radius: .5em;
padding: 1em;
width: 100%;
text-align: left;
}
.active, .whole:hover {
background-color: azure;
}
.content {
display: flex;
overflow: hidden;
padding: .2em;
width: 100%;
}
.box {
border: 2px solid;
border-radius: .3em;
}
.input-box {
overflow: hidden;
width: 30%;
}
.flags-box {
flex-grow: 1;
width: 10%;
}
.expected-box {
flex-grow: 1;
width: 30%;
}
.result-box {
flex-grow: 1;
width: 30%;
}
.variable {
color: black;
font-family: monospace;
}
.pass {
color: green;
border-color: green;
}
.fail {
border-color: red;
color: red;
}
.space {
color: lightgray;
}
.key {
font-weight: bold;
font-style: italic;
}
.string {
color: green;
}
.number, .boolean, .null {
color: blue;
}
</style>
<script src="hexy.js"></script>
<script src="test/testcases.js"></script>
<script type="text/javascript">
var test_root = null;
var template_node;
var testcase_id = [ 0, 0 ];
var executed = 0, failed = 0;
function prettyPrintString(str) {
return str.replace(/\n/g, '<br/>')
.replace(/ /g, '<span class="space">.</span>');
}
function prettyPrintObject(object) {
const json = JSON.stringify(object, undefined, 1)
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/{/g, '{<br>')
.replace(/}/g, '<br>}')
.replace(/,/g, ',<br>');
return json.replace(/("(\u[a-zA-Z0-9]{4}|\[^u]|[^\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g,
function (match) {
var cls = 'number';
if (/^"/.test(match)) {
if (/:$/.test(match)) {
cls = 'key';
} else {
cls = 'string';
}
} else if (/true|false/.test(match)) {
cls = 'boolean';
} else if (/null/.test(match)) {
cls = 'null';
}
return '<span class="' + cls + '">' + match + '</span>';
});
}
function prettyPrint(obj) {
return typeof obj === 'string'
? '"<span class="string">' + prettyPrintString(obj) + '</span>"'
: prettyPrintObject(obj);
}
function check(input, flags, expected, result) {
var testcase_node = template_node.content.cloneNode(true);
var whole = testcase_node.querySelector(".whole");
var header = testcase_node.querySelector(".header");
var content = testcase_node.querySelector(".content");
header.innerHTML = "test " + testcase_id.toString() + ": ";
testcase_node.querySelector(".input").innerHTML = prettyPrint(input);
testcase_node.querySelector(".flags").innerHTML = prettyPrintObject(flags);
testcase_node.querySelector(".expected").innerHTML = prettyPrintString(expected);
testcase_node.querySelector(".result").innerHTML = prettyPrintString(result);
if (expected !== result) {
failed++;
header.innerHTML += "FAIL";
whole.className += " fail";
} else {
header.innerHTML += "PASS";
whole.className += " pass";
content.style.display = "none";
}
test_root.appendChild(testcase_node);
executed++;
}
function pageLoaded() {
totals_root = document.querySelector("#totals");
test_root = document.querySelector("#test");
template_node = document.getElementById("testcase");
for (var tc = 0; tc < testcases.length; tc++) {
if ('inputs' in testcases[tc]) {
for (var ii = 0; ii < testcases[tc].inputs.length; ii++) {
testcase_id = [tc, ii];
check(testcases[tc].inputs[ii],
testcases[tc].params,
testcases[tc].result,
hexy(testcases[tc].inputs[ii], testcases[tc].params));
}
} else {
testcase_id = tc;
check(testcases[tc].input,
testcases[tc].params,
testcases[tc].result,
hexy(testcases[tc].input, testcases[tc].params));
}
}
totals_root.innerHTML = "Executed: " + executed + ", Failed: " + failed;
}
function collapse() {
var content = event.srcElement.querySelector(".content");
if (content.style.display === "none") {
content.style.display = "flex";
} else {
content.style.display = "none";
}
}
</script>
</head>
<body onload='pageLoaded();'>
<div id="totals"></div>
<div id="test"></div>
<template id='testcase'>
<fieldset class='whole' onclick='collapse();'>
<legend class='header'>PASS</legend>
<div class='content'> <!-- this div collapses-->
<fieldset class='input-box box'>
<legend>input</legend>
<div class='input variable'></div>
</fieldset>
<fieldset class='flags-box box'>
<legend>flags</legend>
<div class='flags variable'></div>
</fieldset>
<fieldset class='expected-box box'>
<legend>expected</legend>
<div class='expected variable'></div>
</fieldset>
<fieldset class='result-box box'>
<legend>result</legend>
<div class='result variable'></div>
</fieldset>
</div>
</fieldset>
</template>
</body>
</html>