Add argument type check tests for basic library
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
-- utilities to check that args of various types pass or fail
|
||||
-- argument type checking
|
||||
|
||||
akey = 'aa'
|
||||
astring = 'abc'
|
||||
astrnum = '789'
|
||||
anumber = 1.23
|
||||
aboolean = true
|
||||
atable = {aa=11,bb=22}
|
||||
atable = {[akey]=456}
|
||||
afunction = function() end
|
||||
anil = nil
|
||||
|
||||
@@ -12,11 +14,13 @@ anylua = { anil, astring, anumber, aboolean, atable, afunction }
|
||||
|
||||
somestring = { astring }
|
||||
somenumber = { anumber }
|
||||
somestrnum = { astring, anumber }
|
||||
somestrnum = { anumber, astrnum }
|
||||
someboolean = { aboolean }
|
||||
sometable = { atable }
|
||||
somefunction = { afunction }
|
||||
somenil = { anil }
|
||||
somekey = { akey }
|
||||
notakey = { astring, anumber, aboolean, atable, afunction }
|
||||
|
||||
local function contains(set,val)
|
||||
local m = #set
|
||||
@@ -180,18 +184,6 @@ function checkallfail( name, typesets )
|
||||
end
|
||||
end
|
||||
|
||||
function checkfail( name, ... )
|
||||
subbanner('checkfail')
|
||||
local v = {...}
|
||||
local sig = signature(name,v)
|
||||
local s,e = invoke( name, v )
|
||||
if not s then
|
||||
print( ok, sig )
|
||||
else
|
||||
print( needcheck, sig, e )
|
||||
end
|
||||
end
|
||||
|
||||
-- check that all combinations of arguments fail in some way,
|
||||
-- ignore error messages
|
||||
function checkallerrors( name, typesets, template )
|
||||
|
||||
@@ -37,7 +37,7 @@ checkallerrors('getfenv', {{true,{},'abc'}}, 'bad argument')
|
||||
-- getmetatable
|
||||
banner('getmetatable')
|
||||
checkallpass('getmetatable', {notanil})
|
||||
checkfail('getmetatable',nil)
|
||||
checkallerrors('getmetatable',{},'bad argument')
|
||||
|
||||
-- ipairs
|
||||
banner('ipairs')
|
||||
@@ -62,12 +62,92 @@ checkallerrors('loadstring', {{'return'},{afunction,atable}}, 'bad argument')
|
||||
|
||||
-- next
|
||||
banner('next')
|
||||
checkallpass('next', {{{aa=11}},{nil,'aa'}})
|
||||
-- checkallpass('next', {{{aa=11}},{nil,'aa'}})
|
||||
checkallpass('next', {sometable,somekey})
|
||||
checkallerrors('next', {notatable,{nil,1}}, 'bad argument')
|
||||
checkallerrors('next', {sometable,{astring,afunction,atable}}, 'invalid key')
|
||||
checkallerrors('next', {sometable,notakey}, 'invalid key')
|
||||
|
||||
-- pairs
|
||||
banner('pairs')
|
||||
checkallpass('pairs', {sometable})
|
||||
checkallerrors('pairs', {notatable}, 'bad argument')
|
||||
|
||||
-- pcall
|
||||
banner('pcall')
|
||||
checkallpass('pcall', {notanil,anylua})
|
||||
checkallerrors('pcall',{},'bad argument')
|
||||
|
||||
-- print
|
||||
banner('print')
|
||||
checkallpass('print', {})
|
||||
checkallpass('print', {{anil,astring,anumber,aboolean}})
|
||||
|
||||
-- rawequal
|
||||
banner('rawequal')
|
||||
checkallpass('rawequal', {notanil,notanil})
|
||||
checkallerrors('rawequal', {notanil}, 'bad argument')
|
||||
checkallerrors('rawequal', {}, 'bad argument')
|
||||
|
||||
-- rawget
|
||||
banner('rawget')
|
||||
checkallpass('rawget', {sometable,somekey})
|
||||
checkallpass('rawget', {sometable,notakey})
|
||||
checkallerrors('rawget', {notatable,notakey}, 'bad argument')
|
||||
checkallerrors('rawget', {}, 'bad argument')
|
||||
|
||||
-- rawset
|
||||
banner('rawset')
|
||||
checkallpass('rawset', {sometable,somekey,notanil})
|
||||
checkallpass('rawset', {sometable,notakey,notanil})
|
||||
checkallerrors('rawset', {sometable,somekey}, 'bad argument')
|
||||
checkallerrors('rawset', {notatable,somestring,somestring}, 'bad argument')
|
||||
checkallerrors('rawset', {}, 'bad argument')
|
||||
|
||||
-- select
|
||||
banner('select')
|
||||
checkallpass('select', {{anumber,'#'},anylua})
|
||||
checkallerrors('select', {notanumber}, 'bad argument')
|
||||
|
||||
-- setfenv
|
||||
banner('setfenv')
|
||||
checkallpass('setfenv', {{function()end},sometable})
|
||||
checkallerrors('setfenv', {{function()end}}, 'bad argument')
|
||||
checkallerrors('setfenv', {{function()end},notatable}, 'bad argument')
|
||||
|
||||
-- setmetatable
|
||||
banner('setmetatable')
|
||||
checkallpass('setmetatable', {sometable,sometable})
|
||||
checkallpass('setmetatable', {sometable,{anil,atable},{'anchor'}})
|
||||
checkallerrors('setmetatable',{notatable,sometable},'bad argument')
|
||||
checkallerrors('setmetatable',{sometable,notatable},'bad argument')
|
||||
|
||||
-- tonumber
|
||||
banner('tonumber')
|
||||
checkallpass('tonumber',{somestrnum,{nil,2,10,36}})
|
||||
checkallpass('tonumber',{notastrnum,{nil,10}})
|
||||
checkallerrors('tonumber',{notastrnum,{2,9,11,36}},'bad argument')
|
||||
checkallerrors('tonumber',{somestrnum,{1,37,atable,afunction,aboolean}},'bad argument')
|
||||
|
||||
-- tostring
|
||||
banner('tostring')
|
||||
checkallpass('tostring',{notanil})
|
||||
checkallpass('tostring',{anylua,{'anchor'}})
|
||||
checkallerrors('tostring',{},'bad argument')
|
||||
|
||||
-- type
|
||||
banner('type')
|
||||
checkallpass('type',{notanil})
|
||||
checkallpass('type',{anylua,{'anchor'}})
|
||||
checkallerrors('type',{},'bad argument')
|
||||
|
||||
-- unpack
|
||||
banner('unpack')
|
||||
checkallpass('unpack',{sometable,{nil,anumber,astrnum},{nil,anumber,astrnum}})
|
||||
checkallerrors('unpack',{notatable,{nil,anumber,astrnum},{nil,anumber,astrnum}},'bad argument')
|
||||
|
||||
-- xpcall
|
||||
banner('xpcall')
|
||||
checkallpass('xpcall', {notanil,notanil})
|
||||
checkallerrors('xpcall',{},'bad argument')
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user