Improve argument type check tests.

This commit is contained in:
James Roseborough
2008-07-17 01:23:33 +00:00
parent 02cffe8a58
commit a18c16dabb
6 changed files with 112 additions and 165 deletions

View File

@@ -1,5 +1,10 @@
-- utilities to check that args of various types pass or fail
-- argument type checking
local ok = '-\t'
local fail = 'fail '
local needcheck = 'needcheck '
local badmsg = 'badmsg '
akey = 'aa'
astring = 'abc'
@@ -11,12 +16,12 @@ aboolean = true
atable = {[akey]=456}
afunction = function() end
anil = nil
athread = coroutine.create(afunction)
anylua = { anil, astring, anumber, aboolean, atable, afunction }
anylua = { nil, astring, anumber, aboolean, atable, afunction, athread }
somestring = { astring }
somenumber = { anumber }
somestrnum = { anumber, astrnum }
somestring = { astring, anumber }
somenumber = { anumber, astrnum }
someboolean = { aboolean }
sometable = { atable }
somefunction = { afunction }
@@ -24,36 +29,21 @@ somenil = { anil }
somekey = { akey }
notakey = { astring, anumber, aboolean, atable, afunction }
local function contains(set,val)
local m = #set
for i=1,m do
if set[i] == val then
return true
end
end
return val == nil
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 = { 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 }
notastring = except(somestring)
notanumber = except(somenumber)
notastrnum = except(somestrnum)
notaboolean = except(someboolean)
notatable = except(sometable)
notafunction = except(somefunction)
notanil = except(somenil)
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 }
local structtypes = {
['table']='<table>',
@@ -108,33 +98,6 @@ local function arglists(typesets)
return ipairs(argsets)
end
--[[
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
--]]
local function lookup( name )
return loadstring('return '..name)()
@@ -155,11 +118,6 @@ local function subbanner(name)
print( '--- '..tostring(name) )
end
local ok = 'ok '
local fail = 'fail '
local needcheck = 'needcheck '
local badmsg = 'badmsg '
-- check that all combinations of arguments pass
function checkallpass( name, typesets )
subbanner('checkallpass')
@@ -174,21 +132,6 @@ function checkallpass( name, typesets )
end
end
-- check that all combinations of arguments fail in some way,
-- ignore error messages
function checkallfail( name, typesets )
subbanner('checkallfail')
for i,v in arglists(typesets) do
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
end
-- check that all combinations of arguments fail in some way,
-- ignore error messages
function checkallerrors( name, typesets, template )

View File

@@ -3,136 +3,138 @@ require 'args'
-- arg types for basic library functions
local somestrnumnil={anil,astring,anumber}
local notastrnumnil={aboolean,atable,afunction}
-- assert
banner('assert')
checkallpass('assert',{{true,123},anylua})
checkallfail('assert',{{nil,false},{nil,'message'}})
checkallerrors('assert',{{nil,false},{nil}},'assertion failed')
checkallerrors('assert',{{nil,false},{'message'}},'message')
-- collectgarbage
banner('collectgarbage')
checkallpass('collectgarbage',{{'collect','count'}})
checkallerrors('collectgarbage',{notanil},'bad argument')
checkallerrors('collectgarbage',{notanil},'bad argument #1')
-- dofila
-- dofile
banner('dofile')
checkallpass('dofile', {{nil,'src/test/errors/args.lua','args.lua'}})
checkallerrors('dofile', {notastrnumnil}, 'bad argument')
checkallpass('dofile', {{nil,'src/test/errors/args.lua'}})
checkallerrors('dofile', {{'args.lua'}}, 'cannot open args.lua')
checkallerrors('dofile', {nonstring}, 'bad argument #1')
-- error
banner('error')
checkallfail('error', {anylua,{nil,0,1,2}})
checkallerrors('dofile', {{'message'},{nil,0,1,2}}, 'message')
checkallerrors('dofile', {{123},{nil,1,2}}, 123)
checkallerrors('error', {{'message'},{nil,0,1,2}}, 'message')
checkallerrors('error', {{123},{nil,1,2}}, 123)
-- getfenv
banner('getfenv')
checkallpass('getfenv', {{nil,print,function()end,0,1,2}})
checkallerrors('getfenv', {{true,{},'abc'}}, 'bad argument')
checkallerrors('getfenv', {{true,{},'abc'}}, 'bad argument #1')
-- getmetatable
banner('getmetatable')
checkallpass('getmetatable', {notanil})
checkallerrors('getmetatable',{},'bad argument')
checkallerrors('getmetatable',{},'bad argument #1')
-- ipairs
banner('ipairs')
checkallpass('ipairs', {sometable})
checkallerrors('ipairs', {notatable}, 'bad argument')
checkallerrors('ipairs', {notatable}, 'bad argument #1')
-- load
banner('load')
checkallpass('load', {somefunction,{anil,astring}})
checkallerrors('load', {notafunction,{anil,astring,anumber}}, 'bad argument')
checkallerrors('load', {somefunction,{afunction,atable}}, 'bad argument')
checkallpass('load', {somefunction,{nil,astring}})
checkallerrors('load', {notafunction,{nil,astring,anumber}}, 'bad argument #1')
checkallerrors('load', {somefunction,{afunction,atable}}, 'bad argument #2')
-- loadfile
banner('loadfile')
checkallerrors('loadfile', {notastring}, 'bad argument')
checkallpass('loadfile', {})
checkallerrors('loadfile', {nonstring}, 'bad argument #1')
-- loadstring
banner('loadstring')
checkallpass('loadstring', {{'return'},{anil,astring}})
checkallerrors('loadstring', {notastring,{anil,astring,anumber}}, 'bad argument')
checkallerrors('loadstring', {{'return'},{afunction,atable}}, 'bad argument')
checkallpass('loadstring', {{'return'},{nil,astring}})
checkallerrors('loadstring', {notastring,{nil,astring,anumber}}, 'bad argument #1')
checkallerrors('loadstring', {{'return'},{afunction,atable}}, 'bad argument #2')
-- next
banner('next')
-- checkallpass('next', {{{aa=11}},{nil,'aa'}})
checkallpass('next', {sometable,somekey})
checkallerrors('next', {notatable,{nil,1}}, 'bad argument')
checkallerrors('next', {sometable,notakey}, 'invalid key')
checkallerrors('next', {notatable,{nil,1}}, 'bad argument #1')
checkallerrors('next', {sometable,nonkey}, 'invalid key')
-- pairs
banner('pairs')
checkallpass('pairs', {sometable})
checkallerrors('pairs', {notatable}, 'bad argument')
checkallerrors('pairs', {notatable}, 'bad argument #1')
-- pcall
banner('pcall')
checkallpass('pcall', {notanil,anylua})
checkallerrors('pcall',{},'bad argument')
checkallerrors('pcall',{},'bad argument #1')
-- print
banner('print')
checkallpass('print', {})
checkallpass('print', {{anil,astring,anumber,aboolean}})
checkallpass('print', {{nil,astring,anumber,aboolean}})
-- rawequal
banner('rawequal')
checkallpass('rawequal', {notanil,notanil})
checkallerrors('rawequal', {notanil}, 'bad argument')
checkallerrors('rawequal', {}, 'bad argument')
checkallerrors('rawequal', {}, 'bad argument #1')
checkallerrors('rawequal', {notanil}, 'bad argument #2')
-- rawget
banner('rawget')
checkallpass('rawget', {sometable,somekey})
checkallpass('rawget', {sometable,notakey})
checkallerrors('rawget', {notatable,notakey}, 'bad argument')
checkallerrors('rawget', {}, 'bad argument')
checkallpass('rawget', {sometable,nonkey})
checkallerrors('rawget', {sometable,somenil},'bad argument #2')
checkallerrors('rawget', {notatable,notakey}, 'bad argument #1')
checkallerrors('rawget', {}, 'bad argument #1')
-- 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')
checkallpass('rawset', {sometable,nonkey,notanil})
checkallerrors('rawset', {sometable,somenil},'table index is nil')
checkallerrors('rawset', {}, 'bad argument #1')
checkallerrors('rawset', {notatable,somestring,somestring}, 'bad argument #1')
checkallerrors('rawset', {sometable,somekey}, 'bad argument #3')
-- select
banner('select')
checkallpass('select', {{anumber,'#'},anylua})
checkallerrors('select', {notanumber}, 'bad argument')
checkallerrors('select', {notanumber}, 'bad argument #1')
-- setfenv
banner('setfenv')
checkallpass('setfenv', {{function()end},sometable})
checkallerrors('setfenv', {{function()end}}, 'bad argument')
checkallerrors('setfenv', {{function()end},notatable}, 'bad argument')
checkallerrors('setfenv', {{1.23, '1.33'},{getfenv()}}, 'cannot change environment of given object')
checkallerrors('setfenv', {{atable,athread,aboolean,astring},sometable}, 'bad argument #1')
checkallerrors('setfenv', {notafunction}, 'bad argument #2')
checkallerrors('setfenv', {anylua}, 'bad argument #2')
checkallerrors('setfenv', {{function()end},notatable}, 'bad argument #2')
-- 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')
checkallpass('setmetatable', {sometable,{nil,atable},{'anchor'}})
checkallerrors('setmetatable',{notatable,sometable},'bad argument #1')
checkallerrors('setmetatable',{sometable,notatable},'bad argument #2')
-- 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')
checkallpass('tonumber',{somenumber,{nil,2,10,36}})
checkallpass('tonumber',{notanil,{nil,10}})
checkallerrors('tonumber',{{nil,afunction,atable},{2,9,11,36}},'bad argument #1')
checkallerrors('tonumber',{somenumber,{1,37,atable,afunction,aboolean}},'bad argument #2')
-- tostring
banner('tostring')
checkallpass('tostring',{notanil})
checkallpass('tostring',{anylua,{'anchor'}})
checkallerrors('tostring',{},'bad argument')
checkallerrors('tostring',{},'bad argument #1')
-- type
banner('type')
@@ -142,12 +144,16 @@ 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')
checkallpass('unpack',{sometable})
checkallpass('unpack',{sometable,somenumber})
checkallpass('unpack',{sometable,somenumber,somenumber})
checkallerrors('unpack',{notatable,somenumber,somenumber},'bad argument #1')
checkallerrors('unpack',{sometable,nonnumber,somenumber},'bad argument #2')
checkallerrors('unpack',{sometable,somenumber,nonnumber},'bad argument #3')
-- xpcall
banner('xpcall')
checkallpass('xpcall', {notanil,notanil})
checkallerrors('xpcall',{},'bad argument')
checkallerrors('xpcall',{anylua},'bad argument #2')

View File

@@ -12,7 +12,7 @@ checkallerrors('coroutine.create',{notafunction},'bad argument')
banner('coroutine.resume')
local co = coroutine.create(function() while true do coroutine.yield() end end)
checkallpass('coroutine.resume',{{co},anylua})
checkallerrors('coroutine.resume',{anylua},'bad argument')
checkallerrors('coroutine.resume',{notathread},'bad argument #1')
-- coroutine.running
banner('coroutine.running')
@@ -21,12 +21,12 @@ checkallpass('coroutine.running',{anylua})
-- coroutine.status
banner('coroutine.status')
checkallpass('coroutine.status',{{co}})
checkallerrors('coroutine.status',{anylua},'bad argument')
checkallerrors('coroutine.status',{notathread},'bad argument #1')
-- coroutine.wrap
banner('coroutine.wrap')
checkallpass('coroutine.wrap',{somefunction})
checkallerrors('coroutine.wrap',{notafunction},'bad argument')
checkallerrors('coroutine.wrap',{notafunction},'bad argument #1')
-- coroutine.yield
banner('coroutine.yield')

View File

@@ -3,7 +3,7 @@ require 'args'
-- arg type tests for math library functions
local somenumber = {23,45.67,'-12','-345.678'}
local notanumber = {nil,astring,aboolean,afunction,atable}
local notanumber = {nil,astring,aboolean,afunction,atable,athread}
local nonnumber = {astring,aboolean,afunction,atable}
local singleargfunctions = {

View File

@@ -23,7 +23,7 @@ checkallerrors('package.seeall',{notatable},'bad argument')
-- module (last because it pretty much breaks this chunk)
print( pcall( function()
banner('module')
checkallpass('module',{somestrnum,{package.seeall},{nil,afunction}})
checkallpass('module',{somenumber,{package.seeall},{nil,afunction}})
checkallerrors('module',{{aboolean,atable,afunction}},'bad argument')
checkallerrors('module',{{aboolean,atable,afunction},{package.seeall}},'bad argument')
checkallerrors('module',{somestring,{astring,anumber,aboolean,atable}},'attempt to call a')

View File

@@ -2,10 +2,6 @@ package.path = "?.lua;src/test/errors/?.lua"
require 'args'
-- arg type tests for string library functions
local somestring = {astring,anumber}
local notastring = {nil,aboolean,afunction,atable}
local somenumber = {ainteger,adouble,tostring(ainteger),tostring(adouble)}
local notanumber = {nil,astring,aboolean,afunction,atable}
-- string.byte
banner('string.byte')
@@ -17,8 +13,10 @@ checkallerrors('string.byte',{notastring,{nil,111}},'bad argument')
banner('string.char')
checkallpass('string.char',{{nil,0,1,40,127,128,255,'0','1','255','1.2',1.2}})
checkallpass('string.char',{{0,127,255},{0,127,255}})
checkallerrors('string.char',{{-1,256}},'bad argument')
checkallerrors('string.char',{notanumber,notanumber},'bad argument')
checkallpass('string.char',{})
checkallerrors('string.char',{{-1,256}},'bad argument #1')
checkallerrors('string.char',{notanumber,{23,'45',6.7}},'bad argument #1')
checkallerrors('string.char',{{23,'45',6.7},nonnumber},'bad argument #2')
-- string.dump
banner('string.dump')
@@ -30,74 +28,74 @@ banner('string.find')
checkallpass('string.find',{somestring,somestring})
checkallpass('string.find',{somestring,somestring,{nil,-3,3}})
checkallpass('string.find',{somestring,somestring,somenumber,anylua})
checkallerrors('string.find',{somestring,notastring},'bad argument')
checkallerrors('string.find',{notastring,somestring},'bad argument')
checkallerrors('string.find',{somestring,somestring,notanumber},'bad argument')
checkallerrors('string.find',{notastring,somestring},'bad argument #1')
checkallerrors('string.find',{somestring,notastring},'bad argument #2')
checkallerrors('string.find',{somestring,somestring,nonnumber},'bad argument #3')
-- string.format
local numfmts = {'%c','%d','%E','%e','%f','%g','%G','%i','%o','%u','%X','%x'}
local strfmts = {'%q','%s'}
banner('string.format')
checkallpass('string.format',{somestring,anylua})
checkallpass('string.format',{numfmts,{ainteger,adouble,astrnum}})
checkallpass('string.format',{strfmts,{astring,ainteger,adouble,astrnum}})
checkallerrors('string.format',{numfmts,notastring},'bad argument')
checkallerrors('string.format',{strfmts,notastring},'bad argument')
checkallpass('string.format',{numfmts,somenumber})
checkallpass('string.format',{strfmts,somestring})
checkallerrors('string.format',{numfmts,notanumber},'bad argument #2')
checkallerrors('string.format',{strfmts,notastring},'bad argument #2')
-- string.gmatch
banner('string.gmatch')
checkallpass('string.gmatch',{somestring,somestring})
checkallerrors('string.gmatch',{somestring,notastring},'bad argument')
checkallerrors('string.gmatch',{notastring,somestring},'bad argument')
checkallerrors('string.gmatch',{notastring,somestring},'bad argument #1')
checkallerrors('string.gmatch',{somestring,notastring},'bad argument #2')
-- string.gsub
local somerepl = {astring,atable,afunction}
local notarepl = {nil,aboolean}
banner('string.gsub')
checkallpass('string.gsub',{somestring,somestring,somerepl,{nil,-1}})
checkallerrors('string.gsub',{notastring,somestring,somerepl},'bad argument')
checkallerrors('string.gsub',{somestring,notastring,somerepl},'bad argument')
checkallerrors('string.gsub',{{astring},{astring},notarepl},'bad argument')
checkallerrors('string.gsub',{{astring},{astring},somerepl,notanumber},'bad argument')
checkallerrors('string.gsub',{notastring,somestring,somerepl},'bad argument #1')
checkallerrors('string.gsub',{somestring,notastring,somerepl},'bad argument #2')
checkallerrors('string.gsub',{{astring},{astring},notarepl},'bad argument #3')
checkallerrors('string.gsub',{{astring},{astring},somerepl,nonnumber},'bad argument #4')
-- string.len
banner('string.len')
checkallpass('string.len',{somestring})
checkallerrors('string.len',{notastring},'bad argument')
checkallerrors('string.len',{notastring},'bad argument #1')
-- string.lower
banner('string.lower')
checkallpass('string.lower',{somestring})
checkallerrors('string.lower',{notastring},'bad argument')
checkallerrors('string.lower',{notastring},'bad argument #1')
-- string.match
banner('string.match')
checkallpass('string.match',{somestring,somestring})
checkallpass('string.match',{somestring,somestring,{nil,-3,3}})
checkallerrors('string.match',{somestring,notastring},'bad argument')
checkallerrors('string.match',{notastring,somestring},'bad argument')
checkallerrors('string.match',{somestring,somestring,notanumber},'bad argument')
checkallerrors('string.match',{notastring,somestring},'bad argument #1')
checkallerrors('string.match',{somestring,notastring},'bad argument #2')
checkallerrors('string.match',{somestring,somestring,notanumber},'bad argument #3')
-- string.reverse
banner('string.reverse')
checkallpass('string.reverse',{somestring})
checkallerrors('string.reverse',{notastring},'bad argument')
checkallerrors('string.reverse',{notastring},'bad argument #1')
-- string.rep
banner('string.rep')
checkallpass('string.rep',{somestring,somenumber})
checkallerrors('string.rep',{notastring,somenumber},'bad argument')
checkallerrors('string.rep',{somestring,notanumber},'bad argument')
checkallerrors('string.rep',{notastring,somenumber},'bad argument #1')
checkallerrors('string.rep',{somestring,notanumber},'bad argument #2')
-- string.sub
banner('string.sub')
checkallpass('string.sub',{somestring,somenumber,somenumber})
checkallerrors('string.sub',{notastring,somenumber,somenumber},'bad argument')
checkallerrors('string.sub',{somestring,notanumber,somenumber},'bad argument')
checkallerrors('string.sub',{somestring,somenumber,notanumber},'bad argument')
checkallerrors('string.sub',{notastring,somenumber,somenumber},'bad argument #1')
checkallerrors('string.sub',{somestring,notanumber,somenumber},'bad argument #2')
checkallerrors('string.sub',{somestring,somenumber,nonnumber},'bad argument #3')
-- string.upper
banner('string.upper')
checkallpass('string.upper',{somestring})
checkallerrors('string.upper',{notastring},'bad argument')
checkallerrors('string.upper',{notastring},'bad argument #1')