151 lines
6.4 KiB
Lua
151 lines
6.4 KiB
Lua
|
|
local SUITE = require 'TestSuite' 'errors'
|
|
SUITE:setEnviroment {
|
|
error = require 'errors' 'test_errors'
|
|
}
|
|
|
|
--------------------------------------------------------------------------------
|
|
-- Basic errors functions
|
|
|
|
SUITE:addTest('Basic error', function()
|
|
local expected_error = './test/test_errors.lua:'..curline(1)..': [test_errors]: Hello World'
|
|
assert_equal(expected_error, select(2, pcall(error, 'Hello World')))
|
|
end)
|
|
|
|
SUITE:addTest('Basic formatting', function()
|
|
local expected_error = './test/test_errors.lua:'..curline(1)..': [test_errors]: Welcome to the Errors'
|
|
assert_equal(expected_error, select(2, pcall(error, '%s to the %s', 'Welcome', 'Errors')))
|
|
end)
|
|
|
|
SUITE:addTest('Default generic error', function()
|
|
local expected_error = './test/test_errors.lua:'..curline(1)..': [test_errors]: Unknown error occured'
|
|
assert_equal(expected_error, select(2, pcall(error)))
|
|
end)
|
|
|
|
SUITE:addTest('Internal is almost the same as error in itself', function()
|
|
local expected_error = './test/test_errors.lua:'..curline(1)..': [test_errors/internal]: World Hello'
|
|
assert_equal(expected_error, select(2, pcall(error.internal, 'World Hello')))
|
|
end)
|
|
|
|
SUITE:addTest('Internal can also be called using object index', function()
|
|
local expected_error = './test/test_errors.lua:'..curline(1)..': [test_errors/internal]: André the Giant'
|
|
assert_equal(expected_error, select(2, pcall(error.internal, error, 'André the Giant')))
|
|
end)
|
|
|
|
SUITE:addTest('Internal includes the stack', function()
|
|
local func_a = function() error.internal 'Thomas the Steam Train' end
|
|
local expected_error = './test/test_errors.lua:'..curline(-1)..': [test_errors/internal]: Thomas the Steam Train'
|
|
assert_equal(expected_error, select(2, pcall(func_a)))
|
|
end)
|
|
|
|
SUITE:addTest('Internal includes the stack 2', function()
|
|
local func_a = function() error.internal 'Thomas the Steam Train' end
|
|
local func_b = function() return pcall(func_a) end
|
|
local expected_error = './test/test_errors.lua:'..curline(-2)..': [test_errors/internal]: Thomas the Steam Train'
|
|
assert_equal(expected_error, select(2, pcall(func_a)))
|
|
end)
|
|
|
|
--------------------------------------------------------------------------------
|
|
-- External
|
|
|
|
SUITE:addTest('External errors hides the stack', function()
|
|
local func_a = function () error.external 'Hello World' end
|
|
local func_b = function () func_a() end
|
|
error.register(func_a)
|
|
|
|
local expected_error = './test/test_errors.lua:'..curline(-3)..': [test_errors]: Hello World'
|
|
assert_equal(expected_error, select(2, pcall(func_b)))
|
|
end)
|
|
|
|
SUITE:addTest('External errors can be triggered deep', function()
|
|
local func_a = function () error.external 'Hello World' end
|
|
local func_b = function () func_a() end
|
|
local func_c = function () func_b() end
|
|
error.register(func_a, func_b)
|
|
|
|
local expected_error = './test/test_errors.lua:'..curline(-3)..': [test_errors]: Hello World'
|
|
assert_equal(expected_error, select(2, pcall(func_c)))
|
|
end)
|
|
|
|
SUITE:addTest('Internal errors don\'t hide the stack', function()
|
|
local func_a = function () error.internal 'Hello World' end
|
|
local func_b = function () func_a() end
|
|
local func_c = function () func_b() end
|
|
error.register(func_a, func_b)
|
|
|
|
local expected_error = './test/test_errors.lua:'..curline(-5)..': [test_errors/internal]: Hello World'
|
|
assert_equal(expected_error, select(2, pcall(func_c)))
|
|
end)
|
|
|
|
--------------------------------------------------------------------------------
|
|
-- Correcting errors
|
|
|
|
SUITE:addTest('Correct errors helps with autocorrection', function()
|
|
local _, err_msg = pcall(error.correct, 'Not %s, maybe: %s', 'help', {'Help', 'whelp', 'halp'})
|
|
local expected_error = './test/test_errors.lua:'..curline(-1)..': [test_errors]: Not help, maybe: Help, whelp or halp'
|
|
assert_equal(expected_error, err_msg)
|
|
end)
|
|
|
|
SUITE:addTest('Correct errors has nice default string', function()
|
|
local _, err_msg = pcall(error.correct, 'Vlard', {'Vlad', 'Question Mark', 'Frankenstein'})
|
|
local expected_error = './test/test_errors.lua:'..curline(-1)..': [test_errors]: Unexpected input "Vlard", maybe you meant Vlad, Question Mark or Frankenstein?'
|
|
assert_equal(expected_error, err_msg)
|
|
end)
|
|
|
|
--------------------------------------------------------------------------------
|
|
-- Strict table
|
|
|
|
SUITE:addTest('Strict tables can be accessed normally', function()
|
|
local t = error.strict_table { a = 1, b = 2, 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)
|
|
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)
|
|
end)
|
|
|
|
SUITE:addTest('Strict tables throw specific error on empty table', function()
|
|
local t = error.strict_table {}
|
|
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)
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
return SUITE
|