Improve package lib behavior and error reporting.

This commit is contained in:
James Roseborough
2010-05-16 17:53:33 +00:00
parent fe7658e83b
commit ce13cc8621
14 changed files with 46 additions and 36 deletions

View File

@@ -132,12 +132,11 @@ local function arglists(typesets)
return ipairs(argsets)
end
local function lookup( name )
function lookup( name )
return loadstring('return '..name)()
end
local function invoke( name, arglist )
function invoke( name, arglist )
local s,c = pcall(lookup, name)
if not s then return s,c end
return pcall(c, unpack(arglist,1,arglist.n or #arglist))

View File

@@ -1,6 +1,12 @@
package.path = "?.lua;test/lua/errors/?.lua"
require 'args'
local tostring = tostring
_G.tostring = function(x)
local s = tostring(x)
return type(x)=='number' and #s>4 and (s:sub(1,5)..'...') or s
end
-- arg type tests for math library functions
local somenumber = {1,0.75,'-1','-0.25'}
local somepositive = {1,0.75,'2', '2.5'}

View File

@@ -22,28 +22,25 @@ checkallerrors('package.seeall',{notatable},'bad argument')
-- module tests - require special rigging
banner('module')
print( pcall( function()
checkallpass('module',{{20001}})
end ) )
print( pcall( function()
checkallpass('module',{{20002},{package.seeall}})
end ) )
print( pcall( function()
checkallpass('module',{{20003},{package.seeall},{function() end}})
end ) )
print( pcall( function()
checkallerrors('module',{{aboolean,atable,function() end}},'bad argument')
checkallerrors('module',{{aboolean,atable,function() end},{package.seeall}},'bad argument')
end ) )
print( pcall( function()
checkallerrors('module',{{'testmodule1'},{'pqrs'}},'attempt to call')
end ) )
print( pcall( function()
checkallerrors('module',{{'testmodule2'},{aboolean}},'attempt to call')
end ) )
print( pcall( function()
checkallerrors('module',{{'testmodule3'},{athread}},'attempt to call')
end ) )
print( pcall( function()
checkallerrors('module',{{'testmodule4'},{atable}},'attempt to call')
end ) )
checkallerrors('module',{{20001},{nil,package.seeall,n=2},{nil,function()end,n=2}},"'module' not called from a Lua function")
checkallerrors('module',{{'testmodule1'},{nil,'pqrs',aboolean,athread,atable}},"'module' not called from a Lua function")
checkallerrors('module',{{aboolean,atable,function() end}},'bad argument')
checkallerrors('module',{{aboolean,atable,function() end},{package.seeall}},'bad argument')
-- enclose each invokation in its own function
function invoke( name, arglist )
assert( name=='module', 'module rig used for '..name )
local func = function()
module( unpack(arglist,1,arglist.n or #arglist) )
end
return pcall( func )
end
checkallpass('module',{{'foo1',20001}})
checkallpass('module',{{'foo2',20002},{package.seeall}})
checkallpass('module',{{'foo3',20003},{package.seeall},{function() end}})
checkallerrors('module',{{aboolean,atable,function() end}},'bad argument')
checkallerrors('module',{{aboolean,atable,function() end},{package.seeall}},'bad argument')
checkallerrors('module',{{'testmodule2'},{'pqrs'}},'attempt to call')
checkallerrors('module',{{'testmodule3'},{aboolean}},'attempt to call')
checkallerrors('module',{{'testmodule4'},{athread}},'attempt to call')
checkallerrors('module',{{'testmodule5'},{atable}},'attempt to call')