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
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2017-07-17 11:36:56 +00:00
|
|
|
local SUITE = require 'TestSuite' 'resilience'
|
2016-12-30 11:03:14 +00:00
|
|
|
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 ()
|
2017-07-25 15:49:06 +00:00
|
|
|
local error_msg = bad_call(pretty, 'Hello World', { indent = 51 })
|
2017-06-09 14:20:33 +00:00
|
|
|
assert(error_msg)
|
|
|
|
end)
|
|
|
|
|
2017-05-25 20:53:02 +00:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Strange standard libs
|
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
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2017-10-21 10:53:32 +00:00
|
|
|
if false then
|
|
|
|
-- This test takes a long time.
|
|
|
|
local data = {}
|
|
|
|
for str, name in SUITE:getExamples 'STRING_UTF8_MALFORMED' do
|
|
|
|
data[#data+1] = {str, name}
|
|
|
|
end
|
2017-08-07 08:39:45 +00:00
|
|
|
|
2017-07-31 14:29:56 +00:00
|
|
|
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()
|
|
|
|
|
2017-10-21 10:53:32 +00:00
|
|
|
SUITE:addTest('Proper malformed utf8 escaping (through LÖVE)', function (str, name)
|
|
|
|
-- 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 robust unicode checking, and will throw an error if
|
|
|
|
-- the characters is not escaped properly.
|
|
|
|
|
|
|
|
-- The input strings are gotten from TestSuite's example strings.
|
|
|
|
|
|
|
|
--do return error 'Test skipped' end
|
2017-07-31 14:29:56 +00:00
|
|
|
|
|
|
|
-- 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
|
2017-10-21 10:53:32 +00:00
|
|
|
error(('Could not escape the string "%s" properly: %s\n\tByte-sequence: \\%s'):format(name, str, table.concat(l, '\\')))
|
2017-07-31 14:29:56 +00:00
|
|
|
end
|
2017-10-21 10:53:32 +00:00
|
|
|
end, { data = data })
|
|
|
|
end
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Strange metatable
|
|
|
|
|
|
|
|
SUITE:addTest('Metatable: Error on %s operator', function (op)
|
|
|
|
pretty( setmetatable( { a = 2, b = 4, 1, 2, 3 } , {['__'..op] = function() error 'This should be unreachable!' end}) )
|
|
|
|
assert(true)
|
|
|
|
end, { data = {
|
|
|
|
{'add'}, {'sub'}, {'mul'}, {'div'}, {'mod'},{'pow'}, {'unm'}, {'concat'},
|
|
|
|
{'len'}, {'eq'}, {'lt'}, {'le'}, {'index'}, {'newindex'}, {'call'} }
|
|
|
|
})
|
|
|
|
|
|
|
|
SUITE:addTest('Metatable: # returns bogus length', function ()
|
|
|
|
assert_equal('{ 1, 2, 3, 4 }', pretty( setmetatable({ 1, 2, 3, 4 }, {__len = function() return 2 end})))
|
|
|
|
end)
|
|
|
|
|
|
|
|
SUITE:addTest('Metatable: object is never equal to anything, not even itself', function ()
|
|
|
|
assert_equal('{ 1, 2, 3, 4 }', pretty( setmetatable({ 1, 2, 3, 4 }, {__eq = function() return false end})))
|
|
|
|
end)
|
|
|
|
|
|
|
|
SUITE:addTest('Metatable: object contains nothing, but has prototype', function ()
|
|
|
|
pretty( setmetatable({}, {__index = { a = 2, b = 5, c = 2 }}) )
|
|
|
|
assert(true)
|
|
|
|
-- TODO: Determine what to output do when this happens.
|
2017-07-31 14:29:56 +00:00
|
|
|
end)
|
|
|
|
|
2017-10-21 10:53:32 +00:00
|
|
|
SUITE:addTest('Metatable: object contains every odd number', function ()
|
|
|
|
pretty( setmetatable({}, {__index = function(t, i) return i*2-1 end}) )
|
|
|
|
assert(true)
|
|
|
|
-- TODO: Determine what to output do when this happens.
|
|
|
|
end)
|
|
|
|
|
|
|
|
SUITE:addTest('Metatable: object is also callable', function ()
|
|
|
|
pretty( setmetatable({}, {__call = function(t, i) return i*2-1 end}) )
|
|
|
|
assert(true)
|
|
|
|
-- TODO: Determine what to output do when this happens.
|
|
|
|
end)
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Metatable __tostring
|
|
|
|
|
|
|
|
|
|
|
|
do
|
|
|
|
local data = {}
|
|
|
|
for str, name in SUITE:getExamples 'TABLE_METAMETHODS' do
|
|
|
|
data[#data+1] = { name, str }
|
|
|
|
end
|
|
|
|
|
|
|
|
SUITE:addTest('Metatable shenanigans', function (_, str)
|
|
|
|
pretty(str)
|
|
|
|
assert(true)
|
|
|
|
end, { data = data })
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- TODO: Tests for alternative modes (__mode = 'k', __mode = 'v', __mode = 'kv')
|
|
|
|
-- TODO: Tests for __metatable
|
|
|
|
|
2017-07-31 14:29:56 +00:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2016-12-30 11:03:14 +00:00
|
|
|
return SUITE
|