Added unique error messages when indexing into a sequence.
This commit is contained in:
parent
f187329711
commit
accffabc26
35
errors.lua
35
errors.lua
|
@ -96,6 +96,34 @@ local function correct_error (self, module_suffix, format_msg, gotten_string, po
|
|||
return error(('[%s%s]: '..format_msg):format(self.module_name, module_suffix, gotten_string, list_string), 3)
|
||||
end
|
||||
|
||||
----
|
||||
|
||||
local function handle_index_error (err_hdl, t, key)
|
||||
--
|
||||
assert(type(err_hdl) == 'table')
|
||||
assert(type(t) == 'table')
|
||||
-- Find keys
|
||||
local keys = {}
|
||||
for k in pairs(t) do keys[#keys+1] = k end
|
||||
-- Is empty table?
|
||||
if #keys == 0 then
|
||||
external_error(err_hdl, '', 'Attempting to index unknown key %s in an empty table.', key)
|
||||
end
|
||||
-- Is sequence?
|
||||
if #keys == #t then
|
||||
if type(key) ~= 'number' then
|
||||
external_error(err_hdl, '', 'Attempting to index %s key %s, for sequence with length %i.', type(key), key, #t)
|
||||
elseif key == math.floor(key) then -- Integer
|
||||
external_error(err_hdl, '', 'Attempting to index position %i, for sequence with length %i.', key, #t)
|
||||
end
|
||||
end
|
||||
-- Is map?
|
||||
for i = 1, #keys do keys[i] = tostring(keys[i]) end
|
||||
correct_error(err_hdl, '', 'Attempting to index unknown key %s, maybe you meant %s?', tostring(key), keys)
|
||||
end
|
||||
|
||||
----
|
||||
|
||||
|
||||
local ErrorHandler_mt = {__call = function (self, ...) internal_error(self, '', ...) end}
|
||||
|
||||
|
@ -144,13 +172,12 @@ return function (module_name)
|
|||
if args[1] == err_hdl then table.remove(args, 1) end
|
||||
return setmetatable(args[1] or {}, {
|
||||
__index = function (t, k)
|
||||
local keys = {}
|
||||
for k in pairs(t) do keys[#keys+1] = tostring(k) end
|
||||
if #keys == 0 then external_error(err_hdl, '', 'Attempting to index unknown key %s in an empty table.', k) end
|
||||
correct_error(err_hdl, '', 'Attempting to index unknown key %s, maybe you meant %s?', k, keys)
|
||||
return handle_index_error(err_hdl, t, k)
|
||||
end
|
||||
})
|
||||
end
|
||||
|
||||
err_hdl.register(format_probable_strings, internal_error, external_error, correct_error, handle_index_error, err_hdl.internal, err_hdl.external, err_hdl.correct, err_hdl.register, err_hdl.strict_table)
|
||||
|
||||
return err_hdl
|
||||
end
|
||||
|
|
|
@ -111,13 +111,13 @@ end)
|
|||
|
||||
SUITE:addTest('Strict tables throw errors on unknown indices', function()
|
||||
local t = error.strict_table { a = 1, b = 2, c = 3 }
|
||||
print(bad_call(function() return t.hello end))
|
||||
bad_call(function() return t.hello end)
|
||||
end)
|
||||
|
||||
SUITE:addTest('Strict tables throw errors on unknown indices, even if table contains number keys', function()
|
||||
local t = error.strict_table { 'hi', 'hello', 'world' }
|
||||
local msg = bad_call(function() return t.hello end)
|
||||
assert_equal('[test_errors]: Attempting to index unknown key hello, maybe you meant 1, 2 or 3?', msg)
|
||||
local t = error.strict_table { 'hi', 'hello', 'world', aa = 1, ab = 2, ac = 3 }
|
||||
local msg = bad_call(function() return t.a end)
|
||||
assert_equal('[test_errors]: Attempting to index unknown key a, maybe you meant ac, aa or ab?', msg)
|
||||
end)
|
||||
|
||||
SUITE:addTest('Strict tables throw specific error on empty table', function()
|
||||
|
@ -125,6 +125,24 @@ SUITE:addTest('Strict tables throw specific error on empty table', function()
|
|||
bad_call(function() return t.hello end)
|
||||
end)
|
||||
|
||||
SUITE:addTest('Strict tables throws errors on indexing strings in sequence', function()
|
||||
local t = error.strict_table { 'turing', 'church', 'ada', 'hopper' }
|
||||
local msg = bad_call(function() return t.hello end)
|
||||
assert_equal('./test/test_errors.lua:'..curline(-1)..': [test_errors]: Attempting to index string key hello, for sequence with length 4.', msg)
|
||||
end)
|
||||
|
||||
SUITE:addTest('Strict tables throws errors on sequence under-access', function()
|
||||
local t = error.strict_table { 'turing', 'church', 'ada', 'hopper' }
|
||||
local msg = bad_call(function() return t[0] end)
|
||||
assert_equal('./test/test_errors.lua:'..curline(-1)..': [test_errors]: Attempting to index position 0, for sequence with length 4.', msg)
|
||||
end)
|
||||
|
||||
SUITE:addTest('Strict tables throws errors on sequence over-access', function()
|
||||
local t = error.strict_table { 'turing', 'church', 'ada', 'hopper' }
|
||||
local msg = bad_call(function() return t[7] end)
|
||||
assert_equal('./test/test_errors.lua:'..curline(-1)..': [test_errors]: Attempting to index position 7, for sequence with length 4.', msg)
|
||||
end)
|
||||
|
||||
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
|
Loading…
Reference in New Issue
Block a user