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

lunarmodules / Penlight / 46279356752

29 Nov 2026 03:36PM UTC coverage: 83.225% (-7.1%) from 90.342%
46279356752

push

github

web-flow
Merge 0622ec222 into 44007e542

5125 of 6158 relevant lines covered (83.23%)

880.41 hits per line

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

42.19
/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'
22 ✔
12
local utils = require 'pl.utils'
22 ✔
13
local pretty = require 'pl.pretty'
22 ✔
14
local path = require 'pl.path'
22 ✔
15
local type,unpack,pack = type,utils.unpack,utils.pack
22 ✔
16
local clock = os.clock
22 ✔
17
local debug = require 'debug'
22 ✔
18
local io = io
22 ✔
19

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

30
local test = {}
22 ✔
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)
22 ✔
36
    local err = io.stderr
×
37
    err:write(path.basename(file)..':'..line..': assertion failed\n')
×
38
    err:write("got:\t",got_text,'\n')
×
39
    err:write("needed:\t",needed_text,'\n')
×
40
    utils.quit(1,msg or "these values were not equal")
×
41
end
42

43
local function complain (x,y,msg,where)
44
    local i = debug.getinfo(3 + (where or 0))
×
45
    test.error_handler(i.short_src,i.currentline,dump(x),dump(y),msg)
×
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
22 ✔
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)
22 ✔
64
    local res = x == y
×
65
    if not res then
×
66
        res = tablex.deepcompare(x,y,true,eps)
×
67
    end
68
    if not res then
×
69
        complain(x,y,nil,where)
×
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)
22 ✔
78
    if not s1:match(s2) then
×
79
        complain (s1,s2,"these strings did not match",where)
×
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)
22 ✔
88
    local ok, err
89
    if type(fn) == 'table' then
22 ✔
90
        ok, err = pcall(unpack(fn))
×
91
    else
92
        ok, err = pcall(fn)
26 ✔
93
    end
94
    if ok or err:match(e)==nil then
22 ✔
95
        complain (err,e,"these errors did not match",where)
×
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)
22 ✔
108
    if x1 ~= y1 then complain(x1,y1,nil,where) end
×
109
    if x2 ~= y2 then complain(x2,y2,nil,where) end
×
110
end
111

112
-- tuple type --
113

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

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

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

136
function tuple_mt.__len(self)
22 ✔
137
    return self.n
×
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(...)
22 ✔
149
    return setmetatable(pack(...), tuple_mt)
×
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,...)
22 ✔
159
    local start = clock()
×
160
    for i = 1,n do fun(...) end
×
161
    utils.printf("%s: took %7.2f sec\n",msg,clock()-start)
×
162
end
163

164
return test
22 ✔
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