2007-11-30 16:41:59 +00:00
|
|
|
-- unit tests for require() function
|
|
|
|
|
local ids = {}
|
2007-12-05 01:46:59 +00:00
|
|
|
local ti = table.insert
|
2007-11-30 16:41:59 +00:00
|
|
|
local function id(obj)
|
|
|
|
|
if not obj or type(obj) == 'number' or type(obj) == 'string' then
|
|
|
|
|
return obj
|
|
|
|
|
end
|
|
|
|
|
local v = ids[obj]
|
|
|
|
|
if v then
|
|
|
|
|
return v
|
|
|
|
|
end
|
2007-12-05 01:46:59 +00:00
|
|
|
ti(ids,obj)
|
2007-11-30 16:41:59 +00:00
|
|
|
ids[obj] = type(obj)..'.'..tostring(#ids)
|
|
|
|
|
return ids[obj]
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- tests on require
|
2007-12-05 01:46:59 +00:00
|
|
|
package.path='?.lua;src/test/res/?.lua'
|
2007-11-30 16:41:59 +00:00
|
|
|
function f( name )
|
|
|
|
|
print( module( 'testmod', package.seeall ) )
|
|
|
|
|
print( 'before', id(sample), id(bogus), id(_G[name]) );
|
|
|
|
|
local status,result = pcall( require, name )
|
|
|
|
|
if status then
|
|
|
|
|
print( 'pcall(require,"'..name..'")', status, id(result) )
|
|
|
|
|
print( 'after', id(sample), id(bogus), id(_G[name]) );
|
|
|
|
|
else
|
|
|
|
|
print( 'pcall(require,"'..name..'")', status )
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
f('sample')
|
|
|
|
|
print( 'main', id(sample), id(bogus), id(custom) );
|
|
|
|
|
f('sample')
|
|
|
|
|
print( 'main', id(sample), id(bogus), id(custom) );
|
|
|
|
|
f('bogus')
|
|
|
|
|
print( 'main', id(sample), id(bogus), id(custom) );
|
|
|
|
|
|
|
|
|
|
-- custom loader chain
|
2007-12-05 01:46:59 +00:00
|
|
|
for i=1,3 do
|
2007-11-30 16:41:59 +00:00
|
|
|
print( i,id(package.loaders[i]) )
|
|
|
|
|
end
|
|
|
|
|
function loader1( ... )
|
|
|
|
|
print ('in loader1', ...)
|
|
|
|
|
return "loader1 didn't find anything"
|
|
|
|
|
end
|
|
|
|
|
function loader2( name, ... )
|
|
|
|
|
print ('in loader2', ...)
|
|
|
|
|
if name ~= 'custom' then
|
|
|
|
|
message = "name is not 'custom'"
|
|
|
|
|
print ('loader2 is returning', message )
|
|
|
|
|
return message
|
|
|
|
|
end
|
|
|
|
|
table = {}
|
|
|
|
|
result = function()
|
|
|
|
|
print( 'in loader function, returning', id(table) )
|
|
|
|
|
return table
|
|
|
|
|
end
|
|
|
|
|
print ('loader2 is returning', id(result) )
|
|
|
|
|
return result
|
|
|
|
|
end
|
|
|
|
|
function loader3( ... )
|
|
|
|
|
print ('in loader3', ...)
|
|
|
|
|
return nil
|
|
|
|
|
end
|
|
|
|
|
package.loaders = { loader1, loader2, loader3 }
|
|
|
|
|
f( 'bogus' )
|
|
|
|
|
print( 'main', id(sample), id(bogus), id(custom) );
|
|
|
|
|
f( 'custom' )
|
|
|
|
|
print( 'main', id(sample), id(bogus), id(custom) );
|