1
0
pretty/test/test_resilience.lua

157 lines
5.4 KiB
Lua

--------------------------------------------------------------------------------
-- 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
--------------------------------------------------------------------------------
local SUITE = require 'TestSuite' 'resilience'
SUITE:setEnviroment{
pretty = require('pretty'),
setfenv = setfenv,
}
--------------------------------------------------------------------------------
-- 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', { indent = 51 })
assert(error_msg)
end)
--------------------------------------------------------------------------------
-- Strange standard libs
SUITE:addTest('no_std_lib', function ()
-- This tests whether one could load the library with an empty env, without
-- an error.
local env = {}
local library = setfenv(loadfile('./library.lua'), env)()
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.
local env = { debug = debug, math = { abs = math.abs } }
local library = setfenv(loadfile('./library.lua'), env)()
assert( library[math.abs], 'Why is math.abs not defined in the library?' )
end)
SUITE:addTest('redefined part of math', function ()
-- This tests whether the library ignores a redefined function of an
-- builtin, when defining documentation.
local env = { debug = debug, 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( not library[env.loadfile], 'Why is loadfile defined in the library?' )
end)
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)
--------------------------------------------------------------------------------
SUITE:addTest('Proper malformed utf8 escaping (through LÖVE)', function ()
-- This test asserts that malformed unicode strings are escaped. This is
-- done by checking whether LÖVE can draw the sequence of bytes, given by
-- `pretty`. LÖVE have roboust unicode checking, and will throw an error if
-- the characters is not escaped properly.
-- The input strings are gotten from TestSuite's example strings.
local pjk_path = '/tmp/test_pjk_'..os.time()
local conf = [[
function love.conf(t)
-- Graphics Settings
t.window.width = 1
t.window.height = 1
t.window.minwidth = 1
t.window.minheight = 1
-- Modules
t.modules.audio = false
t.modules.event = true
t.modules.graphics = true
t.modules.image = false
t.modules.joystick = false
t.modules.keyboard = false
t.modules.math = false
t.modules.mouse = false
t.modules.physics = false
t.modules.sound = false
t.modules.system = false
t.modules.timer = false
t.modules.touch = false
t.modules.video = false
t.modules.window = true
t.modules.thread = false
end]]
-- Create project, main.lua and conf.lua
os.execute('mkdir '..pjk_path)
local f = io.open(pjk_path..'/conf.lua', 'w')
f:write(conf)
f:close()
local f = io.open(pjk_path..'/main.lua', 'w')
f:write''
f:close()
for i, str in ipairs(EXAMPLES.STRING_UTF8) do
-- Create project and main.lua
local f = io.open(pjk_path..'/main.lua', 'w')
f:write('love.errhand = love.event.quit love.graphics.print('..pretty(str)..') love.event.quit(0)')
f:close()
-- Run project
local status_code = os.execute('love '..pjk_path)
if status_code ~= 0 then
local l = {}
for i = 1, #str do l[i] = tostring(str:byte(i)) end
error('Could not escape following string properly: '..str..'\n\tByte-sequence: \\'..table.concat(l, '\\'))
end
end
end)
--------------------------------------------------------------------------------
return SUITE