2008-07-15 05:32:56 +00:00
|
|
|
-- utilities to check that args of various types pass or fail
|
|
|
|
|
-- argument type checking
|
|
|
|
|
|
2008-07-15 21:51:42 +00:00
|
|
|
akey = 'aa'
|
2008-07-15 18:35:38 +00:00
|
|
|
astring = 'abc'
|
2008-07-15 21:51:42 +00:00
|
|
|
astrnum = '789'
|
2008-07-15 18:35:38 +00:00
|
|
|
anumber = 1.23
|
|
|
|
|
aboolean = true
|
2008-07-15 21:51:42 +00:00
|
|
|
atable = {[akey]=456}
|
2008-07-15 18:35:38 +00:00
|
|
|
afunction = function() end
|
|
|
|
|
anil = nil
|
2008-07-15 05:32:56 +00:00
|
|
|
|
2008-07-15 18:35:38 +00:00
|
|
|
anylua = { anil, astring, anumber, aboolean, atable, afunction }
|
|
|
|
|
|
|
|
|
|
somestring = { astring }
|
|
|
|
|
somenumber = { anumber }
|
2008-07-15 21:51:42 +00:00
|
|
|
somestrnum = { anumber, astrnum }
|
2008-07-15 18:35:38 +00:00
|
|
|
someboolean = { aboolean }
|
|
|
|
|
sometable = { atable }
|
|
|
|
|
somefunction = { afunction }
|
|
|
|
|
somenil = { anil }
|
2008-07-15 21:51:42 +00:00
|
|
|
somekey = { akey }
|
|
|
|
|
notakey = { astring, anumber, aboolean, atable, afunction }
|
2008-07-15 05:32:56 +00:00
|
|
|
|
|
|
|
|
local function contains(set,val)
|
|
|
|
|
local m = #set
|
|
|
|
|
for i=1,m do
|
|
|
|
|
if set[i] == val then
|
|
|
|
|
return true
|
|
|
|
|
end
|
|
|
|
|
end
|
2008-07-15 18:35:38 +00:00
|
|
|
return val == nil
|
2008-07-15 05:32:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local function except(some)
|
|
|
|
|
local n = #anylua
|
|
|
|
|
local z = {}
|
|
|
|
|
local j = 1
|
|
|
|
|
for i=1,n do
|
|
|
|
|
if not contains(some, anylua[i]) then
|
|
|
|
|
z[j] = anylua[i]
|
|
|
|
|
j = j + 1
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
return z
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
notastring = except(somestring)
|
|
|
|
|
notanumber = except(somenumber)
|
|
|
|
|
notastrnum = except(somestrnum)
|
|
|
|
|
notaboolean = except(someboolean)
|
|
|
|
|
notatable = except(sometable)
|
|
|
|
|
notafunction = except(somefunction)
|
|
|
|
|
notanil = except(somenil)
|
|
|
|
|
|
|
|
|
|
local function signature(name,arglist)
|
|
|
|
|
local t = {}
|
|
|
|
|
for i=1,#arglist do
|
|
|
|
|
if type(arglist[i]) == 'table' then
|
|
|
|
|
t[i] = 'table'
|
|
|
|
|
elseif type(arglist[i]) == 'function' then
|
|
|
|
|
t[i] = 'function'
|
|
|
|
|
else
|
|
|
|
|
t[i] = tostring(arglist[i])
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
return name..'('..table.concat(t,',')..')'
|
|
|
|
|
end
|
|
|
|
|
|
2008-07-15 18:35:38 +00:00
|
|
|
local function dup(t)
|
|
|
|
|
local s = {}
|
|
|
|
|
for i=1,#t do
|
|
|
|
|
s[i] = t[i]
|
|
|
|
|
end
|
|
|
|
|
return s
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local function split(t)
|
|
|
|
|
local s = {}
|
|
|
|
|
local n = #t
|
|
|
|
|
for i=1,n-1 do
|
|
|
|
|
s[i] = t[i]
|
|
|
|
|
end
|
|
|
|
|
return s,t[n]
|
|
|
|
|
end
|
|
|
|
|
|
2008-07-15 05:32:56 +00:00
|
|
|
local function expand(argsets, typesets, ...)
|
|
|
|
|
local n = typesets and #typesets or 0
|
|
|
|
|
if n <= 0 then
|
|
|
|
|
table.insert(argsets,{...})
|
|
|
|
|
return argsets
|
|
|
|
|
end
|
|
|
|
|
|
2008-07-15 18:35:38 +00:00
|
|
|
local s,v = split(typesets)
|
|
|
|
|
for i=1,#v do
|
|
|
|
|
expand(argsets, s, v[i], ...)
|
2008-07-15 05:32:56 +00:00
|
|
|
end
|
|
|
|
|
return argsets
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local function arglists(typesets)
|
|
|
|
|
local argsets = expand({},typesets)
|
|
|
|
|
return ipairs(argsets)
|
|
|
|
|
end
|
|
|
|
|
|
2008-07-15 18:35:38 +00:00
|
|
|
--[[
|
|
|
|
|
local function expand(arglists,fixed,varying,...)
|
|
|
|
|
for i=1,#varying do
|
|
|
|
|
local f = dup(fixed)
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local function arglists(typesets)
|
|
|
|
|
local argsets = {}
|
|
|
|
|
local args={}
|
|
|
|
|
local n = typesets and #typesets or 0
|
|
|
|
|
if n == 0 then
|
|
|
|
|
table.insert( argsets, args )
|
|
|
|
|
end
|
|
|
|
|
for i=1,n do
|
|
|
|
|
local t = typesets[i]
|
|
|
|
|
for j=1,#t do
|
|
|
|
|
args[i] = t[j]
|
|
|
|
|
if i == n then
|
|
|
|
|
table.insert( argsets, duptable(args) )
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
return ipairs(argsets)
|
|
|
|
|
end
|
|
|
|
|
--]]
|
|
|
|
|
|
2008-07-15 05:32:56 +00:00
|
|
|
local function lookup( name )
|
|
|
|
|
return loadstring('return '..name)()
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local function invoke( name, arglist )
|
|
|
|
|
local s,c = pcall(lookup, name)
|
|
|
|
|
if not s then return s,c end
|
|
|
|
|
return pcall(c, unpack(arglist))
|
|
|
|
|
end
|
|
|
|
|
|
2008-07-15 18:35:38 +00:00
|
|
|
-- messages, banners
|
|
|
|
|
function banner(name)
|
|
|
|
|
print( '====== '..tostring(name)..' ======' )
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local function subbanner(name)
|
|
|
|
|
print( '--- '..tostring(name) )
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local ok = 'ok '
|
|
|
|
|
local fail = 'fail '
|
|
|
|
|
local needcheck = 'needcheck '
|
|
|
|
|
local badmsg = 'badmsg '
|
|
|
|
|
|
2008-07-15 05:32:56 +00:00
|
|
|
-- check that all combinations of arguments pass
|
|
|
|
|
function checkallpass( name, typesets )
|
2008-07-15 18:35:38 +00:00
|
|
|
subbanner('checkallpass')
|
2008-07-15 05:32:56 +00:00
|
|
|
for i,v in arglists(typesets) do
|
|
|
|
|
local sig = signature(name,v)
|
|
|
|
|
local s,e = invoke( name, v )
|
|
|
|
|
if s then
|
2008-07-15 18:35:38 +00:00
|
|
|
print( ok, sig )
|
2008-07-15 05:32:56 +00:00
|
|
|
else
|
2008-07-15 18:35:38 +00:00
|
|
|
print( fail, sig, e )
|
2008-07-15 05:32:56 +00:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- check that all combinations of arguments fail in some way,
|
|
|
|
|
-- ignore error messages
|
|
|
|
|
function checkallfail( name, typesets )
|
2008-07-15 18:35:38 +00:00
|
|
|
subbanner('checkallfail')
|
2008-07-15 05:32:56 +00:00
|
|
|
for i,v in arglists(typesets) do
|
|
|
|
|
local sig = signature(name,v)
|
2008-07-15 18:35:38 +00:00
|
|
|
local s,e = invoke( name, v )
|
2008-07-15 05:32:56 +00:00
|
|
|
if not s then
|
2008-07-15 18:35:38 +00:00
|
|
|
print( ok, sig )
|
2008-07-15 05:32:56 +00:00
|
|
|
else
|
2008-07-15 18:35:38 +00:00
|
|
|
print( needcheck, sig, e )
|
2008-07-15 05:32:56 +00:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- check that all combinations of arguments fail in some way,
|
|
|
|
|
-- ignore error messages
|
|
|
|
|
function checkallerrors( name, typesets, template )
|
2008-07-15 18:35:38 +00:00
|
|
|
subbanner('checkallerrors')
|
|
|
|
|
template = tostring(template)
|
2008-07-15 05:32:56 +00:00
|
|
|
for i,v in arglists(typesets) do
|
|
|
|
|
local sig = signature(name,v)
|
2008-07-15 18:35:38 +00:00
|
|
|
local s,e = invoke( name, v )
|
2008-07-15 05:32:56 +00:00
|
|
|
if not s then
|
|
|
|
|
if string.match(e, template) then
|
2008-07-15 18:35:38 +00:00
|
|
|
print( ok, sig, '...'..template..'...' )
|
2008-07-15 05:32:56 +00:00
|
|
|
else
|
2008-07-15 18:35:38 +00:00
|
|
|
print( badmsg, sig, "template='"..template.."' actual='"..e.."'" )
|
2008-07-15 05:32:56 +00:00
|
|
|
end
|
|
|
|
|
else
|
2008-07-15 18:35:38 +00:00
|
|
|
print( needcheck, sig, e )
|
2008-07-15 05:32:56 +00:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|