-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestingunit.lua
More file actions
executable file
·624 lines (575 loc) · 22.5 KB
/
testingunit.lua
File metadata and controls
executable file
·624 lines (575 loc) · 22.5 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
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
#!/usr/bin/env lua
TestingUnit = setmetatable({},
{
__call = function(self, ...)
local test_table = {
--[[this table value should be considered immutable as it is used
for table discovery]]
is_testing_unit = true,
fixtures = {},
_assertion_failure = function(self, ...)
error("override me", 4)
end,
assert_raises = function(self, func, args)
--[[ given a callable ``func`` and table of arguments ``args``,
assert that ``func(unpack(args))`` throws an error.
]]
local args = args or {}
local success, result = pcall(func, unpack(args))
--we don't want success as this is assert_raises()
if success then
self:_assertion_failure{
err_str=result,
func=func,
args=args,
traceback=debug.traceback(2),
debug_info=debug.getinfo(2)
}
end
end,
assert_equal = function(self, a, b)
if a ~= b then
self:_assertion_failure{
traceback=debug.traceback(2),
err_str=string.format("assert_equal failed: %s ~= %s",
a , b
),
debug_info=debug.getinfo(2),
args={a, b}
}
end
end,
assert_almost_equal = function(self, a, b, epsilon)
local epsilon = epsilon or 0.000001
if (math.abs(a) - math.abs(b)) > epsilon then
self:_assertion_failure{
traceback=debug.traceback(2),
err_str=string.format(
"assert_almost equal failed: %s ~= %s +/-%s",
a , b, epsilon
),
debug_info=debug.getinfo(2),
args={a, b},
}
end
end,
assert_truthy = function(self, x)
if not x then
self:_assertion_failure{
traceback=debug.traceback(2),
err_str=string.format(
"assert_truthy failed: '%s' is not truthy",
x
),
debug_info=debug.getinfo(2),
args={x}
}
end
end,
assert_true = function(self, x)
if x ~= true then
self:_assertion_failure{
traceback=debug.traceback(2),
err_str=string.format(
"assert_true failed:'%s' ~= true",
x
),
debug_info=debug.getinfo(2),
args={x}
}
end
end,
assert_false = function(self, x)
if x ~= false then
self:_assertion_failure{
err_str=string.format(
"assert_false failed: '%s' ~= false",
x
),
debug_info=debug.getinfo(2),
args={x}
}
end
end,
assert_match = function(self, s, exp)
if not string.match(s, exp) then
self:_assertion_failure{
err_str=string.format(
"'%s' does not match '%s'",
s, exp
),
debug_info=debug.getinfo(2),
args={s, exp}
}
end
end,
assert_in = function(self, haystack, needle)
if type(haystack) ~= 'string' and type(haystack) ~= 'table' then
self:_assertion_failure{
err_str=string.format(
"type('%s') is not a container type",
haystack
),
debug_info=debug.getinfo(2),
args={needle=needle, haystack=haystack}
}
return
end
if type(haystack) == 'string' then
if type(needle) ~= 'string' then
self:_assertion_failure{
err_str=string.format(
"'%s' cannot be a substring of '%s'",
tostring(needle), haystack
),
debug_info=debug.getinfo(2),
args={needle=needle, haystack=haystack}
}
return
end
local start, finish = string.find(haystack, needle)
if start == nil then
self:_assertion_failure{
err_str=string.format(
"'%s' not found in '%s'",
needle, haystack
),
debug_info=debug.getinfo(2),
args={needle=needle, haystack=haystack}
}
return
end
end
if type(haystack) == 'table' then
local in_table = false
for k, v in pairs(haystack) do
if v == needle then
in_table = true
end
end
if not in_table then
self:_assertion_failure{
traceback=debug.traceback(2),
err_str=string.format(
"'%s' not found in '%s'",
needle, haystack
),
debug_info=debug.getinfo(2),
args={needle=needle, haystack=haystack}
}
end
end
end,
assert_nil = function(self, x)
if x ~= nil then
self:_assertion_failure{
traceback=debug.traceback(2),
err_str=string.format("'%s' not nil", x),
debug_info=debug.getinfo(2),
args={needle=needle, haystack=haystack}
}
end
end,
assert_calls = function(self, caller, callee, args)
if(type(caller)~='function') or (type(callee)~='function') then
error(
string.format(
[[callable arguments required for assert_calls().
'Got %s, %s, %s]],
caller, callee, args
),
2
)
end
local was_called = false
local function trace_hook()
if debug.getinfo(2, "f").func == callee then
was_called = true
end
end
local function getname(func)
--From PiL 'the debug library
local n = debug.getinfo(func)
if n.what == "C" then
return n.name
end
local lc = string.format("[%s]:%d",
n.short_src,
n.linedefined
)
if n.what ~= "main" and n.namewhat ~= "" then
return string.format("%s, (%s)", lc, n.name)
else
return lc
end
end
debug.sethook(trace_hook, 'c')
--[[pcall is required here because any
errors prevent reporting / cancelling the debug hook]]
pcall(caller,args)
debug.sethook()
if not was_called then
self:_assertion_failure{
traceback=debug.traceback(2),
err_str=string.format(
"assert_calls failure:'%s' not called by '%s' with args(%s)'",
getname(callee),
getname(caller),
table.concat(type(args) == 'table' and args or {args}, ', ')
),
debug_info=debug.getinfo(2),
args={caller=caller, callee=callee, args=args}
}
end
end
}
--inherit called values before return.
if ... ~= nil then
for k, v in pairs(...) do
test_table[k] = v
end
end
return test_table
end,
__tostring = function(self)
local tests = {}
for k, v in pairs(self) do
if type(v) == 'function' and string.match(k, '^test.+') then
tests[#tests + 1] = k .. '()'
end
end
return string.format("TestingUnit{%s}", table.concat(tests, ',\n'))
end
})
function find_test_files(dirs, depth, pattern)
--[[discover all lua test scripts in the given directories and put their
filenames in the returned array.
This currently uses the posix `find` command, so we expect it on $PATH.
Sorry Win32... except I'm not.
]]
local tests = {}
local function enumerate_files(dirs, depth, pattern)
-- generator function to enumerate all files within a given directory
local dirs = dirs or {'.'}
local depth = tonumber(depth) or 2
local pattern = pattern or nil
if pattern then
pattern = string.format('-iname %q', pattern)
else
pattern = ""
end
local function scandir(directory)
local t = {}
--This quoting is not security safe, but should prevent accidents
for filename in io.popen(
string.format("find %s -maxdepth %s %s -type f",
string.format("%q", directory),
depth,
pattern
)
):lines() do
t[#t + 1] = filename
end
return t
end
local all_files = {}
for _, dir in pairs(dirs) do
for _, v in pairs(scandir(dir)) do
all_files[#all_files + 1] = v
end
end
local index = 0
local function file_iter()
index = index + 1
return all_files[index]
end
return file_iter
end
local function basename(path)
local index = string.find(path:reverse(), '/', 1, true)
if not index then return path end
return string.sub(path, #path-index+2)
end
for file in enumerate_files(dirs, depth, pattern) do
if string.match(basename(file), '^test.*.lua$') then
tests[#tests + 1] = file
end
end
return tests
end
function runtests(all_test_files, show_tracebacks, silence_output)
--[[
Given a table containing paths of testing scripts, load each one in turn
and execute any TestingUnits instantiated into the global table by the script.
The global namespace is reset between each file load, so there are no
side-effects from other test files, it is not reset per suite in the case that
multiple test suites exist in a single file.
This shouldn't present a problem in most cases.
]]
local old_globals = {} --we store a clean copy of _G to restore between tests.
local n_tests_run = 0
local n_tests_failed = 0
local n_tests_passed = 0
local n_tests_expected_failed = 0
local n_test_errors = 0
local start_time = 0
local stop_time = 0
local failures = {}
local errors = {}
local expected_failures = {}
local function report(...)
if not(silence_output) then
print(...)
end
end
local function save_globals()
--store the contents of the global table locally
for k, v in pairs(_G) do
old_globals[k] = v
end
end
local function reset_globals()
--set the global table back to the saved upvalue
local pairs = pairs
for k, v in pairs(_G) do
if not old_globals[k] then
_G[k] = nil
end
end
end
local function load_files_iter(files_list)
--[[
Given a list of files, load the test into the global environment and
return all discovered test tables.
warns on failure to load, but skips to the next test.
]]
local index = 0
local file_iter = ipairs(files_list)
local function iter()
local _, file = file_iter(files_list, index)
index = index + 1
if file == nil then return nil end
local func, err = loadfile(file)
report(string.format("loading '%s'...", file))
if func then
if not pcall(func) then
report(string.format("ERROR:failed to exec '%s'", file))
errors[#errors] = {file=file}
return {}
else
local all_test_units = {}
--[[
Iterate the global registry for tables with
key ``is_testing_unit == true ``.
Add that table to the list of unit test tables.
]]
for k, v in pairs(_G) do
if type(v) == 'table' then
if v.is_testing_unit == true then
all_test_units[#all_test_units +1] = v
end
end
end
return all_test_units
end
else
report(string.format("ERROR:failed to load '%s':\n\t%s", file, err))
n_test_errors = n_test_errors + 1
return {}
end
end
return iter
end
function get_fixtures(t, member_name)
if not t.fixtures[member_name] then return {{}} end
return t.fixtures[member_name]
end
local function run_test_unit(t)
--[[ Search each identified table ``t`` for member functions
named ``test*`` and execute each one within pcall, identifying and
counting failures, expected failures, and execution errors.
]]
local is_expected_failure
local last_assertion
--[[We inject the following function into the test table so we can
reference ``last_assertion`` as a nonlocal variable, and the test
table can call self:_assertion_failure()
]]
local function _assertion_failure(self, t)
last_assertion = t
end
t._assertion_failure = _assertion_failure
--iterate all callables and iterate/call with any supplied fixtures
for k, v in pairs(t) do
if type(v) == 'function' or (type(v) == 'table'
and getmetatable(v) ~= nil
and type(getmetatable(v)['__call']) == 'function') then
local func_name, callable = k, v
if string.match(string.lower(func_name), '^test') then
for _, fixture in ipairs(get_fixtures(t, func_name)) do
--[[
Expected failures are indicated by naming convention.
rather than decorators or similar conventions used in
other langauges. The following member functions will be
treated as expected failures:
test_myfunction_expected_fail'
'TestExpectedFailure'
'test_myfunctionExpected_failREALLYUGLY'
Basically any sane name beginning with 'test' having the
substrings, 'expected' and 'fail' following.
See pattern below for details.
]]
is_expected_failure = false
last_assertion = nil
if string.match(
string.lower(func_name),
'^test[%a%d_]*expected[%a%d_]*fail'
) then
--[[The expected failure handling is a hack relying on
the nonlocal variable ``last_assertion`` as
semi-global state,but it significantly
simplifies the assert_* functions
and that's a good thing.
]]
is_expected_failure = true
end
--execute the test with data
if t.setup then t:setup(callable, fixture) end
local success, ret
if #fixture > 0 then
success, ret = pcall(callable, t, unpack(fixture))
else
success, ret = pcall(callable, t)
end
n_tests_run = n_tests_run + 1
if not success then
errors[#errors +1] = {
name=func_name,
err_str=ret,
args=fixture
}
end
if t.teardown then t:teardown(callable, fixture) end
if is_expected_failure then
if not last_assertion then
n_tests_failed = n_tests_failed + 1
failures[#failures +1 ] = {
err_str=string.format(
"%s: did not fail as expected",
func_name
),
debug_info=debug.getinfo(callable),
args=fixture,
func_name=func_name
}
else
n_tests_expected_failed = n_tests_expected_failed + 1
end
else
if last_assertion then
last_assertion.func_name = func_name
n_tests_failed = n_tests_failed + 1
failures[#failures +1 ] = last_assertion
else
n_tests_passed = n_tests_passed +1
end
end
end
end
end
end
end
--actually run all the tests
save_globals()
start_time = os.time()
for t in load_files_iter(all_test_files) do
if t then
for _, v in ipairs(t) do
run_test_unit(v)
end
end
reset_globals()
end
stop_time = os.time()
local function print_results()
local function getname(func)
--From PiL 'the debug library
local n = debug.getinfo(func)
if n.what == "C" then
return n.name
end
local lc = string.format("[%s]:%d", n.short_src, n.linedefined)
if n.what ~= "main" and n.namewhat ~= "" then
return string.format("%s, (%s)", lc, n.name)
else
return lc
end
end
local function format_args(args)
local t = {}
if args == nil then return '' end
print("args:",args)
for k, v in pairs(args)do print(k, v) end
for _, arg in ipairs(args) do t[#t + 1] = tostring(arg) end
return string.format('(%s)', table.concat(t, ', '))
end
for _, f in ipairs(failures) do
report(string.rep("=", 70))
report(
string.format("FAILURE:%s%s\n%s",
f.func_name,
format_args(f.args),
getname(f.debug_info.func))
)
report(string.rep("-", 70))
report(f.err_str)
if show_tracebacks then
report(string.format("Traceback:\n%s", f.traceback))
end
report()
end
for _, e in ipairs(errors) do
n_test_errors = n_test_errors + 1
report(string.rep("=", 70))
report("ERROR:execution failure:%s%s\n%s")
for k, v in pairs(e) do
report(k, v)
end
report(string.rep("-", 70))
end
end
print_results()
report(
string.format([[Ran %s tests in %s seconds.
%s failures, %s expected failures, %s errors, %s passed]],
n_tests_run,
os.difftime(stop_time, start_time),
n_tests_failed,
n_tests_expected_failed,
n_test_errors,
n_tests_passed
)
)
return n_tests_failed + n_test_errors
end
if arg then
script_name = arg[0]:match("[.%w]*$")
end
if script_name == 'testingunit' or script_name == 'testingunit.lua'then
--[[
`arg` is set by the lua repl: 'lua_setglobal(L, "arg");', not by the Lua VM,
so this will work in an embedded system that doesn't set `arg`. Fragile much?
]]
local test_dirs = #arg > 0 and {} or {'.', 'tests'}
local depth = 1
for _, v in ipairs(arg) do
if v:match('^--depth=[%d]$') ~= nil then
depth = tonumber(v:match('%d'))
else
test_dirs[#test_dirs + 1] = v
end
end
return os.exit(runtests(find_test_files(test_dirs, depth, "*.lua")))
else
return TestingUnit
end