2008-07-15 05:32:56 +00:00
|
|
|
-- utilities to check that args of various types pass or fail
|
|
|
|
|
-- argument type checking
|
2008-07-17 01:23:33 +00:00
|
|
|
local ok = '-\t'
|
|
|
|
|
local fail = 'fail '
|
|
|
|
|
local needcheck = 'needcheck '
|
|
|
|
|
local badmsg = 'badmsg '
|
|
|
|
|
|
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'
|
2010-05-15 17:12:50 +00:00
|
|
|
anumber = 1.25
|
2008-07-16 17:38:28 +00:00
|
|
|
ainteger = 345
|
2010-05-15 17:12:50 +00:00
|
|
|
adouble = 12.75
|
2008-07-15 18:35:38 +00:00
|
|
|
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-17 01:23:33 +00:00
|
|
|
athread = coroutine.create(afunction)
|
2008-07-15 05:32:56 +00:00
|
|
|
|
2008-07-17 01:23:33 +00:00
|
|
|
anylua = { nil, astring, anumber, aboolean, atable, afunction, athread }
|
2008-07-15 18:35:38 +00:00
|
|
|
|
2008-07-17 01:23:33 +00:00
|
|
|
somestring = { astring, anumber }
|
|
|
|
|
somenumber = { 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
|
|
|
|
2008-07-17 01:23:33 +00:00
|
|
|
notastring = { nil, aboolean, atable, afunction, athread }
|
|
|
|
|
notanumber = { nil, astring, aboolean, atable, afunction, athread }
|
|
|
|
|
notaboolean = { nil, astring, anumber, atable, afunction, athread }
|
|
|
|
|
notatable = { nil, astring, anumber, aboolean, afunction, athread }
|
|
|
|
|
notafunction = { nil, astring, anumber, aboolean, atable, athread }
|
|
|
|
|
notathread = { nil, astring, anumber, aboolean, atable, afunction }
|
|
|
|
|
notanil = { astring, anumber, aboolean, atable, afunction, athread }
|
|
|
|
|
|
|
|
|
|
nonstring = { aboolean, atable, afunction, athread }
|
|
|
|
|
nonnumber = { astring, aboolean, atable, afunction, athread }
|
|
|
|
|
nonboolean = { astring, anumber, atable, afunction, athread }
|
|
|
|
|
nontable = { astring, anumber, aboolean, afunction, athread }
|
|
|
|
|
nonfunction = { astring, anumber, aboolean, atable, athread }
|
|
|
|
|
nonthread = { astring, anumber, aboolean, atable, afunction }
|
|
|
|
|
nonkey = { astring, anumber, aboolean, atable, afunction }
|
2008-07-15 05:32:56 +00:00
|
|
|
|
2008-07-15 22:13:12 +00:00
|
|
|
local structtypes = {
|
|
|
|
|
['table']='<table>',
|
|
|
|
|
['function']='<function>',
|
|
|
|
|
['thread']='<thread>',
|
|
|
|
|
['userdata']='<userdata>',
|
|
|
|
|
}
|
|
|
|
|
|
2008-07-17 18:49:43 +00:00
|
|
|
local function bracket(v)
|
|
|
|
|
return "<"..type(v)..">"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local function quote(v)
|
|
|
|
|
return "'"..v.."'"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local function ellipses(v)
|
|
|
|
|
local s = tostring(v)
|
2008-07-21 22:12:06 +00:00
|
|
|
return #s <= 8 and s or (string.sub(s,1,8)..'...')
|
2008-07-17 18:49:43 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local pretty = {
|
|
|
|
|
['table']=bracket,
|
|
|
|
|
['function']=bracket,
|
|
|
|
|
['thread']=bracket,
|
|
|
|
|
['userdata']=bracket,
|
|
|
|
|
['string']= quote,
|
|
|
|
|
['number']= ellipses,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
local function values(list)
|
2008-07-15 05:32:56 +00:00
|
|
|
local t = {}
|
2010-05-09 23:51:46 +00:00
|
|
|
for i=1,(list.n or #list) do
|
2008-07-17 18:49:43 +00:00
|
|
|
local ai = list[i]
|
|
|
|
|
local fi = pretty[type(ai)]
|
|
|
|
|
t[i] = fi and fi(ai) or tostring(ai)
|
2008-07-15 05:32:56 +00:00
|
|
|
end
|
2008-07-17 18:49:43 +00:00
|
|
|
return table.concat(t,',')
|
|
|
|
|
end
|
|
|
|
|
|
2008-07-21 22:12:06 +00:00
|
|
|
local function types(list)
|
|
|
|
|
local t = {}
|
2010-05-09 23:51:46 +00:00
|
|
|
for i=1,(list.n or #list) do
|
2008-07-21 22:12:06 +00:00
|
|
|
local ai = list[i]
|
|
|
|
|
t[i] = type(ai)
|
|
|
|
|
end
|
|
|
|
|
return table.concat(t,',')
|
|
|
|
|
end
|
|
|
|
|
|
2008-07-17 18:49:43 +00:00
|
|
|
local function signature(name,arglist)
|
|
|
|
|
return name..'('..values(arglist)..')'
|
2008-07-15 05:32:56 +00:00
|
|
|
end
|
|
|
|
|
|
2008-07-15 18:35:38 +00:00
|
|
|
local function dup(t)
|
|
|
|
|
local s = {}
|
2010-05-09 23:51:46 +00:00
|
|
|
for i=1,(t.n or #t) do
|
2008-07-15 18:35:38 +00:00
|
|
|
s[i] = t[i]
|
|
|
|
|
end
|
|
|
|
|
return s
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local function split(t)
|
|
|
|
|
local s = {}
|
2010-05-09 23:51:46 +00:00
|
|
|
local n = (t.n or #t)
|
2008-07-15 18:35:38 +00:00
|
|
|
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, ...)
|
2012-09-10 04:59:45 +00:00
|
|
|
local arg = table.pack(...)
|
2008-07-15 05:32:56 +00:00
|
|
|
local n = typesets and #typesets or 0
|
|
|
|
|
if n <= 0 then
|
2010-05-09 23:51:46 +00:00
|
|
|
table.insert(argsets,arg)
|
2008-07-15 05:32:56 +00:00
|
|
|
return argsets
|
|
|
|
|
end
|
|
|
|
|
|
2008-07-15 18:35:38 +00:00
|
|
|
local s,v = split(typesets)
|
2010-05-09 23:51:46 +00:00
|
|
|
for i=1,(v.n or #v) do
|
2012-09-07 14:05:41 +00:00
|
|
|
expand(argsets, s, v[i], table.unpack(arg,1,arg.n))
|
2008-07-15 05:32:56 +00:00
|
|
|
end
|
|
|
|
|
return argsets
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local function arglists(typesets)
|
|
|
|
|
local argsets = expand({},typesets)
|
|
|
|
|
return ipairs(argsets)
|
|
|
|
|
end
|
|
|
|
|
|
2010-05-16 17:53:33 +00:00
|
|
|
function lookup( name )
|
2012-09-07 14:05:41 +00:00
|
|
|
return load('return '..name)()
|
2008-07-15 05:32:56 +00:00
|
|
|
end
|
|
|
|
|
|
2010-05-16 17:53:33 +00:00
|
|
|
function invoke( name, arglist )
|
2008-07-15 05:32:56 +00:00
|
|
|
local s,c = pcall(lookup, name)
|
|
|
|
|
if not s then return s,c end
|
2012-09-07 14:05:41 +00:00
|
|
|
return pcall(c, table.unpack(arglist,1,arglist.n or #arglist))
|
2008-07-15 05:32:56 +00:00
|
|
|
end
|
|
|
|
|
|
2008-07-15 18:35:38 +00:00
|
|
|
-- messages, banners
|
2008-07-24 01:28:52 +00:00
|
|
|
local _print = print
|
|
|
|
|
local _tostring = tostring
|
|
|
|
|
local _find = string.find
|
2008-07-15 18:35:38 +00:00
|
|
|
function banner(name)
|
2008-07-24 01:28:52 +00:00
|
|
|
_print( '====== '.._tostring(name)..' ======' )
|
2008-07-15 18:35:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local function subbanner(name)
|
2008-07-24 01:28:52 +00:00
|
|
|
_print( '--- '.._tostring(name) )
|
2008-07-15 18:35:38 +00:00
|
|
|
end
|
|
|
|
|
|
2008-07-23 23:52:20 +00:00
|
|
|
local function pack(s,...)
|
2012-09-08 22:04:56 +00:00
|
|
|
return s,{...}
|
2008-07-23 23:52:20 +00:00
|
|
|
end
|
|
|
|
|
|
2008-07-15 05:32:56 +00:00
|
|
|
-- check that all combinations of arguments pass
|
2008-07-21 22:12:06 +00:00
|
|
|
function checkallpass( name, typesets, typesonly )
|
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)
|
2008-07-23 23:52:20 +00:00
|
|
|
local s,r = pack( invoke( name, v ) )
|
2008-07-15 05:32:56 +00:00
|
|
|
if s then
|
2008-07-21 22:12:06 +00:00
|
|
|
if typesonly then
|
2008-07-24 01:28:52 +00:00
|
|
|
_print( ok, sig, types(r) )
|
2008-07-21 22:12:06 +00:00
|
|
|
else
|
2008-07-24 01:28:52 +00:00
|
|
|
_print( ok, sig, values(r) )
|
2008-07-21 22:12:06 +00:00
|
|
|
end
|
2008-07-15 05:32:56 +00:00
|
|
|
else
|
2008-07-24 01:28:52 +00:00
|
|
|
_print( fail, sig, values(r) )
|
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')
|
2008-07-24 01:28:52 +00:00
|
|
|
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
|
2008-07-24 01:28:52 +00:00
|
|
|
if _find(e, template, 1, true) then
|
|
|
|
|
_print( ok, sig, '...'..template..'...' )
|
2008-07-15 05:32:56 +00:00
|
|
|
else
|
2008-07-24 01:28:52 +00:00
|
|
|
_print( badmsg, sig, "template='"..template.."' actual='"..e.."'" )
|
2008-07-15 05:32:56 +00:00
|
|
|
end
|
|
|
|
|
else
|
2008-07-24 01:28:52 +00:00
|
|
|
_print( needcheck, sig, e )
|
2008-07-15 05:32:56 +00:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|