From f187329711b119174fa88b9cb5eac959f188758d Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Sun, 27 Aug 2017 12:05:46 +0200 Subject: [PATCH] Improved error checking, and fixed bug where strict tables would not report the proper error, when indexing --- errors.lua | 2 +- string_distance.lua | 11 +++++------ test/test_errors.lua | 21 +++++++++++++++++---- test/test_string_distance.lua | 9 +++++++++ 4 files changed, 32 insertions(+), 11 deletions(-) diff --git a/errors.lua b/errors.lua index 57e0702..26ae176 100644 --- a/errors.lua +++ b/errors.lua @@ -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 diff --git a/string_distance.lua b/string_distance.lua index f9a90ea..f0bd122 100644 --- a/string_distance.lua +++ b/string_distance.lua @@ -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 diff --git a/test/test_errors.lua b/test/test_errors.lua index 69a508a..eb866a2 100644 --- a/test/test_errors.lua +++ b/test/test_errors.lua @@ -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() diff --git a/test/test_string_distance.lua b/test/test_string_distance.lua index 64645d4..14aad81 100644 --- a/test/test_string_distance.lua +++ b/test/test_string_distance.lua @@ -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) + --------------------------------------------------------------------------------