1
0

Improved error checking, and fixed bug where strict tables would not report the proper error, when indexing

This commit is contained in:
Jon Michael Aanes 2017-08-27 12:05:46 +02:00
parent 9d94601f83
commit f187329711
4 changed files with 32 additions and 11 deletions

View File

@ -145,7 +145,7 @@ return function (module_name)
return setmetatable(args[1] or {}, {
__index = function (t, k)
local keys = {}
for k in pairs(t) do keys[#keys+1] = k end
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)
end

View File

@ -42,9 +42,8 @@ local function levenshtein (str1, str2)
-- Adapted from the C version given at: https://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Levenshtein_distance
-- Error handeling.
assert(type(str1) == 'string')
assert(type(str2) == 'string')
if type(str1) ~= 'string' then error(('[errors/internal]: Bad argument #1 to levenshtein, expected string, got %s (%s)'):format(str1, type(str1))) end
if type(str2) ~= 'string' then error(('[errors/internal]: Bad argument #1 to levenshtein, expected string, got %s (%s)'):format(str2, type(str2))) end
-- Do work
local str1, str2 = str1:lower(), str2:lower()
@ -90,8 +89,8 @@ local function longest_common_subsequence (str1, str2)
-- Higher numbers denote more similar strings.
-- Error handeling.
assert(type(str1) == 'string')
assert(type(str2) == 'string')
if type(str1) ~= 'string' then error(('[errors/internal]: Bad argument #1 to longest_common_subsequence, expected string, got %s (%s)'):format(str1, type(str1))) end
if type(str2) ~= 'string' then error(('[errors/internal]: Bad argument #1 to longest_common_subsequence, expected string, got %s (%s)'):format(str2, type(str2))) end
-- Do work
local str1, str2 = str1:lower(), str2:lower()
@ -178,7 +177,7 @@ local function strings_with_highest_similarity (str, list_of_other_str)
if type(str) ~= 'string' then error(('[errors/internal]: Bad argument #1, expected string, got %s (%s)'):format(str, type(str))) end
if type(list_of_other_str) ~= 'table' then error(('[errors/internal]: Bad argument #2, expected table, got %s (%s)'):format(list_of_other_str, type(list_of_other_str))) end
for i = 1, #list_of_other_str do
if type(list_of_other_str[i]) ~= 'string' then error(('[errors/internal]: Bad argument #2, expected sequence of strings, but got %s (%s) on index %i'):format(list_of_other_str[i], type(list_of_other_str[i])), i) end
if type(list_of_other_str[i]) ~= 'string' then error(('[errors/internal]: Bad argument #2, expected sequence of strings, but got %s (%s) on index %i'):format(list_of_other_str[i], type(list_of_other_str[i]), i)) end
end
-- Do work

View File

@ -97,14 +97,27 @@ end)
SUITE:addTest('Strict tables can be accessed normally', function()
local t = error.strict_table { a = 1, b = 2, c = 3 }
assert(t.a, 1)
assert(t.b, 2)
assert(t.c, 3)
assert_equal(t.a, 1)
assert_equal(t.b, 2)
assert_equal(t.c, 3)
end)
SUITE:addTest('Strict tables with number keys works as well', function()
local t = error.strict_table { 'hi', 'hello', 'world' }
assert_equal(t[1], 'hi')
assert_equal(t[2], 'hello')
assert_equal(t[3], 'world')
end)
SUITE:addTest('Strict tables throw errors on unknown indices', function()
local t = error.strict_table { a = 1, b = 2, c = 3 }
bad_call(function() return t.hello end)
print(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)
end)
SUITE:addTest('Strict tables throw specific error on empty table', function()

View File

@ -115,6 +115,15 @@ SUITE:addTest('strings_with_highest_similarity example 1', function()
assert_equal( 'Allan Turing', output[1] )
end)
SUITE:addTest('strings_with_highest_similarity not defined on non-strings', function()
local strings = { 'Ada Lovelace', 'Charles Babbage ', 'Allan Turing', 'Grace Hopper' }
bad_call(strings_with_highest_similarity, 132, strings)
local strings = { 'Ada Lovelace', 'Charles Babbage ', 'Allan Turing', 4, 'Grace Hopper' }
bad_call(strings_with_highest_similarity, 'turning', strings)
local strings = { 'Ada Lovelace', 'Charles Babbage ', 'Allan Turing', {}, 'Grace Hopper' }
bad_call(strings_with_highest_similarity, 'turning', strings)
end)
--------------------------------------------------------------------------------