|
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+<!DOCTYPE html>
|
|
|
2
|
+<html>
|
|
|
3
|
+ <head>
|
|
|
4
|
+ <meta charset="utf-8">
|
|
|
5
|
+ <title>Markdown Unit Tests</title>
|
|
|
6
|
+ <link rel="icon" href="data:;base64,iVBORw0KGgo=">
|
|
|
7
|
+ <style type="text/css">
|
|
|
8
|
+ :root {
|
|
|
9
|
+ font-family: sans-serif;
|
|
|
10
|
+ }
|
|
|
11
|
+ .testcase {
|
|
|
12
|
+ border: 1px solid black;
|
|
|
13
|
+ padding: 0.5em 1em;
|
|
|
14
|
+ margin-bottom: 1em;
|
|
|
15
|
+ max-width: 70em;
|
|
|
16
|
+ }
|
|
|
17
|
+ .testcasename {
|
|
|
18
|
+ font-size: 115%;
|
|
|
19
|
+ font-weight: bold;
|
|
|
20
|
+ }
|
|
|
21
|
+ .testcasestatus {
|
|
|
22
|
+ font-weight: bold;
|
|
|
23
|
+ }
|
|
|
24
|
+ .result-untested {
|
|
|
25
|
+ color: #888;
|
|
|
26
|
+ }
|
|
|
27
|
+ .result-testing {
|
|
|
28
|
+ color: black;
|
|
|
29
|
+ }
|
|
|
30
|
+ .result-passed {
|
|
|
31
|
+ color: #090;
|
|
|
32
|
+ }
|
|
|
33
|
+ .result-failed {
|
|
|
34
|
+ color: #a00;
|
|
|
35
|
+ }
|
|
|
36
|
+ .result-errored {
|
|
|
37
|
+ color: #a80;
|
|
|
38
|
+ }
|
|
|
39
|
+ </style>
|
|
|
40
|
+ <script src="js/markdown.js"></script>
|
|
|
41
|
+ <script>
|
|
|
42
|
+ // TODO: Either run tests on a worker or at least on a setTimeout so
|
|
|
43
|
+ // UI can update and not block the thread for the whole duration.
|
|
|
44
|
+ function onLoad() {
|
|
|
45
|
+ let testClasses = [
|
|
|
46
|
+ MarkdownTest,
|
|
|
47
|
+ ];
|
|
|
48
|
+ var tests = [];
|
|
|
49
|
+ for (const testClass of testClasses) {
|
|
|
50
|
+ const instance = new testClass();
|
|
|
51
|
+ if (!(instance instanceof BaseTest)) {
|
|
|
52
|
+ console.error(`${testClass.name} does not extend BaseTest`);
|
|
|
53
|
+ continue;
|
|
|
54
|
+ }
|
|
|
55
|
+ const testCases = TestCaseRun.findTestCases(instance);
|
|
|
56
|
+ tests.push(...testCases);
|
|
|
57
|
+ var classHTML = '<div class="testclass">';
|
|
|
58
|
+ classHTML += `<div class="testclassname">${testClass.name}</div>`;
|
|
|
59
|
+ for (const testCase of testCases) {
|
|
|
60
|
+ classHTML += testCase.toHTML();
|
|
|
61
|
+ }
|
|
|
62
|
+ classHTML += '</div>';
|
|
|
63
|
+ document.getElementById('results').innerHTML += classHTML;
|
|
|
64
|
+ }
|
|
|
65
|
+ var testInterval = setInterval(function() {
|
|
|
66
|
+ if (tests.length == 0) {
|
|
|
67
|
+ clearInterval(testInterval);
|
|
|
68
|
+ testInterval = null;
|
|
|
69
|
+ return;
|
|
|
70
|
+ }
|
|
|
71
|
+ let testCase = tests[0];
|
|
|
72
|
+ if (testCase.result == ResultType.testing) {
|
|
|
73
|
+ tests.splice(0, 1);
|
|
|
74
|
+ testCase.updateHTML();
|
|
|
75
|
+ testCase.run();
|
|
|
76
|
+ testCase.updateHTML();
|
|
|
77
|
+ } else {
|
|
|
78
|
+ testCase.result = ResultType.testing;
|
|
|
79
|
+ testCase.updateHTML();
|
|
|
80
|
+ }
|
|
|
81
|
+ }, 1);
|
|
|
82
|
+ }
|
|
|
83
|
+
|
|
|
84
|
+ /**
|
|
|
85
|
+ * @param {String} text
|
|
|
86
|
+ * @returns {String}
|
|
|
87
|
+ */
|
|
|
88
|
+ function escapeHTML(text) {
|
|
|
89
|
+ return text.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
|
|
90
|
+ }
|
|
|
91
|
+
|
|
|
92
|
+ class ResultType {
|
|
|
93
|
+ static untested = new ResultType('untested');
|
|
|
94
|
+ static testing = new ResultType('testing');
|
|
|
95
|
+ static passed = new ResultType('passed');
|
|
|
96
|
+ static failed = new ResultType('failed');
|
|
|
97
|
+ static errored = new ResultType('errored');
|
|
|
98
|
+
|
|
|
99
|
+ #name;
|
|
|
100
|
+
|
|
|
101
|
+ constructor(name) {
|
|
|
102
|
+ this.#name = name;
|
|
|
103
|
+ }
|
|
|
104
|
+
|
|
|
105
|
+ toString() {
|
|
|
106
|
+ return `${this.constructor.name}.${this.#name}`;
|
|
|
107
|
+ }
|
|
|
108
|
+ }
|
|
|
109
|
+
|
|
|
110
|
+ class FailureError extends Error {
|
|
|
111
|
+ constructor(message=null) {
|
|
|
112
|
+ super(message);
|
|
|
113
|
+ }
|
|
|
114
|
+ }
|
|
|
115
|
+
|
|
|
116
|
+ class BaseTest {
|
|
|
117
|
+ setUp() {}
|
|
|
118
|
+ tearDown() {}
|
|
|
119
|
+ /** @var {TestCaseRun|null} */
|
|
|
120
|
+ currentRunner = null;
|
|
|
121
|
+ fail(failMessage=null) {
|
|
|
122
|
+ throw new FailureError(failMessage || 'failed');
|
|
|
123
|
+ }
|
|
|
124
|
+ assertTrue(test, failMessage=null) {
|
|
|
125
|
+ if (!test) this.fail(failMessage || `expected true, got ${test}`);
|
|
|
126
|
+ }
|
|
|
127
|
+ assertFalse(test, failMessage=null) {
|
|
|
128
|
+ if (test) this.fail(failMessage || `expected false, got ${test}`);
|
|
|
129
|
+ }
|
|
|
130
|
+ assertEqual(a, b, failMessage=null) {
|
|
|
131
|
+ if (a != b) this.fail(failMessage || `equality failed: ${a} != ${b}`);
|
|
|
132
|
+ }
|
|
|
133
|
+ expectError(e=true) {
|
|
|
134
|
+ if (this.currentRunner) this.currentRunner.expectedError = e;
|
|
|
135
|
+ }
|
|
|
136
|
+ /**
|
|
|
137
|
+ * @param {number} maxTime maximum time in seconds
|
|
|
138
|
+ * @param {function} timedCode code to run and time
|
|
|
139
|
+ */
|
|
|
140
|
+ profile(maxTime, timedCode) {
|
|
|
141
|
+ const startTime = performance.now();
|
|
|
142
|
+ const result = timedCode();
|
|
|
143
|
+ const endTime = performance.now();
|
|
|
144
|
+ const seconds = (endTime - startTime) / 1000.0;
|
|
|
145
|
+ if (seconds > maxTime) {
|
|
|
146
|
+ this.fail(`Expected <= ${maxTime}s execution time, actual ${seconds}s.`);
|
|
|
147
|
+ }
|
|
|
148
|
+ return result;
|
|
|
149
|
+ }
|
|
|
150
|
+ }
|
|
|
151
|
+
|
|
|
152
|
+ class TestCaseRun {
|
|
|
153
|
+ static #nextUniqueId = 1;
|
|
|
154
|
+ /** @var {BaseTest} */
|
|
|
155
|
+ #objectUnderTest;
|
|
|
156
|
+ /** @var {method} */
|
|
|
157
|
+ #method;
|
|
|
158
|
+ uniqueId = 0;
|
|
|
159
|
+ /** @var {ResultType} */
|
|
|
160
|
+ result = ResultType.untested;
|
|
|
161
|
+ /** @var {String|null} */
|
|
|
162
|
+ message = null;
|
|
|
163
|
+ expectedError = null;
|
|
|
164
|
+ /** @var {String} */
|
|
|
165
|
+ get className() { return this.#objectUnderTest.constructor.name; }
|
|
|
166
|
+ /** @var {String} */
|
|
|
167
|
+ get methodName() { return this.#method.name; }
|
|
|
168
|
+ /**
|
|
|
169
|
+ * @param {BaseTest} objectUnderTest
|
|
|
170
|
+ * @param {method} method
|
|
|
171
|
+ */
|
|
|
172
|
+ constructor(objectUnderTest, method) {
|
|
|
173
|
+ this.#objectUnderTest = objectUnderTest;
|
|
|
174
|
+ this.#method = method;
|
|
|
175
|
+ this.uniqueId = TestCaseRun.#nextUniqueId++;
|
|
|
176
|
+ }
|
|
|
177
|
+ run() {
|
|
|
178
|
+ try {
|
|
|
179
|
+ this.expectedError = null;
|
|
|
180
|
+ this.#objectUnderTest.currentRunner = this;
|
|
|
181
|
+ this.#objectUnderTest.setUp();
|
|
|
182
|
+ this.#method.bind(this.#objectUnderTest)();
|
|
|
183
|
+ this.result = ResultType.passed;
|
|
|
184
|
+ this.message = null;
|
|
|
185
|
+ } catch (e) {
|
|
|
186
|
+ if (e instanceof FailureError) {
|
|
|
187
|
+ this.result = ResultType.failed;
|
|
|
188
|
+ this.message = e.message;
|
|
|
189
|
+ } else if (this.#isExpectedError(e)) {
|
|
|
190
|
+ this.result = ResultType.passed;
|
|
|
191
|
+ this.message = null;
|
|
|
192
|
+ } else {
|
|
|
193
|
+ this.result = ResultType.errored;
|
|
|
194
|
+ this.message = e.message;
|
|
|
195
|
+ }
|
|
|
196
|
+ } finally {
|
|
|
197
|
+ this.expectedError = null;
|
|
|
198
|
+ try {
|
|
|
199
|
+ this.#objectUnderTest.tearDown();
|
|
|
200
|
+ this.#objectUnderTest.currentRunner = null;
|
|
|
201
|
+ } catch (e0) {
|
|
|
202
|
+ console.error(`Failed to run ${this.className}.tearDown() - ${e0.message}`);
|
|
|
203
|
+ this.result = ResultType.errored;
|
|
|
204
|
+ this.message = e0.message;
|
|
|
205
|
+ }
|
|
|
206
|
+ }
|
|
|
207
|
+ }
|
|
|
208
|
+ #isExpectedError(e) {
|
|
|
209
|
+ if (this.expectedError === null) return false;
|
|
|
210
|
+ if (this.expectedError === true) return true;
|
|
|
211
|
+ // TODO: Have a way to specify details about what kind of error is expected. Maybe a prototype instance and/or a testing lambda.
|
|
|
212
|
+ return false;
|
|
|
213
|
+ }
|
|
|
214
|
+ toHTML() {
|
|
|
215
|
+ var html = `<div class="testcase" id="testcase${this.uniqueId}">`;
|
|
|
216
|
+ html += `<div class="testcasename"><span class="testcaseclass">${this.className}</span>.<span class="testcasemethod">${this.methodName}</span></div>`;
|
|
|
217
|
+ switch (this.result) {
|
|
|
218
|
+ case ResultType.untested:
|
|
|
219
|
+ html += '<div class="testcasestatus result-untested">Waiting to test</div>';
|
|
|
220
|
+ break;
|
|
|
221
|
+ case ResultType.testing:
|
|
|
222
|
+ html += '<div class="testcasestatus result-tesitng">Testing...</div>';
|
|
|
223
|
+ break;
|
|
|
224
|
+ case ResultType.passed:
|
|
|
225
|
+ html += '<div class="testcasestatus result-passed">Passed</div>';
|
|
|
226
|
+ break;
|
|
|
227
|
+ case ResultType.failed:
|
|
|
228
|
+ html += '<div class="testcasestatus result-failed">Failed</div>';
|
|
|
229
|
+ html += `<div class="testcasemessage">${escapeHTML(this.message)}</div>`;
|
|
|
230
|
+ break;
|
|
|
231
|
+ case ResultType.errored:
|
|
|
232
|
+ html += '<div class="testcasestatus result-errored">Errored</div>';
|
|
|
233
|
+ html += `<div class="testcasemessage">${escapeHTML(this.message)}</div>`;
|
|
|
234
|
+ break;
|
|
|
235
|
+ }
|
|
|
236
|
+ html += `</div>`;
|
|
|
237
|
+ return html;
|
|
|
238
|
+ }
|
|
|
239
|
+ updateHTML() {
|
|
|
240
|
+ let existing = document.getElementById(`testcase${this.uniqueId}`);
|
|
|
241
|
+ if (existing) {
|
|
|
242
|
+ existing.outerHTML = this.toHTML();
|
|
|
243
|
+ } else {
|
|
|
244
|
+ document.getElementById('results').innerHTML += this.toHTML();
|
|
|
245
|
+ }
|
|
|
246
|
+ }
|
|
|
247
|
+
|
|
|
248
|
+ /**
|
|
|
249
|
+ * @param {object} instance
|
|
|
250
|
+ * @returns {TestCaseRun[]}
|
|
|
251
|
+ */
|
|
|
252
|
+ static findTestCases(instance) {
|
|
|
253
|
+ if (!(instance instanceof BaseTest)) return [];
|
|
|
254
|
+ var members = [];
|
|
|
255
|
+ var obj = instance;
|
|
|
256
|
+ do {
|
|
|
257
|
+ members.push(...Object.getOwnPropertyNames(obj));
|
|
|
258
|
+ } while (obj = Object.getPrototypeOf(obj));
|
|
|
259
|
+ return members.sort().filter((e, i, arr) => {
|
|
|
260
|
+ if (e != arr[i + 1] && typeof instance[e] == 'function' && e.startsWith('test')) return true;
|
|
|
261
|
+ }).map((name) => {
|
|
|
262
|
+ return new TestCaseRun(instance, instance[name]);
|
|
|
263
|
+ });
|
|
|
264
|
+ }
|
|
|
265
|
+ }
|
|
|
266
|
+
|
|
|
267
|
+ class MarkdownTest extends BaseTest {
|
|
|
268
|
+ test_simpleSingleParagraph() {
|
|
|
269
|
+ let markdown = 'Lorem ipsum';
|
|
|
270
|
+ let expected = '<p>Lorem ipsum</p>';
|
|
|
271
|
+ let actual = Markdown.toHTML(markdown).trim();
|
|
|
272
|
+ this.assertEqual(actual, expected);
|
|
|
273
|
+ }
|
|
|
274
|
+
|
|
|
275
|
+ test_strong() {
|
|
|
276
|
+ let markdown = 'Lorem **ipsum** dolor **sit**';
|
|
|
277
|
+ let expected = '<p>Lorem <strong>ipsum</strong> dolor <strong>sit</strong></p>';
|
|
|
278
|
+ let actual = Markdown.toHTML(markdown).trim();
|
|
|
279
|
+ this.assertEqual(actual, expected);
|
|
|
280
|
+ }
|
|
|
281
|
+
|
|
|
282
|
+ test_emphasis() {
|
|
|
283
|
+ let markdown = 'Lorem _ipsum_ dolor _sit_';
|
|
|
284
|
+ let expected = '<p>Lorem <em>ipsum</em> dolor <em>sit</em></p>';
|
|
|
285
|
+ let actual = Markdown.toHTML(markdown).trim();
|
|
|
286
|
+ this.assertEqual(actual, expected);
|
|
|
287
|
+ }
|
|
|
288
|
+
|
|
|
289
|
+ test_strongEmphasis_easy() {
|
|
|
290
|
+ let markdown = 'Lorem **ipsum *dolor* sit** amet';
|
|
|
291
|
+ let expected = '<p>Lorem <strong>ipsum <em>dolor</em> sit</strong> amet</p>';
|
|
|
292
|
+ let actual = Markdown.toHTML(markdown).trim();
|
|
|
293
|
+ this.assertEqual(actual, expected);
|
|
|
294
|
+ }
|
|
|
295
|
+
|
|
|
296
|
+ test_strongEmphasis_medium() {
|
|
|
297
|
+ let markdown = 'Lorem ***ipsum*** dolor';
|
|
|
298
|
+ let expected1 = '<p>Lorem <strong><em>ipsum</em></strong> dolor</p>';
|
|
|
299
|
+ let expected2 = '<p>Lorem <em><strong>ipsum</strong></em> dolor</p>';
|
|
|
300
|
+ let actual = Markdown.toHTML(markdown).trim();
|
|
|
301
|
+ this.assertTrue(actual == expected1 || actual == expected2);
|
|
|
302
|
+ }
|
|
|
303
|
+
|
|
|
304
|
+ test_strongEmphasis_hard1() {
|
|
|
305
|
+ let markdown = 'Lorem ***ipsum* dolor** sit';
|
|
|
306
|
+ let expected = '<p>Lorem <strong><em>ipsum</em> dolor</strong> sit</p>';
|
|
|
307
|
+ let actual = Markdown.toHTML(markdown).trim();
|
|
|
308
|
+ this.assertEqual(actual, expected);
|
|
|
309
|
+ }
|
|
|
310
|
+
|
|
|
311
|
+ test_strongEmphasis_hard2() {
|
|
|
312
|
+ let markdown = 'Lorem ***ipsum** dolor* sit';
|
|
|
313
|
+ let expected = '<p>Lorem <em><strong>ipsum</strong> dolor</em> sit</p>';
|
|
|
314
|
+ let actual = Markdown.toHTML(markdown).trim();
|
|
|
315
|
+ this.assertEqual(actual, expected);
|
|
|
316
|
+ }
|
|
|
317
|
+
|
|
|
318
|
+ test_inlineCode() {
|
|
|
319
|
+ let markdown = 'Lorem `ipsum` dolor';
|
|
|
320
|
+ let expected = '<p>Lorem <code>ipsum</code> dolor</p>';
|
|
|
321
|
+ let actual = Markdown.toHTML(markdown).trim();
|
|
|
322
|
+ this.assertEqual(actual, expected);
|
|
|
323
|
+ }
|
|
|
324
|
+
|
|
|
325
|
+ test_inlineCode_withInnerBacktick() {
|
|
|
326
|
+ let markdown = 'Lorem ``ip`su`m`` dolor';
|
|
|
327
|
+ let expected = '<p>Lorem <code>ip`su`m</code> dolor</p>';
|
|
|
328
|
+ let actual = Markdown.toHTML(markdown).trim();
|
|
|
329
|
+ this.assertEqual(actual, expected);
|
|
|
330
|
+ }
|
|
|
331
|
+ }
|
|
|
332
|
+
|
|
|
333
|
+ document.addEventListener('DOMContentLoaded', onLoad);
|
|
|
334
|
+ </script>
|
|
|
335
|
+ </head>
|
|
|
336
|
+ <body>
|
|
|
337
|
+ <div id="results"></div>
|
|
|
338
|
+ </body>
|
|
|
339
|
+</html>
|