-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.yue
464 lines (393 loc) · 17.4 KB
/
tests.yue
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
import "leadoc.doc" as leadoc
import "leadoc.converter.snippets"
import "leadoc.converter.tsd"
import "leadoc.validate.lua" as validate_lua
import "leadoc.find"
import "leadoc.json"
-- import "leadoc.lint"
import fast_push, fast_peek, fast_pop, fast_del, fast_iter, fast_extend from require "leadoc.util"
import describe, it, expect, report, exit from require "lester"
describe "leadoc", ->
describe "doc", ->
describe "section", ->
it "section", ->
doc = leadoc.doc => @section "test"
expect.equal doc.tree[1].type, "section"
expect.equal doc.tree[1].data.name, "test"
expect.equal doc.tree[1].tree.n, 0
expect.equal doc.tree.n, 1
it "section errors as child of function", ->
expect.fail(
->
leadoc.doc => @function "test", -> @section "test",
"section cannot be a descendent of function"
)
describe "namespace", ->
it "namespace", ->
doc = leadoc.doc => @namespace "test"
expect.equal doc.tree[1].type, "namespace"
expect.equal doc.tree[1].data.name, "test"
it "namespace with description", ->
doc = leadoc.doc => @namespace "test", -> @description "test"
expect.equal doc.tree[1].data.description, "test"
describe "function", ->
it "function", ->
doc = leadoc.doc => @function "test"
expect.equal doc.tree[1].type, "function"
expect.equal doc.tree[1].data.name, "test"
expect.equal doc.tree[1].data.arguments.n, 0
it "function with arguments", ->
doc = leadoc.doc => @function "test", -> @arg "test", "test"
expect.equal doc.tree[1].type, "function"
expect.equal doc.tree[1].data.name, "test"
expect.equal doc.tree[1].data.arguments.n, 1
expect.equal doc.tree[1].data.arguments[1].name, "test"
expect.equal doc.tree[1].data.arguments[1].type.type, "test"
it "function returns", ->
doc = leadoc.doc => @function "test", -> @returns "test"
expect.equal doc.tree[1].type, "function"
expect.equal doc.tree[1].data.name, "test"
expect.equal doc.tree[1].data.arguments.n, 0
expect.equal doc.tree[1].data.returns.type.type, "test"
describe "class", ->
it "class", ->
doc = leadoc.doc => @class "test"
expect.equal doc.tree[1].type, "class"
expect.equal doc.tree[1].data.name, "test"
describe "variable", ->
it "variable", ->
doc = leadoc.doc => @variable "test"
expect.equal doc.tree[1].type, "variable"
expect.equal doc.tree[1].data.name, "test"
it "variable with type", ->
doc = leadoc.doc => @variable "test", -> @type "test"
expect.equal doc.tree[1].type, "variable"
expect.equal doc.tree[1].data.name, "test"
expect.equal doc.tree[1].data.type.type.type, "test"
describe "flag", ->
it "global flag", ->
doc = leadoc.doc => @flag "test"
expect.equal doc.flags, {test: true}
it "namespace flag", ->
doc = leadoc.doc => @namespace "test", -> @flag "test"
expect.equal doc.tree[1].flags, {test: true}
it "global description fail", ->
expect.fail (-> leadoc.doc => @description "test"), "description must be a descenedet of something"
describe "fmt", ->
it "reference", ->
reference = leadoc.fmt.reference "test1", "test2"
expect.equal reference.kind, "reference"
expect.equal reference.ref, "test1"
expect.equal reference.inner, {"test2"}
for kind in *{"bold", "italic", "code", "info", "warn", "danger"}
it kind, ->
obj = leadoc.fmt[kind] "test1", "test2"
expect.equal obj.kind, kind
expect.equal obj.inner, {"test1", "test2"}
it "link", ->
link = leadoc.fmt.link "https://example.com", "test"
expect.equal link.kind, "link"
expect.equal link.href, "https://example.com"
expect.equal link.inner, {"test"}
it "link with no inner", ->
link = leadoc.fmt.link "https://example.com"
expect.equal link.kind, "link"
expect.equal link.href, "https://example.com"
expect.equal link.inner, "https://example.com"
it "type", ->
type = leadoc.fmt.type "test"
expect.equal type.kind, "type"
expect.equal type.type, {type: "test", array: false, optional: false}
it "header", ->
header = leadoc.fmt.header 2, "test"
expect.equal header.kind, "header"
expect.equal header.level, 2
expect.equal header.inner, {"test"}
it "codeblock", ->
codeblock = leadoc.fmt.codeblock "lua", "test"
expect.equal codeblock.kind, "codeblock"
expect.equal codeblock.language, "lua"
expect.equal codeblock.inner, {"test"}
describe "type parser", ->
it "simple", ->
type = leadoc.parse_type "test"
expect.equal type, {type: "test", array: false, optional: false}
it "optional", ->
type = leadoc.parse_type "?test"
expect.equal type, {type: "test", array: false, optional: true}
type = leadoc.parse_type "test?"
expect.equal type, {type: "test?", array: false, optional: false}
it "optional with spaces", ->
type = leadoc.parse_type " ? test "
expect.equal type, {type: "test", array: false, optional: true}
it "array of one type", ->
type = leadoc.parse_type "[]test"
expect.equal type, {type: "test", array: true, optional: false}
it "one type array and optional", ->
type = leadoc.parse_type "?[]test"
expect.equal type, {type: "test", array: true, optional: true}
type = leadoc.parse_type "[]?test"
expect.equal type, {type: "?test", array: true, optional: false}
it "array with multiple types", ->
type = leadoc.parse_type "[test1, test2]"
expect.equal type, {type: {{type: "test1", array: false, optional: false}, {type: "test2", array: false, optional: false}}, array: false, optional: false}
it "array with multiple advanced types", ->
type = leadoc.parse_type "[[]test1, ?test2]"
expect.equal type, {type: {{type: "test1", array: true, optional: false}, {type: "test2", array: false, optional: true}}, array: false, optional: false}
it "array of array with multiple types", ->
type = leadoc.parse_type "[][test1, test2]"
expect.equal type, {type: {{type: "test1", array: false, optional: false}, {type: "test2", array: false, optional: false}}, array: true, optional: false}
describe "validate", ->
it "validate", ->
doc = leadoc.doc =>
@namespace "test", ->
@function "testf_1"
@function "testf_2"
@function "testf_3"
@variable "testv_1"
@variable "testv_2", ->
@type "string"
@variable "testv_3", ->
@type "number"
@variable "testv_4"
@function "not_checked", ->
@flag "dont_validate"
@namespace "test2", ->
@flag "dont_validate"
@function "not_checked"
local result
env = {
index#: _G
test: {
testf_1: ->
testf_2: "test"
-- test_f_3
testv_1: true
testv_2: "test"
testv_3: "test"
-- testv_4
undocumented: true
}
print: => result = @
}
env._G = env
code = validate_lua doc.tree
local code_fn
if setfenv -- Lua 5.1
code_fn = loadstring code
setfenv code_fn, env
else -- Lua 5.2+
code_fn = load code, "<sandboxed validate.lua>", "bt", env
code_fn!
output = [x for x in result\gmatch "[^\n]+"]
expected = {
"TYPE MISMATCH: test.testf_2 (expected: function, got: string)"
"TYPE MISMATCH: test.testf_3 (expected: function, got: nil)"
"TYPE MISMATCH: test.testv_3 (expected: number, got: string)"
"UNDOCUMENTED: test.undocumented (type: boolean)"
}
expect.equal expected, output
describe "json", ->
describe "stringify", ->
it "json.null", ->
encoded = json.stringify test: json.null
expect.equal encoded, '{"test":null}'
it "fast arrays", ->
array = {n: 0}
fast_push array, "test 1"
fast_push array, "test 2"
fast_push array, "test 3"
encoded = json.stringify array
expect.equal encoded, '["test 1", "test 2", "test 3"]'
it "empty array", ->
encoded = json.stringify n: 0
expect.equal encoded, "[]"
encoded = json.stringify {}
expect.equal encoded, "{}"
it "mixed array and object", ->
object = {
"test 1"
"test 2"
"test 3"
n: 3
test: "test"
}
encoded = json.stringify object
-- order can be arbitrary
expected = {k, true for k in *{
'"1":"test 1"'
'"2":"test 2"'
'"3":"test 3"'
'"n":3'
'"test":"test"'
}}
for pair in (encoded\sub 2, -2)\gmatch "[^,]+"
pair = pair\sub 1 + #pair\match "%s*" -- remove whitespace
expect.truthy expected[pair]
expected[pair] = nil
expect.equal expected, {}
describe "fast arrays", ->
it "push", ->
array = {n: 0}
fast_push array, "test 1"
fast_push array, "test 2"
fast_push array, "test 3"
expect.equal array, {"test 1", "test 2", "test 3", n: 3}
it "peek", ->
array = {n: 0}
expect.falsy fast_peek array
fast_push array, "test 1"
expect.equal "test 1", fast_peek array
fast_push array, "test 2"
expect.equal "test 2", fast_peek array
fast_push array, "test 3"
expect.equal "test 3", fast_peek array
expect.equal array, {"test 1", "test 2", "test 3", n: 3}
it "pop", ->
array = {n: 0}
fast_push array, "test 1"
expect.equal array, {"test 1", n: 1}
expect.equal "test 1", fast_pop array
expect.equal array, {n: 0}
fast_push array, "test 1"
fast_push array, "test 2"
fast_push array, "test 3"
expect.equal "test 3", fast_pop array
expect.equal array, {"test 1", "test 2", n: 2}
expect.equal "test 2", fast_pop array
it "del", ->
array = {n: 0}
fast_push array, "test 1"
expect.equal array, {"test 1", n: 1}
expect.falsy fast_del array
expect.equal array, {n: 0}
fast_push array, "test 1"
fast_push array, "test 2"
fast_push array, "test 3"
expect.falsy fast_del array
expect.equal array, {"test 1", "test 2", n: 2}
it "iter", ->
array = {"test 1", "test 2", "test 3", n: 3}
i = 1
for x in fast_iter array
expect.equal array[i], x
i += 1
expect.equal i, 4
it "iter with limit", ->
array = {"test 1", "test 2", "test 3", n: 2}
i = 1
for x in fast_iter array
expect.equal array[i], x
i += 1
expect.equal i, 3
it "extend", ->
array = {"test 1", "test 2", "test 3", n: 3}
array2 = {"test 4", "test 5", "test 6", n: 3}
fast_extend array, array2
expect.equal array, {"test 1", "test 2", "test 3", "test 4", "test 5", "test 6", n: 6}
describe "find", ->
describe "flag", ->
it "any value", ->
local test, test2
doc = leadoc.doc =>
@namespace "test", ->
test = @function "test", ->
@flag "test"
test2 = @function "test2", ->
@flag "test2"
results = find.find_flag doc, "test"
expect.equal results, {test.id, n: 1}
results = find.find_flag doc, "test2"
expect.equal results, {test2.id, n: 1}
it "specific value", ->
local test, test2
doc = leadoc.doc =>
@namespace "test", ->
test = @function "test", ->
@flag "test", "veryspecific"
test2 = @function "test2", ->
@flag "test"
results = find.find_flag doc, "test", "veryspecific"
expect.equal results, {test.id, n: 1}
results = find.find_flag doc, "test", true
expect.equal results, {test2.id, n: 1}
results = find.find_flag doc, "test"
expect.equal results, {test.id, test2.id, n: 2}
describe "converter", ->
it "snippets", ->
doc = leadoc.doc =>
@namespace "test", ->
@function "test", ->
@arg "test", "string"
@arg "test2", "[]number"
@arg "test3", "[number, number]"
@returns "string"
@variable "test2", ->
@type "string"
@namespace "test2", ->
@flag "dont_include_in_snippet"
@function "not_included"
@class "testclass", ->
@function "test"
result = snippets.generate_snippet doc, snippets.configs.lua
expect.equal result, {
testtest: {
prefix: "test.test(test: string, test2: []number, test3: [number, number]): string"
body: "test.test(test, test2, test3)"
description: ""
scope: "lua"
}
testtest2: {
prefix: "test.test2: string"
body: "test.test2"
description: ""
scope: "lua"
},
testclass: {
prefix: "testclass()"
body: "testclass()"
description: ""
scope: "lua"
},
testclasstest: {
prefix: "testclass.test()"
body: "testclass.test()"
description: ""
scope: "lua"
}
}
it "typescript definition", ->
doc = leadoc.doc =>
@flag "globals"
@namespace "test", ->
@function "test", ->
@arg "test", "string"
@arg "test2", "[]number"
@arg "test3", "[number, number]"
@arg "test4", "?integer"
@returns "string"
@variable "test2", ->
@type "string"
@function "test3"
@namespace "test2", ->
@flag "dont_include_in_definition"
@function "not_included"
result = tsd.generate_typescript doc
lines = {
"export {}"
"type integer = number;"
"type float = number;"
"declare global {"
"\tnamespace test {"
"\t\tfunction test(test: string, test2: Array<number>, test3: [number, number], test4: integer | undefined): string;"
"\t\tlet test2: string;"
"\t\tfunction test3(): void;"
"\t}"
"}"
}
i = 1
for line in result\gmatch "[^\n]+"
expect.equal line, lines[i]
i += 1
report!
exit!