2016-12-30 11:03:14 +00:00
|
|
|
|
2017-01-05 14:50:44 +00:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Lua 5.2 compat
|
|
|
|
|
|
|
|
if not setfenv then -- Lua 5.2
|
|
|
|
-- based on http://lua-users.org/lists/lua-l/2010-06/msg00314.html
|
|
|
|
-- this assumes f is a function
|
|
|
|
local function findenv(f)
|
|
|
|
local level = 1
|
|
|
|
repeat
|
|
|
|
local name, value = debug.getupvalue(f, level)
|
|
|
|
if name == '_ENV' then return level, value end
|
|
|
|
level = level + 1
|
|
|
|
until name == nil
|
|
|
|
return nil end
|
|
|
|
getfenv = function (f) return(select(2, findenv(f)) or _G) end
|
|
|
|
setfenv = function (f, t)
|
|
|
|
local level = findenv(f)
|
|
|
|
if level then debug.setupvalue(f, level, t) end
|
|
|
|
return f end
|
|
|
|
end
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2016-12-30 11:03:14 +00:00
|
|
|
local SUITE = require('TestSuite').new('resilience')
|
|
|
|
SUITE:setEnviroment{
|
2017-04-03 14:39:19 +00:00
|
|
|
pretty = require('pretty'),
|
|
|
|
setfenv = setfenv,
|
2016-12-30 11:03:14 +00:00
|
|
|
}
|
|
|
|
|
2017-04-03 14:39:19 +00:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Prevent usage of unknown options.
|
|
|
|
|
|
|
|
SUITE:addTest('Dont allow unknown options', function ()
|
|
|
|
local error_msg = bad_call(pretty, 'Hello World', { some_random_option = true })
|
|
|
|
assert(error_msg)
|
|
|
|
end)
|
|
|
|
|
|
|
|
SUITE:addTest('Dont allow bad types in options', function ()
|
|
|
|
local error_msg = bad_call(pretty, 'Hello World', { max_depth = "hello world" })
|
|
|
|
assert(error_msg)
|
|
|
|
end)
|
|
|
|
|
2016-12-30 11:03:14 +00:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2017-01-05 12:22:25 +00:00
|
|
|
SUITE:addTest('no_std_lib', function ()
|
2016-12-30 11:03:14 +00:00
|
|
|
-- This tests whether one could load the library with an empty env, without
|
|
|
|
-- an error.
|
2017-04-14 10:19:23 +00:00
|
|
|
local env = {}
|
|
|
|
local library = setfenv(loadfile('./library.lua'), env)()
|
2017-01-05 12:22:25 +00:00
|
|
|
|
|
|
|
for func, func_info in pairs(library) do
|
|
|
|
error(('For some reason %s is defined in the library'):format(func_info.name))
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
SUITE:addTest('a_very_small_part_of_math', function ()
|
|
|
|
-- This tests whether one could load the library with an empty env, without
|
|
|
|
-- an error.
|
2017-04-14 11:24:29 +00:00
|
|
|
local env = { debug = debug, math = { abs = math.abs } }
|
2017-04-14 10:19:23 +00:00
|
|
|
local library = setfenv(loadfile('./library.lua'), env)()
|
2017-01-05 12:22:25 +00:00
|
|
|
|
|
|
|
assert( library[math.abs], 'Why is math.abs not defined in the library?' )
|
2016-12-30 11:03:14 +00:00
|
|
|
end)
|
|
|
|
|
2017-04-14 10:19:23 +00:00
|
|
|
SUITE:addTest('redefined part of math', function ()
|
|
|
|
-- This tests whether the library ignores a redefined function of an
|
|
|
|
-- builtin, when defining documentation.
|
2017-04-14 11:24:29 +00:00
|
|
|
local env = { debug = debug, setfenv = setfenv, loadfile = function() end }
|
2017-04-14 10:19:23 +00:00
|
|
|
local library = setfenv(loadfile('./library.lua'), env)()
|
|
|
|
|
|
|
|
assert( library[env.setfenv], 'Why is setfenv not defined in the library?' )
|
|
|
|
assert( not library[env.loadfile], 'Why is loadfile defined in the library?' )
|
|
|
|
end)
|
|
|
|
|
2017-04-14 11:24:29 +00:00
|
|
|
SUITE:addTest('redefined part of math 2', function ()
|
|
|
|
-- This tests whether the library allows a redefined function of an
|
|
|
|
-- builtin, when defining documentation, due to not being able to determine
|
|
|
|
-- the origin of the function.
|
|
|
|
local env = { setfenv = setfenv, loadfile = function() end }
|
|
|
|
local library = setfenv(loadfile('./library.lua'), env)()
|
|
|
|
|
|
|
|
assert( library[env.setfenv], 'Why is setfenv not defined in the library?' )
|
|
|
|
assert( library[env.loadfile], 'Why is loadfile not defined in the library?' )
|
|
|
|
end)
|
|
|
|
|
2016-12-30 11:03:14 +00:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
return SUITE
|