1
0

Adjusted phrasing of error messages, and added unique error message for when indexing non-integers in sequence.

This commit is contained in:
Jon Michael Aanes 2017-09-01 13:11:29 +02:00
parent accffabc26
commit ec63ade963
2 changed files with 20 additions and 11 deletions

View File

@ -107,19 +107,22 @@ local function handle_index_error (err_hdl, t, key)
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)
external_error(err_hdl, '', 'Cannot index unknown key %s in 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)
external_error(err_hdl, '', 'Cannot index %s %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)
external_error(err_hdl, '', 'Cannot index integer %i, for sequence with length %i.', key, #t)
elseif key ~= math.floor(key) then -- Non-Integer
local format_code = (key == tonumber(tostring(key))) and '%s' or '%f'
external_error(err_hdl, '', 'Cannot index non-integer '..format_code..', 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)
correct_error(err_hdl, '', 'Cannot index unknown key %s, maybe you meant %s?', tostring(key), keys)
end
----

View File

@ -117,7 +117,7 @@ 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', 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)
assert_equal('[test_errors]: Cannot 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,22 +125,28 @@ 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()
SUITE:addTest('Strict tables throws error 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)
assert_equal('./test/test_errors.lua:'..curline(-1)..': [test_errors]: Cannot index string hello, for sequence with length 4.', msg)
end)
SUITE:addTest('Strict tables throws errors on sequence under-access', function()
SUITE:addTest('Strict tables throws error 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)
assert_equal('./test/test_errors.lua:'..curline(-1)..': [test_errors]: Cannot index integer 0, for sequence with length 4.', msg)
end)
SUITE:addTest('Strict tables throws errors on sequence over-access', function()
SUITE:addTest('Strict tables throws error 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)
assert_equal('./test/test_errors.lua:'..curline(-1)..': [test_errors]: Cannot index integer 7, for sequence with length 4.', msg)
end)
SUITE:addTest('Strict tables throws error when indexing non-integer in sequence', function()
local t = error.strict_table { 'turing', 'church', 'ada', 'hopper' }
local msg = bad_call(function() return t[2.5] end)
assert_equal('./test/test_errors.lua:'..curline(-1)..': [test_errors]: Cannot index non-integer 2.5, for sequence with length 4.', msg)
end)