-
-
Notifications
You must be signed in to change notification settings - Fork 218
/
test.js
executable file
·277 lines (253 loc) · 8.43 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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/usr/bin/env node
"use strict";
var fs = require('fs');
var commonmark = require('../lib/index.js');
// Home made mini-version of the npm ansi module:
var escSeq = function(s) {
return function (){
process.stdout.write('\u001b' + s);
return this;
};
};
var repeat = function(pattern, count) {
if (count < 1) {
return '';
}
var result = '';
while (count > 1) {
if (count & 1) {
result += pattern;
}
count >>= 1;
pattern += pattern;
}
return result + pattern;
};
var cursor = {
write: function (s) {
process.stdout.write(s);
return this;
},
green: escSeq('[0;32m'),
red: escSeq('[0;31m'),
cyan: escSeq('[0;36m'),
reset: escSeq('[0m')
};
var writer = new commonmark.HtmlRenderer();
var reader = new commonmark.Parser();
var readerSmart = new commonmark.Parser({smart: true});
var results = {
passed: 0,
failed: 0
};
var showSpaces = function(s) {
var t = s;
return t.replace(/\t/g, '→')
.replace(/ /g, '␣');
};
var extractSpecTests = function(testfile) {
var data = fs.readFileSync(testfile, 'utf8');
var examples = [];
var current_section = "";
var example_number = 0;
var tests = data
.replace(/\r\n?/g, "\n") // Normalize newlines for platform independence
.replace(/^<!-- END TESTS -->(.|[\n])*/m, '');
tests.replace(/^`{32} example\n([\s\S]*?)^\.\n([\s\S]*?)^`{32}$|^#{1,6} *(.*)$/gm,
function(_, markdownSubmatch, htmlSubmatch, sectionSubmatch){
if (sectionSubmatch) {
current_section = sectionSubmatch;
} else {
example_number++;
examples.push({markdown: markdownSubmatch,
html: htmlSubmatch,
section: current_section,
number: example_number});
}
});
return examples;
};
var specTest = function(testcase, res, converter) {
var markdown = testcase.markdown.replace(/→/g, '\t');
var expected = testcase.html.replace(/→/g, '\t');
var actual = converter(markdown);
if (actual === expected) {
res.passed++;
cursor.green().write('✓').reset();
} else {
res.failed++;
cursor.write('\n');
cursor.red().write('✘ Example ' + testcase.number + '\n');
cursor.cyan();
cursor.write('=== markdown ===============\n');
cursor.write(showSpaces(markdown));
cursor.write('=== expected ===============\n');
cursor.write(showSpaces(expected));
cursor.write('=== got ====================\n');
cursor.write(showSpaces(actual));
cursor.reset();
}
};
var specTests = function(testfile, res, converter) {
cursor.write('Spec tests [' + testfile + ']:\n');
var current_section = "";
var examples = extractSpecTests(testfile);
console.time("Elapsed time");
for (var i = 0; i < examples.length; i++) {
var testcase = examples[i];
if (testcase.section !== current_section) {
if (current_section !== '') {
cursor.write('\n');
}
current_section = testcase.section;
cursor.reset().write(current_section).reset().write(' ');
}
specTest(testcase, results, converter);
}
cursor.write('\n');
console.timeEnd("Elapsed time");
cursor.write('\n');
};
var pathologicalTest = function(testcase, res, converter) {
cursor.write(testcase.name + ' ');
console.time(' elapsed time');
var actual = converter(testcase.input);
if (actual === testcase.expected) {
cursor.green().write('✓\n').reset();
res.passed += 1;
} else {
cursor.red().write('✘\n');
cursor.cyan();
cursor.write('=== markdown ===============\n');
cursor.write(showSpaces(testcase.input));
cursor.write('=== expected ===============\n');
cursor.write(showSpaces(testcase.expected));
cursor.write('=== got ====================\n');
cursor.write(showSpaces(actual));
cursor.write('\n');
cursor.reset();
res.failed += 1;
}
console.timeEnd(' elapsed time');
};
specTests('test/spec.txt', results, function(z) {
return writer.render(reader.parse(z));
});
specTests('test/smart_punct.txt', results, function(z) {
return writer.render(readerSmart.parse(z));
});
specTests('test/regression.txt', results, function(z) {
return writer.render(reader.parse(z));
});
// pathological cases
cursor.write('Pathological cases:\n');
var cases = [
{ name: 'U+0000 in input',
input: 'abc\u0000xyz\u0000\n',
expected: '<p>abc\ufffdxyz\ufffd</p>\n' },
{ name: 'alternate line endings',
input: '- a\n- b\r- c\r\n- d',
expected: '<ul>\n<li>a</li>\n<li>b</li>\n<li>c</li>\n<li>d</li>\n</ul>\n'
}
];
var x;
for (x = 1000; x <= 10000; x *= 10) {
cases.push(
{ name: 'nested strong emph ' + x + ' deep',
input: repeat('*a **a ', x) + 'b' + repeat(' a** a*', x),
expected: '<p>' + repeat('<em>a <strong>a ', x) + 'b' +
repeat(' a</strong> a</em>', x) + '</p>\n' });
}
for (x = 1000; x <= 10000; x *= 10) {
cases.push(
{ name: x + ' emph closers with no openers',
input: repeat('a_ ', x),
expected: '<p>' + repeat('a_ ', x - 1) + 'a_</p>\n' });
}
for (x = 1000; x <= 10000; x *= 10) {
cases.push(
{ name: x + ' emph openers with no closers',
input: repeat('_a ', x),
expected: '<p>' + repeat('_a ', x - 1) + '_a</p>\n' });
}
for (x=1000; x <= 10000; x *= 10) {
cases.push(
{ name: x + ' openers and closers multiple of 3',
input: "a**b" + repeat("c* ", x),
expected: '<p>a**b' + repeat('c* ', x - 1) + 'c*</p>\n' });
}
for (x=1000; x <= 10000; x *= 10) {
cases.push(
{ name: x + ' #172',
input: repeat('*_* _ ', x),
expected: '<p>' + repeat('<em>_</em> _ ', x - 1) + '<em>_</em> _</p>\n' });
}
for (x = 1000; x <= 10000; x *= 10) {
cases.push(
{ name: x + ' link closers with no openers',
input: repeat('a] ', x),
expected: '<p>' + repeat('a] ', x - 1) + 'a]</p>\n' });
}
for (x = 1000; x <= 10000; x *= 10) {
cases.push(
{ name: x + ' link openers with no closers',
input: repeat('[a ', x),
expected: '<p>' + repeat('[a ', x - 1) + '[a</p>\n' });
}
for (x = 1000; x <= 10000; x *= 10) {
cases.push(
{ name: x + ' link openers and emph closers',
input: repeat('[ a_ ', x),
expected: '<p>' + repeat('[ a_ ', x - 1) + '[ a_</p>\n' });
}
for (x = 1000; x <= 10000; x *= 10) {
cases.push(
{ name: x + ' mismatched openers and closers',
input: repeat('*a_ ', x),
expected: '<p>' + repeat('*a_ ', x - 1) + '*a_</p>\n' });
}
for (x = 1000; x <= 10000; x *= 10) {
cases.push(
{ name: x + ' pattern [ (](',
input: repeat('[ (](', x),
expected: '<p>' + repeat('[ (](', x) + '</p>\n' });
}
for (x = 1000; x <= 10000; x *= 10) {
cases.push(
{ name: 'nested brackets ' + x + ' deep',
input: repeat('[', x) + 'a' + repeat(']', x),
expected: '<p>' + repeat('[', x) + 'a' + repeat(']', x) +
'</p>\n' });
}
for (x = 1000; x <= 10000; x *= 10) {
cases.push(
{ name: 'nested block quote ' + x + ' deep',
input: repeat('> ', x) + 'a\n',
expected: repeat('<blockquote>\n', x) + '<p>a</p>\n' +
repeat('</blockquote>\n', x) });
}
for (x = 1000; x <= 10000; x *= 10) {
cases.push(
{ name: '[\\\\... ' + x + ' deep',
input: '[' + repeat('\\', x) + '\n',
expected: '<p>' + '[' + repeat('\\', x/2) + '</p>\n'
});
}
// Commented out til we have a fix... see #129
// for (x = 1000; x <= 10000; x *= 10) {
// cases.push(
// { name: '[]( ' + x + ' deep',
// input: repeat('[](', x) + '\n',
// expected: '<p>' + repeat('[](', x) + '</p>\n'
// });
// }
var parse_and_render = function(z) {
return writer.render(reader.parse(z));
};
for (var j = 0; j < cases.length; j++) {
pathologicalTest(cases[j], results, parse_and_render);
}
cursor.write('\n');
cursor.write(results.passed.toString() + ' tests passed, ' +
results.failed.toString() + ' failed.\n');
process.exit(results.failed);