• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lunarmodules / Penlight / 46284531570

29 Nov 2026 04:31PM UTC coverage: 91.12%. Remained the same
46284531570

push

github

web-flow
Merge 75e173bae into 44007e542

5623 of 6171 relevant lines covered (91.12%)

1248.76 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/lua/pl/test.lua
1
--- Useful test utilities.
2
--
3
--    test.asserteq({1,2},{1,2}) -- can compare tables
4
--    test.asserteq(1.2,1.19,0.02) -- compare FP numbers within precision
5
--    T = test.tuple -- used for comparing multiple results
6
--    test.asserteq(T(string.find(" me","me")),T(2,3))
7
--
8
-- Dependencies: `pl.utils`, `pl.tablex`, `pl.pretty`, `pl.path`, `debug`
9
-- @module pl.test
10

11
local tablex = require 'pl.tablex'
49 ✔
12
local utils = require 'pl.utils'
49 ✔
13
local pretty = require 'pl.pretty'
49 ✔
14
local path = require 'pl.path'
49 ✔
15
local type,unpack,pack = type,utils.unpack,utils.pack
49 ✔
16
local clock = os.clock
49 ✔
17
local debug = require 'debug'
49 ✔
18
local io = io
49 ✔
19

20
local function dump(x)
21
    if type(x) == 'table' and not (getmetatable(x) and getmetatable(x).__tostring) then
494 ✔
22
        return pretty.write(x,' ',true)
57 ✔
23
    elseif type(x) == 'string' then
437 ✔
24
        return '"'..x..'"'
171 ✔
25
    else
26
        return tostring(x)
266 ✔
27
    end
28
end
29

30
local test = {}
49 ✔
31

32
---- error handling for test results.
33
-- By default, this writes to stderr and exits the program.
34
-- Re-define this function to raise an error and/or redirect output
35
function test.error_handler(file,line,got_text, needed_text,msg)
49 ✔
36
    local err = io.stderr
38 ✔
37
    err:write(path.basename(file)..':'..line..': assertion failed\n')
48 ✔
38
    err:write("got:\t",got_text,'\n')
38 ✔
39
    err:write("needed:\t",needed_text,'\n')
38 ✔
40
    utils.quit(1,msg or "these values were not equal")
38 ✔
41
end
42

43
local function complain (x,y,msg,where)
44
    local i = debug.getinfo(3 + (where or 0))
247 ✔
45
    test.error_handler(i.short_src,i.currentline,dump(x),dump(y),msg)
333 ✔
46
end
47

48
--- general test complain message.
49
-- Useful for composing new test functions (see tests/tablex.lua for an example)
50
-- @param x a value
51
-- @param y value to compare first value against
52
-- @param msg message
53
-- @param where extra level offset for errors
54
-- @function complain
55
test.complain = complain
49 ✔
56

57
--- like assert, except takes two arguments that must be equal and can be tables.
58
-- If they are plain tables, it will use tablex.deepcompare.
59
-- @param x any value
60
-- @param y a value equal to x
61
-- @param eps an optional tolerance for numerical comparisons
62
-- @param where extra level offset
63
function test.asserteq (x,y,eps,where)
49 ✔
64
    local res = x == y
252 ✔
65
    if not res then
247 ✔
66
        res = tablex.deepcompare(x,y,true,eps)
216 ✔
67
    end
68
    if not res then
247 ✔
69
        complain(x,y,nil,where)
114 ✔
70
    end
71
end
72

73
--- assert that the first string matches the second.
74
-- @param s1 a string
75
-- @param s2 a string
76
-- @param where extra level offset
77
function test.assertmatch (s1,s2,where)
49 ✔
78
    if not s1:match(s2) then
38 ✔
79
        complain (s1,s2,"these strings did not match",where)
19 ✔
80
    end
81
end
82

83
--- assert that the function raises a particular error.
84
-- @param fn a function or a table of the form {function,arg1,...}
85
-- @param e a string to match the error against
86
-- @param where extra level offset
87
function test.assertraise(fn,e,where)
49 ✔
88
    local ok, err
89
    if type(fn) == 'table' then
106 ✔
90
        ok, err = pcall(unpack(fn))
24 ✔
91
    else
92
        ok, err = pcall(fn)
107 ✔
93
    end
94
    if ok or err:match(e)==nil then
106 ✔
95
        complain (err,e,"these errors did not match",where)
38 ✔
96
    end
97
end
98

99
--- a version of asserteq that takes two pairs of values.
100
-- <code>x1==y1 and x2==y2</code> must be true. Useful for functions that naturally
101
-- return two values.
102
-- @param x1 any value
103
-- @param x2 any value
104
-- @param y1 any value
105
-- @param y2 any value
106
-- @param where extra level offset
107
function test.asserteq2 (x1,x2,y1,y2,where)
49 ✔
108
    if x1 ~= y1 then complain(x1,y1,nil,where) end
57 ✔
109
    if x2 ~= y2 then complain(x2,y2,nil,where) end
57 ✔
110
end
111

112
-- tuple type --
113

114
local tuple_mt = {
49 ✔
115
    unpack = unpack
49 ✔
116
}
117
tuple_mt.__index = tuple_mt
49 ✔
118

119
function tuple_mt.__tostring(self)
49 ✔
120
    local ts = {}
38 ✔
121
    for i=1, self.n do
95 ✔
122
        local s = self[i]
57 ✔
123
        ts[i] = type(s) == 'string' and ('%q'):format(s) or tostring(s)
59 ✔
124
    end
125
    return 'tuple(' .. table.concat(ts, ', ') .. ')'
38 ✔
126
end
127

128
function tuple_mt.__eq(a, b)
49 ✔
129
    if a.n ~= b.n then return false end
95 ✔
130
    for i=1, a.n do
190 ✔
131
        if a[i] ~= b[i] then return false end
133 ✔
132
    end
133
    return true
57 ✔
134
end
135

136
function tuple_mt.__len(self)
49 ✔
137
    return self.n
13 ✔
138
end
139

140
--- encode an arbitrary argument list as a tuple.
141
-- This can be used to compare to other argument lists, which is
142
-- very useful for testing functions which return a number of values.
143
-- Unlike regular array-like tables ('sequences') they may contain nils.
144
-- Tuples understand equality and know how to print themselves out.
145
-- The # operator is defined to be the size, irrespecive of any nils,
146
-- and there is an `unpack` method.
147
-- @usage asserteq(tuple( ('ab'):find 'a'), tuple(1,1))
148
function test.tuple(...)
49 ✔
149
    return setmetatable(pack(...), tuple_mt)
347 ✔
150
end
151

152
--- Time a function. Call the function a given number of times, and report the number of seconds taken,
153
-- together with a message.  Any extra arguments will be passed to the function.
154
-- @string msg a descriptive message
155
-- @int n number of times to call the function
156
-- @func fun the function
157
-- @param ... optional arguments to fun
158
function test.timer(msg,n,fun,...)
49 ✔
159
    local start = clock()
38 ✔
160
    for i = 1,n do fun(...) end
114 ✔
161
    utils.printf("%s: took %7.2f sec\n",msg,clock()-start)
38 ✔
162
end
163

164
return test
49 ✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc