Improved error checking, and fixed bug where strict tables would not report the proper error, when indexing
This commit is contained in:
parent
9d94601f83
commit
f187329711
|
@ -145,7 +145,7 @@ return function (module_name)
|
||||||
return setmetatable(args[1] or {}, {
|
return setmetatable(args[1] or {}, {
|
||||||
__index = function (t, k)
|
__index = function (t, k)
|
||||||
local keys = {}
|
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
|
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)
|
correct_error(err_hdl, '', 'Attempting to index unknown key %s, maybe you meant %s?', k, keys)
|
||||||
end
|
end
|
||||||
|
|
|
@ -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
|
-- Adapted from the C version given at: https://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Levenshtein_distance
|
||||||
|
|
||||||
-- Error handeling.
|
-- Error handeling.
|
||||||
assert(type(str1) == 'string')
|
if type(str1) ~= 'string' then error(('[errors/internal]: Bad argument #1 to levenshtein, expected string, got %s (%s)'):format(str1, type(str1))) end
|
||||||
assert(type(str2) == 'string')
|
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
|
-- Do work
|
||||||
local str1, str2 = str1:lower(), str2:lower()
|
local str1, str2 = str1:lower(), str2:lower()
|
||||||
|
@ -90,8 +89,8 @@ local function longest_common_subsequence (str1, str2)
|
||||||
-- Higher numbers denote more similar strings.
|
-- Higher numbers denote more similar strings.
|
||||||
|
|
||||||
-- Error handeling.
|
-- Error handeling.
|
||||||
assert(type(str1) == '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
|
||||||
assert(type(str2) == 'string')
|
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
|
-- Do work
|
||||||
local str1, str2 = str1:lower(), str2:lower()
|
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(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
|
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
|
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
|
end
|
||||||
|
|
||||||
-- Do work
|
-- Do work
|
||||||
|
|
|
@ -97,14 +97,27 @@ end)
|
||||||
|
|
||||||
SUITE:addTest('Strict tables can be accessed normally', function()
|
SUITE:addTest('Strict tables can be accessed normally', function()
|
||||||
local t = error.strict_table { a = 1, b = 2, c = 3 }
|
local t = error.strict_table { a = 1, b = 2, c = 3 }
|
||||||
assert(t.a, 1)
|
assert_equal(t.a, 1)
|
||||||
assert(t.b, 2)
|
assert_equal(t.b, 2)
|
||||||
assert(t.c, 3)
|
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)
|
end)
|
||||||
|
|
||||||
SUITE:addTest('Strict tables throw errors on unknown indices', function()
|
SUITE:addTest('Strict tables throw errors on unknown indices', function()
|
||||||
local t = error.strict_table { a = 1, b = 2, c = 3 }
|
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)
|
end)
|
||||||
|
|
||||||
SUITE:addTest('Strict tables throw specific error on empty table', function()
|
SUITE:addTest('Strict tables throw specific error on empty table', function()
|
||||||
|
|
|
@ -115,6 +115,15 @@ SUITE:addTest('strings_with_highest_similarity example 1', function()
|
||||||
assert_equal( 'Allan Turing', output[1] )
|
assert_equal( 'Allan Turing', output[1] )
|
||||||
end)
|
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)
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user