-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsmd_test_setup.js
309 lines (277 loc) · 7.59 KB
/
smd_test_setup.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
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
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: []
}
/**
* @param {number} len
* @param {number} h
* @returns {string} */
function compare_pad(len, h) {
let txt = ""
if (h < 0) {
txt += "\u001b[31m"
} else if (h > 0) {
txt += "\u001b[32m"
} else {
txt += "\u001b[30m"
}
for (let i = 0; i <= len; i += 1) {
txt += ": "
}
txt += "\u001b[0m"
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) + "\u001b[36m" + smd.token_to_string(type) + "\u001b[0m")
}
/**
* @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 {Children} children
* @param {Children} expected_children
* @param {string } markdown
* @returns {void} */
function assert_children(children, expected_children, markdown) {
/** @type {string[]} */
const lines = []
const result = compare_children(children, expected_children, lines, 0)
if (!result) {
const stl = Error.stackTraceLimit
Error.stackTraceLimit = 0
const e = new Error(
"Children not equal\n"+
"Input:\n```\n"+
markdown+
"\n```\n"+
"Tokens:\n"+
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)
})
}