-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathsmd_test_setup.js
More file actions
340 lines (304 loc) · 9.37 KB
/
smd_test_setup.js
File metadata and controls
340 lines (304 loc) · 9.37 KB
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import * as t from "node:test"
import * as assert from "node:assert/strict"
import * as smd from "./smd.js"
/**
* @typedef {(string | Test_Renderer_Node)[]} Children
* @typedef {Map<Test_Renderer_Node, Test_Renderer_Node>} Parent_Map
* @typedef {{[key in smd.Attr]?: string}} Node_Attrs
*
* @typedef {object} Test_Renderer_Data
* @property {Test_Renderer_Node} root
* @property {Test_Renderer_Node} node
* @property {Parent_Map } parent_map
*
* @typedef {object} Test_Renderer_Node
* @property {smd.Token } type
* @property {Children } children
* @property {Node_Attrs=} attrs
*
* @typedef {smd.Renderer <Test_Renderer_Data>} Test_Renderer
* @typedef {smd.Renderer_Add_Token<Test_Renderer_Data>} Test_Add_Token
* @typedef {smd.Renderer_End_Token<Test_Renderer_Data>} Test_End_Token
* @typedef {smd.Renderer_Add_Text <Test_Renderer_Data>} Test_Add_Text
* @typedef {smd.Renderer_Set_Attr <Test_Renderer_Data>} Test_Set_Attr
*/
/** @returns {Test_Renderer} */
function test_renderer() {
/** @type {Test_Renderer_Node} */
const root = {
type : smd.Token.Document,
children: []
}
return {
add_token: test_renderer_add_token,
end_token: test_renderer_end_token,
set_attr : test_renderer_set_attr,
add_text : test_renderer_add_text,
data : {
parent_map: new Map(),
root : root,
node : root,
},
}
}
/** @type {Test_Add_Token} */
function test_renderer_add_token(data, type) {
/** @type {Test_Renderer_Node} */
const node = {type, children: []}
data.node.children.push(node)
data.parent_map.set(node, data.node)
data.node = node
}
/** @type {Test_Add_Text} */
function test_renderer_add_text(data, text) {
if (typeof data.node.children[data.node.children.length - 1] === "string") {
data.node.children[data.node.children.length - 1] += text
} else {
data.node.children.push(text)
}
}
/** @type {Test_End_Token} */
function test_renderer_end_token(data) {
const parent = data.parent_map.get(data.node)
assert.notEqual(parent, undefined, "Parent not found")
data.node = /** @type {Test_Renderer_Node} */(parent)
}
/** @type {Test_Set_Attr} */
function test_renderer_set_attr(data, type, value) {
if (data.node.attrs === undefined) {
data.node.attrs = {[type]: value}
} else {
data.node.attrs[type] = value
}
}
/** @type {Test_Renderer_Node} */
export const BR = {
type : smd.Token.Line_Break,
children: []
}
const ANSI_GRAY = "\u001b[30m"
const ANSI_RED = "\u001b[31m"
const ANSI_GREEN = "\u001b[32m"
const ANSI_PURPLE = "\u001b[35m"
const ANSI_CYAN = "\u001b[36m"
const ANSI_RESET = "\u001b[0m"
/**
* @param {number} len
* @param {number} h
* @returns {string} */
function compare_pad(len, h) {
let txt = ""
if (h < 0) {
txt += ANSI_RED
} else if (h > 0) {
txt += ANSI_GREEN
} else {
txt += ANSI_GRAY
}
for (let i = 0; i <= len; i += 1) {
txt += ": "
}
txt += ANSI_RESET
return txt
}
/**
* @param {string } text
* @param {string[]} lines
* @param {number } len
* @param {number} h
* @returns {void } */
function compare_push_text(text, lines, len, h) {
lines.push(compare_pad(len, h) + JSON.stringify(text))
}
/**
* @param {Test_Renderer_Node} node
* @param {string[]} lines
* @param {number} len
* @param {number} h
* @returns {void} */
function compare_push_node(node, lines, len, h) {
compare_push_type(node.type, lines, len, h)
for (const child of node.children) {
if (typeof child === "string") {
compare_push_text(child, lines, len + 1, h)
} else {
compare_push_node(child, lines, len + 1, h)
}
}
}
/**
* @param {smd.Token} type
* @param {string[]} lines
* @param {number} len
* @param {number} h
* @returns {void} */
function compare_push_type(type, lines, len, h) {
lines.push(compare_pad(len, h) + ANSI_CYAN + smd.token_to_string(type) + ANSI_RESET)
}
/**
* @param {string | Test_Renderer_Node | undefined} actual
* @param {string | Test_Renderer_Node | undefined} expected
* @param {string[]} lines
* @param {number} len
* @returns {boolean} */
function compare_child(actual, expected, lines, len) {
if (actual === undefined) {
if (expected === undefined) return true
if (typeof expected === "string") {
compare_push_text(expected, lines, len, -1)
} else {
compare_push_node(expected, lines, len, -1)
}
return false
}
if (expected === undefined) {
if (typeof actual === "string") {
compare_push_text(actual, lines, len, +1)
} else {
compare_push_node(actual, lines, len, +1)
}
return false
}
if (typeof actual === "string") {
if (typeof expected === "string") {
if (actual === expected) {
compare_push_text(expected, lines, len, 0)
return true
}
compare_push_text(actual, lines, len, +1)
compare_push_text(expected, lines, len, -1)
return false
}
compare_push_text(actual, lines, len, +1)
compare_push_node(expected, lines, len, -1)
return false
}
if (typeof expected === "string") {
compare_push_text(expected, lines, len, -1)
compare_push_node(actual, lines, len, +1)
return false
}
if (actual.type === expected.type) {
compare_push_type(actual.type, lines, len, 0)
} else {
compare_push_type(actual.type, lines, len, +1)
compare_push_type(expected.type, lines, len, -1)
return false
}
let attrs_str_actual = attrs_to_string(actual.attrs)
let attrs_str_expected = attrs_to_string(expected.attrs)
if (attrs_str_actual !== attrs_str_expected) {
compare_push_text(attrs_str_actual, lines, len + 1, +1)
compare_push_text(attrs_str_expected, lines, len + 1, -1)
return false
}
return compare_children(actual.children, expected.children, lines, len + 1)
}
/**
@param {Node_Attrs|undefined} attrs
@returns {string} */
function attrs_to_string(attrs) {
let txt = '('
if (attrs) {
let entries = /** @type {[string, string][]} */(Object.entries(attrs))
for (let i = 0; i < entries.length; i++) {
let [key, value] = entries[i]
txt += smd.attr_to_html_attr(/** @type {*} */(+key))
txt += '='
txt += value
if (i < entries.length-1) {
txt += ', '
}
}
}
txt += ')'
return txt
}
/**
* @param {Children} children
* @param {Children} expected_children
* @param {string[]} lines
* @param {number} len
* @returns {boolean} */
function compare_children(children, expected_children, lines, len) {
let result = true
let i = 0
for (; i < children.length; i += 1) {
result = compare_child(children[i], expected_children[i], lines, len) && result
}
for (; i < expected_children.length; i += 1) {
compare_child(undefined, expected_children[i], lines, len)
result = false
}
return result
}
/**
* @param {string} str
* @returns {string} */
function display_whitespace(str) {
let txt = ""
for (let i = 0; i < str.length; i += 1) {
let c = str[i]
switch (c) {
case ' ': txt += ANSI_GRAY + "·" + ANSI_RESET ;break
case '\n': txt += ANSI_GRAY + "↵\n" + ANSI_RESET ;break
case '\r': txt += ANSI_GRAY + "↵" + ANSI_RESET ;break
case '\t': txt += ANSI_GRAY + "⇥" + ANSI_RESET ;break
default:
if (c < ' ') {
txt += ANSI_GRAY+"\\x"+c.charCodeAt(0).toString(16).padStart(2, '0')+ANSI_RESET
} else {
txt += c
}
}
}
return txt
}
/**
* @param {Children} children
* @param {Children} expected_children
* @param {string } markdown
* @returns {void} */
function assert_children(children, expected_children, markdown) {
/** @type {string[]} */
let lines = []
if (!compare_children(children, expected_children, lines, 0)) {
let stl = Error.stackTraceLimit
Error.stackTraceLimit = 0
let e = new Error(
ANSI_RED+"Children not equal\n"+ANSI_RESET+
ANSI_PURPLE+"Input:\n"+ANSI_RESET+
display_whitespace(markdown)+
"\n"+
ANSI_PURPLE+"Tokens:\n"+ANSI_RESET+
lines.join("\n")+
"\n"
)
Error.stackTraceLimit = stl
throw e
}
}
/**
* @param {string } title
* @param {string } markdown
* @param {Children} expected_children
* @returns {void}
*/
export function test_single_write(title, markdown, expected_children) {
t.test(title + ";", () => {
const renderer = test_renderer()
const parser = smd.parser(renderer)
smd.parser_write(parser, markdown)
smd.parser_end(parser)
assert_children(renderer.data.root.children, expected_children, markdown)
})
t.test(title + "; by_char;", () => {
const renderer = test_renderer()
const parser = smd.parser(renderer)
for (const char of markdown) {
smd.parser_write(parser, char)
}
smd.parser_end(parser)
assert_children(renderer.data.root.children, expected_children, markdown)
})
}