1
0

Using the new TestSuite subtest stuff.

This commit is contained in:
Jon Michael Aanes 2017-10-21 12:53:32 +02:00
parent b62c4c1c6c
commit d0b83f7f0d
2 changed files with 104 additions and 32 deletions

View File

@ -11,22 +11,24 @@ if not loadstring then loadstring = load end -- Lua 5.3
--
local function format_test (t)
SUITE:addTest(t.name or t.expect, function ()
assert_equal(t.expect, format(t.input, t.options))
end, { line = debug.getinfo(2).currentline })
if not t.not_idempotent then
-- Additional parsable test
SUITE:addTest((t.name or t.expect) .. ' (parsable?)', function ()
local chunk, error_msg = loadstring('return '..format(t.input, t.options))
if not chunk then error(error_msg) end
end, { line = debug.getinfo(2).currentline })
-- Additional idempotent test
SUITE:addTest((t.name or t.expect) .. ' (idempotent?)', function ()
local output = loadstring('return '..format(t.input, t.options))()
SUITE:addTest(t.name or t.expect, function (mode)
local fmt = format(t.input, t.options)
--
assert_equal(t.expect, fmt)
if mode == 'format' then return end
--
local chunk, load_error_msg = loadstring('return '..fmt)
if not chunk then error(load_error_msg) end
if mode == 'parse' then return end
--
local output = chunk()
assert_equal(t.input, output)
end, { line = debug.getinfo(2).currentline })
end
end, { line = debug.getinfo(2).currentline, data = {
'format',
not t.not_idempotent and 'parse' or nil,
not t.not_idempotent and 'idempotent' or nil,
}})
end
--------------------------------------------------------------------------------
@ -241,11 +243,19 @@ format_test {
expect = '\'abc\\255def\'',
}
SUITE:addTest('UTF8 Resilience Test', function ()
for _, str in ipairs(EXAMPLES.STRING_UTF8) do
assert_equal(str, loadstring('return '..format(str))() )
--------------------------------------------------------------------------------
do
local data = {}
for str, name in SUITE:getExamples 'STRING_UTF8' do
data[#data+1] = { name, str }
end
end)
SUITE:addTest('UTF8 Idempotency', function (_, str)
assert_equal(str, loadstring('return '..format(str))() )
end, { data = data })
end
--------------------------------------------------------------------------------

View File

@ -87,15 +87,12 @@ 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.
do return error 'Test skipped' end
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
local pjk_path = '/tmp/test_pjk_'..os.time()
@ -136,7 +133,15 @@ SUITE:addTest('Proper malformed utf8 escaping (through LÖVE)', function ()
f:write''
f:close()
for i, str in ipairs(EXAMPLES.STRING_UTF8) do
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
-- Create project and main.lua
local f = io.open(pjk_path..'/main.lua', 'w')
@ -148,11 +153,68 @@ SUITE:addTest('Proper malformed utf8 escaping (through LÖVE)', function ()
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
error(('Could not escape the string "%s" properly: %s\n\tByte-sequence: \\%s'):format(name, str, table.concat(l, '\\')))
end
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.
end)
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
--------------------------------------------------------------------------------
return SUITE