-
Notifications
You must be signed in to change notification settings - Fork 2
/
lester.lua
599 lines (540 loc) · 19.2 KB
/
lester.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
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
--[[
Minimal test framework for Lua.
lester - v0.1.5 - 18/Oct/2023
Eduardo Bart - [email protected]
https://github.com/edubart/lester
Minimal Lua test framework.
See end of file for LICENSE.
]]
--[[--
Lester is a minimal unit testing framework for Lua with a focus on being simple to use.
## Features
* Minimal, just one file.
* Self contained, no external dependencies.
* Simple and hackable when needed.
* Use `describe` and `it` blocks to describe tests.
* Supports `before` and `after` handlers.
* Colored output.
* Configurable via the script or with environment variables.
* Quiet mode, to use in live development.
* Optionally filter tests by name.
* Show traceback on errors.
* Show time to complete tests.
* Works with Lua 5.1+.
* Efficient.
## Usage
Copy `lester.lua` file to a project and require it,
which returns a table that includes all of the functionality:
```lua
local lester = require 'lester'
local describe, it, expect = lester.describe, lester.it, lester.expect
-- Customize lester configuration.
lester.show_traceback = false
-- Parse arguments from command line.
lester.parse_args()
describe('my project', function()
lester.before(function()
-- This function is run before every test.
end)
describe('module1', function() -- Describe blocks can be nested.
it('feature1', function()
expect.equal('something', 'something') -- Pass.
end)
it('feature2', function()
expect.truthy(false) -- Fail.
end)
local feature3_test_enabled = false
it('feature3', function() -- This test will be skipped.
expect.truthy(false) -- Fail.
end, feature3_test_enabled)
end)
end)
lester.report() -- Print overall statistic of the tests run.
lester.exit() -- Exit with success if all tests passed.
```
## Customizing output with environment variables
To customize the output of lester externally,
you can set the following environment variables before running a test suite:
* `LESTER_QUIET="true"`, omit print of passed tests.
* `LESTER_COLOR="false"`, disable colored output.
* `LESTER_SHOW_TRACEBACK="false"`, disable traceback on test failures.
* `LESTER_SHOW_ERROR="false"`, omit print of error description of failed tests.
* `LESTER_STOP_ON_FAIL="true"`, stop on first test failure.
* `LESTER_UTF8TERM="false"`, disable printing of UTF-8 characters.
* `LESTER_FILTER="some text"`, filter the tests that should be run.
Note that these configurations can be changed via script too, check the documentation.
## Customizing output with command line arguments
You can also customize output using command line arguments
if `lester.parse_args()` is called at startup.
The following command line arguments are available:
* `--quiet`, omit print of passed tests.
* `--no-quiet`, show print of passed tests.
* `--no-color`, disable colored output.
* `--no-show-traceback`, disable traceback on test failures.
* `--no-show-error`, omit print of error description of failed tests.
* `--stop-on-fail`, stop on first test failure.
* `--no-utf8term`, disable printing of UTF-8 characters.
* `--filter="some text"`, filter the tests that should be run.
]]
-- Returns whether the terminal supports UTF-8 characters.
local function is_utf8term()
local lang = os.getenv('LANG')
return (lang and lang:lower():match('utf%-?8$')) and true or false
end
-- Returns whether a system environment variable is "true".
local function getboolenv(varname, default)
local val = os.getenv(varname)
if val == 'true' then
return true
elseif val == 'false' then
return false
end
return default
end
-- The lester module.
local lester = {
--- Whether lines of passed tests should not be printed. False by default.
quiet = getboolenv('LESTER_QUIET', false),
--- Whether the output should be colorized. True by default.
color = getboolenv('LESTER_COLOR', true),
--- Whether a traceback must be shown on test failures. True by default.
show_traceback = getboolenv('LESTER_SHOW_TRACEBACK', true),
--- Whether the error description of a test failure should be shown. True by default.
show_error = getboolenv('LESTER_SHOW_ERROR', true),
--- Whether test suite should exit on first test failure. False by default.
stop_on_fail = getboolenv('LESTER_STOP_ON_FAIL', false),
--- Whether we can print UTF-8 characters to the terminal. True by default when supported.
utf8term = getboolenv('LESTER_UTF8TERM', is_utf8term()),
--- A string with a lua pattern to filter tests. Nil by default.
filter = os.getenv('LESTER_FILTER') or '',
--- Function to retrieve time in seconds with milliseconds precision, `os.clock` by default.
seconds = os.clock,
}
-- Variables used internally for the lester state.
local lester_start = nil
local last_succeeded = false
local level = 0
local successes = 0
local total_successes = 0
local failures = 0
local total_failures = 0
local skipped = 0
local total_skipped = 0
local start = 0
local befores = {}
local afters = {}
local names = {}
-- Color codes.
local color_codes = {
reset = string.char(27) .. '[0m',
bright = string.char(27) .. '[1m',
red = string.char(27) .. '[31m',
green = string.char(27) .. '[32m',
yellow = string.char(27) .. '[33m',
blue = string.char(27) .. '[34m',
magenta = string.char(27) .. '[35m',
}
local quiet_o_char = string.char(226, 151, 143)
-- Colors table, returning proper color code if color mode is enabled.
local colors = setmetatable({}, { __index = function(_, key)
return lester.color and color_codes[key] or ''
end})
--- Table of terminal colors codes, can be customized.
lester.colors = colors
-- Parse command line arguments from `arg` table.
-- It `arg` is nil then the global `arg` is used.
function lester.parse_args(arg)
for _,opt in ipairs(arg or _G.arg) do
local name, value
if opt:find('^%-%-filter') then
name = 'filter'
value = opt:match('^%-%-filter%=(.*)$')
elseif opt:find('^%-%-no%-[a-z0-9-]+$') then
name = opt:match('^%-%-no%-([a-z0-9-]+)$'):gsub('-','_')
value = false
elseif opt:find('^%-%-[a-z0-9-]+$') then
name = opt:match('^%-%-([a-z0-9-]+)$'):gsub('-','_')
value = true
end
if value ~= nil and lester[name] ~= nil and (type(lester[name]) == 'boolean' or type(lester[name]) == 'string') then
lester[name] = value
end
end
end
--- Describe a block of tests, which consists in a set of tests.
-- Describes can be nested.
-- @param name A string used to describe the block.
-- @param func A function containing all the tests or other describes.
function lester.describe(name, func)
if level == 0 then -- Get start time for top level describe blocks.
failures = 0
successes = 0
skipped = 0
start = lester.seconds()
if not lester_start then
lester_start = start
end
end
-- Setup describe block variables.
level = level + 1
names[level] = name
-- Run the describe block.
func()
-- Cleanup describe block.
afters[level] = nil
befores[level] = nil
names[level] = nil
level = level - 1
-- Pretty print statistics for top level describe block.
if level == 0 and not lester.quiet and (successes > 0 or failures > 0) then
local io_write = io.write
local colors_reset, colors_green = colors.reset, colors.green
io_write(failures == 0 and colors_green or colors.red, '[====] ',
colors.magenta, name, colors_reset, ' | ',
colors_green, successes, colors_reset, ' successes / ')
if skipped > 0 then
io_write(colors.yellow, skipped, colors_reset, ' skipped / ')
end
if failures > 0 then
io_write(colors.red, failures, colors_reset, ' failures / ')
end
io_write(colors.bright, string.format('%.6f', lester.seconds() - start), colors_reset, ' seconds\n')
end
end
-- Error handler used to get traceback for errors.
local function xpcall_error_handler(err)
return debug.traceback(tostring(err), 2)
end
-- Pretty print the line on the test file where an error happened.
local function show_error_line(err)
local info = debug.getinfo(3)
local io_write = io.write
local colors_reset = colors.reset
local short_src, currentline = info.short_src, info.currentline
io_write(' (', colors.blue, short_src, colors_reset,
':', colors.bright, currentline, colors_reset)
if err and lester.show_traceback then
local fnsrc = short_src..':'..currentline
for cap1, cap2 in err:gmatch('\t[^\n:]+:(%d+): in function <([^>]+)>\n') do
if cap2 == fnsrc then
io_write('/', colors.bright, cap1, colors_reset)
break
end
end
end
io_write(')')
end
-- Pretty print the test name, with breadcrumb for the describe blocks.
local function show_test_name(name)
local io_write = io.write
local colors_reset = colors.reset
for _,descname in ipairs(names) do
io_write(colors.magenta, descname, colors_reset, ' | ')
end
io_write(colors.bright, name, colors_reset)
end
--- Declare a test, which consists of a set of assertions.
-- @param name A name for the test.
-- @param func The function containing all assertions.
-- @param enabled If not nil and equals to false, the test will be skipped and this will be reported.
function lester.it(name, func, enabled)
-- Skip the test silently if it does not match the filter.
if lester.filter then
local fullname = table.concat(names, ' | ')..' | '..name
if not fullname:match(lester.filter) then
return
end
end
local io_write = io.write
local colors_reset = colors.reset
-- Skip the test if it's disabled, while displaying a message
if enabled == false then
if not lester.quiet then
io_write(colors.yellow, '[SKIP] ', colors_reset)
show_test_name(name)
io_write('\n')
else -- Show just a character hinting that the test was skipped.
local o = (lester.utf8term and lester.color) and quiet_o_char or 'o'
io_write(colors.yellow, o, colors_reset)
end
skipped = skipped + 1
total_skipped = total_skipped + 1
return
end
-- Execute before handlers.
for _,levelbefores in pairs(befores) do
for _,beforefn in ipairs(levelbefores) do
beforefn(name)
end
end
-- Run the test, capturing errors if any.
local success, err
if lester.show_traceback then
success, err = xpcall(func, xpcall_error_handler)
else
success, err = pcall(func)
if not success and err then
err = tostring(err)
end
end
-- Count successes and failures.
if success then
successes = successes + 1
total_successes = total_successes + 1
else
failures = failures + 1
total_failures = total_failures + 1
end
-- Print the test run.
if not lester.quiet then -- Show test status and complete test name.
if success then
io_write(colors.green, '[PASS] ', colors_reset)
else
io_write(colors.red, '[FAIL] ', colors_reset)
end
show_test_name(name)
if not success then
show_error_line(err)
end
io_write('\n')
else
if success then -- Show just a character hinting that the test succeeded.
local o = (lester.utf8term and lester.color) and quiet_o_char or 'o'
io_write(colors.green, o, colors_reset)
else -- Show complete test name on failure.
io_write(last_succeeded and '\n' or '',
colors.red, '[FAIL] ', colors_reset)
show_test_name(name)
show_error_line(err)
io_write('\n')
end
end
-- Print error message, colorizing its output if possible.
if err and lester.show_error then
if lester.color then
local errfile, errline, errmsg, rest = err:match('^([^:\n]+):(%d+): ([^\n]+)(.*)')
if errfile and errline and errmsg and rest then
io_write(colors.blue, errfile, colors_reset,
':', colors.bright, errline, colors_reset, ': ')
if errmsg:match('^%w([^:]*)$') then
io_write(colors.red, errmsg, colors_reset)
else
io_write(errmsg)
end
err = rest
end
end
io_write(err, '\n\n')
end
io.flush()
-- Stop on failure.
if not success and lester.stop_on_fail then
if lester.quiet then
io_write('\n')
io.flush()
end
lester.exit()
end
-- Execute after handlers.
for _,levelafters in pairs(afters) do
for _,afterfn in ipairs(levelafters) do
afterfn(name)
end
end
last_succeeded = success
end
--- Set a function that is called before every test inside a describe block.
-- A single string containing the name of the test about to be run will be passed to `func`.
function lester.before(func)
local levelbefores = befores[level]
if not levelbefores then
levelbefores = {}
befores[level] = levelbefores
end
levelbefores[#levelbefores+1] = func
end
--- Set a function that is called after every test inside a describe block.
-- A single string containing the name of the test that was finished will be passed to `func`.
-- The function is executed independently if the test passed or failed.
function lester.after(func)
local levelafters = afters[level]
if not levelafters then
levelafters = {}
afters[level] = levelafters
end
levelafters[#levelafters+1] = func
end
--- Pretty print statistics of all test runs.
-- With total success, total failures and run time in seconds.
function lester.report()
local now = lester.seconds()
local colors_reset = colors.reset
io.write(lester.quiet and '\n' or '',
colors.green, total_successes, colors_reset, ' successes / ',
colors.yellow, total_skipped, colors_reset, ' skipped / ',
colors.red, total_failures, colors_reset, ' failures / ',
colors.bright, string.format('%.6f', now - (lester_start or now)), colors_reset, ' seconds\n')
io.flush()
return total_failures == 0
end
--- Exit the application with success code if all tests passed, or failure code otherwise.
function lester.exit()
-- Collect garbage before exiting to call __gc handlers
collectgarbage()
collectgarbage()
os.exit(total_failures == 0, true)
end
local expect = {}
--- Expect module, containing utility function for doing assertions inside a test.
lester.expect = expect
--- Converts a value to a human-readable string.
-- If the final string not contains only ASCII characters,
-- then it is converted to a Lua hexdecimal string.
function expect.tohumanstring(v)
local s = tostring(v)
if s:find'[^ -~\n\t]' then -- string contains non printable ASCII
return '"'..s:gsub('.', function(c) return string.format('\\x%02X', c:byte()) end)..'"'
end
return s
end
--- Check if a function fails with an error.
-- If `expected` is nil then any error is accepted.
-- If `expected` is a string then we check if the error contains that string.
-- If `expected` is anything else then we check if both are equal.
function expect.fail(func, expected)
local ok, err = pcall(func)
if ok then
error('expected function to fail', 2)
elseif expected ~= nil then
local found = expected == err
if not found and type(expected) == 'string' then
found = string.find(tostring(err), expected, 1, true)
end
if not found then
error('expected function to fail\nexpected:\n'..tostring(expected)..'\ngot:\n'..tostring(err), 2)
end
end
end
--- Check if a function does not fail with a error.
function expect.not_fail(func)
local ok, err = pcall(func)
if not ok then
error('expected function to not fail\ngot error:\n'..expect.tohumanstring(err), 2)
end
end
--- Check if a value is not `nil`.
function expect.exist(v)
if v == nil then
error('expected value to exist\ngot:\n'..expect.tohumanstring(v), 2)
end
end
--- Check if a value is `nil`.
function expect.not_exist(v)
if v ~= nil then
error('expected value to not exist\ngot:\n'..expect.tohumanstring(v), 2)
end
end
--- Check if an expression is evaluates to `true`.
function expect.truthy(v)
if not v then
error('expected expression to be true\ngot:\n'..expect.tohumanstring(v), 2)
end
end
--- Check if an expression is evaluates to `false`.
function expect.falsy(v)
if v then
error('expected expression to be false\ngot:\n'..expect.tohumanstring(v), 2)
end
end
--- Returns raw tostring result for a value.
local function rawtostring(v)
local mt = getmetatable(v)
if mt then
setmetatable(v, nil)
end
local s = tostring(v)
if mt then
setmetatable(v, mt)
end
return s
end
-- Returns key suffix for a string_eq table key.
local function strict_eq_key_suffix(k)
if type(k) == 'string' then
if k:find('^[a-zA-Z_][a-zA-Z0-9]*$') then -- string is a lua field
return '.'..k
elseif k:find'[^ -~\n\t]' then -- string contains non printable ASCII
return '["'..k:gsub('.', function(c) return string.format('\\x%02X', c:byte()) end)..'"]'
else
return '["'..k..'"]'
end
else
return string.format('[%s]', rawtostring(k))
end
end
--- Compare if two values are equal, considering nested tables.
function expect.strict_eq(t1, t2, name)
if rawequal(t1, t2) then return true end
name = name or 'value'
local t1type, t2type = type(t1), type(t2)
if t1type ~= t2type then
return false, string.format("expected types to be equal for %s\nfirst: %s\nsecond: %s",
name, t1type, t2type)
end
if t1type == 'table' then
if getmetatable(t1) ~= getmetatable(t2) then
return false, string.format("expected metatables to be equal for %s\nfirst: %s\nsecond: %s",
name, expect.tohumanstring(t1), expect.tohumanstring(t2))
end
for k,v1 in pairs(t1) do
local ok, err = expect.strict_eq(v1, t2[k], name..strict_eq_key_suffix(k))
if not ok then
return false, err
end
end
for k,v2 in pairs(t2) do
local ok, err = expect.strict_eq(v2, t1[k], name..strict_eq_key_suffix(k))
if not ok then
return false, err
end
end
elseif t1 ~= t2 then
return false, string.format("expected values to be equal for %s\nfirst:\n%s\nsecond:\n%s",
name, expect.tohumanstring(t1), expect.tohumanstring(t2))
end
return true
end
--- Check if two values are equal.
function expect.equal(v1, v2)
local ok, err = expect.strict_eq(v1, v2)
if not ok then
error(err, 2)
end
end
--- Check if two values are not equal.
function expect.not_equal(v1, v2)
if expect.strict_eq(v1, v2) then
local v1s, v2s = expect.tohumanstring(v1), expect.tohumanstring(v2)
error('expected values to be not equal\nfirst value:\n'..v1s..'\nsecond value:\n'..v2s, 2)
end
end
return lester
--[[
The MIT License (MIT)
Copyright (c) 2021-2023 Eduardo Bart (https://github.com/edubart)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
]]