Purpose: show a simple way to create and run simple testsuites. Yes, the keyword is "simple"...
The code in "tester.lua" is shown below, but first... an example:
dofile('tester.lua')
tester [[
1
1+1
getn(globals())
tinsert(globals(),"abc")
getn(globals())
tremove(globals())
getn(globals())
]]
The output of this code is:
1 == 1
1+1 == 2
getn(globals()) == 0
tinsert(globals(),"abc") == userdata, tag = 0
getn(globals()) == 1
tremove(globals()) == abc
getn(globals()) == 0
What happens is that each line gets evaluated and printed alongside its result.
Once you have your tests working, you may want to consider turning this into a regression test. That's easy - take the above output and create this script from it:
dofile('tester.lua')
tester [[
1 == 1
1+1 == 2
getn(globals()) == 0
tinsert(globals(),"abc") == userdata, tag = 0
getn(globals()) == 1
tremove(globals()) == abc
getn(globals()) == 0
]]
When you run the above script, it will generate no output - how's that for a time saver!
But wait, what if there are errors? Try this:
dofile('tester.lua')
tester [[
1 == 1
1+1 == 1
1+1 == 2
getn(globals()) == 1
getn(globals()) == 0
]]
The output will be:
1+1 == 2
getn(globals()) == 0
3 results out of 5 matched
FWIW, this little test harness was inspired by a test system for Python, written by Tim Peters.
Here is the "tester.lua" script needed to run the above examples:
#!/usr/bin/env lua
-- a little test harness for Lua scripts
-- 09/02/2001 jcw@equi4.com
-- evaluate each line in turn, optionally comparing results
-- if a comparison is included and it matches, nil is returned
-- otherwise, output is such that it can be re-used as match input
function tester(lines)
local t={}
gsub(lines,"\n*([^\n]+)\n*", function (v) tinsert(%t,v) end)
local f = function (s,t)
local r=dostring('return '..s)
if type(r)=="nil" then
r="nil"
elseif type(r)=="number" then
r=r..""
elseif type(r)=="table" then
r='table, n = '..getn(r)..', tag = '..tag(r)
elseif type(r)=="userdata" then
r='userdata, tag = '..tag(r)
elseif type(r)=="function" then
r=type(r)
end
if r~=t then return format('%36s == %s',s,r) end
end
local m,c=0,0
for i=1,getn(t) do
local l=gsub(gsub(t[i],'^ +',''),' +$','')
if i==getn(t) and l=="" then break end
if strfind(l,' == ') then
c=c+1
local o=gsub(l,'^(.*) == (.*)$',f,1)
if o~="" then print(o) else m=m+1 end
else
print(f(l))
end
end
if m<c then print(m..' results out of '..c..' matched') end
end
2001-02-10
(216.232.136.19)
Note: you are looking at
the snapshot of an old wiki
- much of this information
is likely to be very outdated
