-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtest.lua
230 lines (215 loc) · 5.9 KB
/
test.lua
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
-- run tests
package.path = "./?.lua;" .. package.path
local djot = require("djot")
local testcases = {
"attributes.test",
"blockquote.test",
"code_blocks.test",
"definition_lists.test",
"symbol.test",
"emphasis.test",
"escapes.test",
"fenced_divs.test",
"filters.test",
"footnotes.test",
"headings.test",
"insert_delete_mark.test",
"links_and_images.test",
"lists.test",
"math.test",
"para.test",
"raw.test",
"regression.test",
"smart.test",
"spans.test",
"sourcepos.test",
"super_subscript.test",
"tables.test",
"task_lists.test",
"thematic_breaks.test",
"verbatim.test"
}
local opts = {}
local i=1
while i <= #arg do
local thisarg = arg[i]
if string.find(thisarg, "^%-") then
if thisarg == "-v" then
opts.verbose = true
elseif thisarg == "-p" then
opts.pattern = true
elseif thisarg == "--accept" then
opts.accept = true
end
elseif opts.pattern == true then
opts.pattern = thisarg
end
i = i + 1
end
local Tests = {}
function Tests:new()
local contents = {
passed = 0,
failed = 0,
errors = 0,
accept = opts.accept,
verbose = opts.verbose
}
setmetatable(contents, Tests)
Tests.__index = Tests
return contents
end
function Tests:do_test(test)
if self.verbose then
io.write(string.format("Testing %s at linen %d\n", test.file, test.linenum))
end
local sourcepos = false
if test.options:match("p") then
sourcepos = true
end
local actual = ""
if test.options:match("m") then
actual = actual .. djot.parse_and_render_events(test.input)
else
local doc = djot.parse(test.input, sourcepos)
for _,filt in ipairs(test.filters) do
local f, err = djot.filter.load_filter(filt)
if not f then
error(err)
end
djot.filter.apply_filter(doc, f)
end
if test.options:match("a") then
actual = actual .. djot.render_ast_pretty(doc)
else -- match 'h' or empty
actual = actual .. djot.render_html(doc)
end
end
if self.accept then
test.output = actual
end
if actual == test.output then
self.passed = self.passed + 1
return true
else
io.write(string.format("FAILED at %s line %d\n", test.file, test.linenum))
io.write(string.format("--- INPUT -------------------------------------\n%s--- EXPECTED ----------------------------------\n%s--- GOT ---------------------------------------\n%s-----------------------------------------------\n\n", test.input, test.output, actual))
self.failed = self.failed + 1
return false
end
end
local function read_tests(file)
local f = io.open("test/" .. file,"r")
assert(f ~= nil, "File " .. file .. " cannot be read")
local line
local linenum = 0
return function()
while true do
local inp = ""
local out = ""
line = f:read()
local pretext = {}
linenum = linenum + 1
while line and not line:match("^```") do
pretext[#pretext + 1] = line
line = f:read()
linenum = linenum + 1
end
local testlinenum = linenum
if not line then
break
end
local ticks, options = line:match("^(`+)%s*(.*)")
-- parse input
line = f:read()
linenum = linenum + 1
while not line:match("^[%.%!]$") do
inp = inp .. line .. "\n"
line = f:read()
linenum = linenum + 1
end
local filters = {}
while line == "!" do -- parse filter
line = f:read()
linenum = linenum + 1
local filt = ""
while not line:match("^[%.%!]$") do
filt = filt .. line .. "\n"
line = f:read()
linenum = linenum + 1
end
table.insert(filters, filt)
end
-- parse output
line = f:read()
linenum = linenum + 1
while not line:match("^" .. ticks) do
out = out .. line .. "\n"
line = f:read()
linenum = linenum + 1
end
return { file = file,
linenum = testlinenum,
pretext = table.concat(pretext, "\n"),
options = options,
filters = filters,
input = inp,
output = out }
end
end
end
function Tests:do_tests(file)
local tests = {}
for test in read_tests(file) do
tests[#tests + 1] = test
local ok, err = pcall(function()
self:do_test(test)
end)
if not ok then
io.stderr:write(string.format("Error running test %s line %d:\n%s\n",
test.file, test.linenum, err))
self.errors = self.errors + 1
end
end
if self.accept then -- rewrite file
local fh = io.open("test/" .. file, "w")
for idx,test in ipairs(tests) do
local numticks = 3
string.gsub(test.input .. test.output, "(````*)",
function(x)
if #x >= numticks then
numticks = #x + 1
end
end)
local ticks = string.rep("`", numticks)
local pretext = test.pretext
if #pretext > 0 or idx > 1 then
pretext = pretext .. "\n"
end
fh:write(string.format("%s%s%s\n%s",
pretext,
ticks,
(test.options == "" and "") or " " .. test.options,
test.input))
for _,f in ipairs(test.filters) do
fh:write(string.format("!\n%s", f))
end
fh:write(string.format(".\n%s%s\n", test.output, ticks))
end
fh:close()
end
end
local tests = Tests:new()
local starttime = os.clock()
for _,case in ipairs(testcases) do
if not opts.pattern or string.find(case, opts.pattern) then
tests:do_tests(case)
end
end
local endtime = os.clock()
io.write(string.format("%d tests completed in %0.3f s\n",
tests.passed + tests.failed + tests.errors, endtime - starttime))
io.write(string.format("PASSED: %4d\n", tests.passed))
io.write(string.format("FAILED: %4d\n", tests.failed))
io.write(string.format("ERRORS: %4d\n", tests.errors))
os.exit(tests.failed + tests.errors)