1
0
errors/test/test_errors.lua

120 lines
4.8 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(t.a, 1)
assert(t.b, 2)
assert(t.c, 3)
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 specific error on empty table', function()
local t = error.strict_table {}
bad_call(function() return t.hello end)
end)
--------------------------------------------------------------------------------
return SUITE