1
0
errors/test/test_errors.lua

72 lines
2.7 KiB
Lua
Raw Normal View History

local error_handler = require('errors').ErrorHandler 'errors_test'
local SUITE = require('TestSuite').new('errors')
SUITE:setEnviroment {
error_handler = error_handler,
external_error = error_handler:getErrorFunc 'external',
internal_error = error_handler:getErrorFunc 'internal',
}
--------------------------------------------------------------------------------
-- Basic errors functionallity
SUITE:addTest('errors basic functionallity', function()
local func_a = function () external_error 'Hello World' end
local func_b, func_b_line = function () func_a() end, curline()
error_handler:register(func_a)
local status, error_msg = pcall(func_b)
assert_equal(false, status)
local expected_error = './test/test_errors.lua:'..func_b_line..': [errors_test]: Hello World'
assert_equal(expected_error, error_msg)
end)
SUITE:addTest('errors deep nesting works', function()
local func_a = function () external_error 'Hello World' end
local func_b, func_b_line = function () func_a() end, curline()
local func_c, func_c_line = function () func_b() end, curline()
error_handler:register(func_a)
error_handler:register(func_b)
local status, error_msg = pcall(func_c)
assert_equal(false, status)
local expected_error = './test/test_errors.lua:'..func_c_line..': [errors_test]: Hello World'
assert_equal(expected_error, error_msg)
end)
SUITE:addTest('internal errors also works', function()
local func_a, func_a_line = function () internal_error 'Hello World' end, curline()
local func_b, func_b_line = function () func_a() end, curline()
local func_c, func_c_line = function () func_b() end, curline()
error_handler:register(func_a)
error_handler:register(func_b)
local status, error_msg = pcall(func_c)
assert_equal(false, status)
local expected_error = './test/test_errors.lua:'..func_a_line..': [errors_test/internal]: Hello World'
assert_equal(expected_error, error_msg)
end)
SUITE:addTest('errors modes can be overwritten', function()
local extra_error = error_handler:getErrorFunc('external', 'extra')
local func, func_line = function () extra_error 'Hi' end, curline()
local status, error_msg = pcall(func)
local expected_error = './test/test_errors.lua:'..func_line..': [errors_test/extra]: Hi'
assert_equal(expected_error, error_msg)
end)
SUITE:addTest('errors modes can be overwritten, including internal', function()
local extra_error = error_handler:getErrorFunc('internal', 'extra')
local func, func_line = function () extra_error 'Hi' end, curline()
local status, error_msg = pcall(func)
local expected_error = './test/test_errors.lua:'..func_line..': [errors_test/extra]: Hi'
assert_equal(expected_error, error_msg)
end)
--------------------------------------------------------------------------------
return SUITE