1
0

Now ignores additional arguments when parsing, but those arguments are used to format the error message.

This commit is contained in:
Jon Michael Aanes 2018-01-29 10:28:30 +01:00
parent 1b0b6d14d4
commit d9be06658d
2 changed files with 62 additions and 8 deletions

View File

@ -154,7 +154,7 @@ local function get_module_filetext (module_filepath)
-- Just attempt standard file open -- Just attempt standard file open
local filehandle = io.open(module_filepath, 'r') local filehandle = io.open(module_filepath, 'r')
if filehandle then if filehandle then
filetext = filehandle:read '*all' local filetext = filehandle:read '*all'
filehandle:close() filehandle:close()
return filetext return filetext
end end
@ -167,6 +167,26 @@ local function get_module_filetext (module_filepath)
return nil return nil
end end
local function seperate_by_toplevel_commas (text)
assert(type(text) == 'string')
local section_start, index, sections = 1, 1, {}
while index < #text do
local next_comma = text:find(',', index)
local next_par_start, next_par_end = text:find('%b()', index)
if not next_comma then
break
elseif not next_par_start or next_comma < next_par_start then
sections[#sections+1] = text:sub(section_start, next_comma - 1)
index = next_comma + 1
section_start = index
else
index = next_par_end + 1
end
end
sections[#sections+1] = text:sub(section_start)
return sections
end
local function get_assert_body_text (call_info) local function get_assert_body_text (call_info)
if call_info.what == 'Lua' or call_info.what == 'main' then if call_info.what == 'Lua' or call_info.what == 'main' then
-- Find filetext -- Find filetext
@ -190,7 +210,9 @@ local function get_assert_body_text (call_info)
end end
end end
-- Find body exclusively. -- Find body exclusively.
return table.concat(lines_after, '\n'):match('assert%s*(%b())'):sub(2, -2) local assert_arguments_text = table.concat(lines_after, '\n'):match('assert%s*(%b())'):sub(2, -2)
local assert_arguments = seperate_by_toplevel_commas(assert_arguments_text)
return assert_arguments[1]
end end
error 'Not implemented yet!' error 'Not implemented yet!'
@ -365,7 +387,7 @@ local function determine_error_message (call_info, msg, condition)
end end
end end
return function (condition) return function (condition, format, ...)
if condition then return condition end if condition then return condition end
-- --
local level = 2 local level = 2
@ -383,15 +405,26 @@ return function (condition)
if not success then if not success then
io.stderr:write(('[assert-gooder/internal]: Internal error occured while determining error message for calling assert:\n %s\n'):format(internal_error_msg)) io.stderr:write(('[assert-gooder/internal]: Internal error occured while determining error message for calling assert:\n %s\n'):format(internal_error_msg))
end end
--
-- Format error message:
assert(#msg_container <= 2 and type(msg_container[1]) == 'string') assert(#msg_container <= 2 and type(msg_container[1]) == 'string')
local l = {'assertion failed! ', msg_container[1]} local l = {}
if format ~= nil then
l[#l+1] = (type(format) == 'string') and format:format(...) or tostring(format)
l[#l+1] = ':'
else
l[#l+1] = 'assertion failed!'
end
l[#l+1] = ' '
l[#l+1] = msg_container[1]
if msg_container[2] then if msg_container[2] then
assert(type(msg_container[2]) == 'string') assert(type(msg_container[2]) == 'string')
l[3] = ' (' l[#l+1] = ' ('
l[4] = msg_container[2] l[#l+1] = msg_container[2]
l[5] = ')' l[#l+1] = ')'
end end
-- Throw error message
error(table.concat(l, ''), 2) error(table.concat(l, ''), 2)
end end

View File

@ -303,6 +303,27 @@ SUITE:addTest('Identify odd number', function ()
assert_equal('./test/test_assert-gooder.lua:'..curline(-2)..': '..'assertion failed! bad local \'a\' (odd number expected, but got even number 5.21)', msg) assert_equal('./test/test_assert-gooder.lua:'..curline(-2)..': '..'assertion failed! bad local \'a\' (odd number expected, but got even number 5.21)', msg)
end) end)
--------------------------------------------------------------------------------
-- Custom error message
SUITE:addTest('Custom error message', function ()
local _, msg = pcall(function ()
local a = 2
assert(type(a) == 'string', 'expected string')
end)
assert_equal('./test/test_assert-gooder.lua:'..curline(-2)..': '..'expected string: bad local \'a\' (string expected, but got number 2)', msg)
end)
SUITE:addTest('Custom formatted message', function ()
local _, msg = pcall(function ()
local a = 2
assert(type(a) == 'string', 'expected string not %s', type(a))
end)
assert_equal('./test/test_assert-gooder.lua:'..curline(-2)..': '..'expected string not number: bad local \'a\' (string expected, but got number 2)', msg)
end)
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------