1
0
errors/test/test_errors.lua

183 lines
8.0 KiB
Lua
Raw Normal View History

2017-06-27 11:50:27 +00:00
local SUITE = require 'TestSuite' 'errors'
SUITE:setEnviroment {
2017-06-27 11:50:27 +00:00
error = require 'errors' 'test_errors'
}
--------------------------------------------------------------------------------
2017-06-27 11:50:27 +00:00
-- 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(function() error 'Hello World' end)))
2017-06-27 11:50:27 +00:00
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(function() error('%s to the %s', 'Welcome', 'Errors') end)))
2017-06-27 11:50:27 +00:00
end)
2017-06-27 11:50:27 +00:00
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(function() error() end)))
2017-06-27 11:50:27 +00:00
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(function() error.internal 'World Hello' end)))
2017-06-27 11:50:27 +00:00
end)
2017-06-27 11:50:27 +00:00
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(function() error:internal 'André the Giant' end)))
end)
2017-06-27 11:50:27 +00:00
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(function() func_a() end)))
end)
2017-06-27 11:50:27 +00:00
SUITE:addTest('Internal includes the stack 2', function()
local func_a = function() error.internal 'Thomas the Steam Train' end
local func_b = function() pcall(func_a) end
2017-06-27 11:50:27 +00:00
local expected_error = './test/test_errors.lua:'..curline(-2)..': [test_errors/internal]: Thomas the Steam Train'
assert_equal(expected_error, select(2, pcall(function() func_a() end)))
end)
2017-06-27 11:50:27 +00:00
--------------------------------------------------------------------------------
-- 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)
2017-06-27 11:50:27 +00:00
local expected_error = './test/test_errors.lua:'..curline(-3)..': [test_errors]: Hello World'
assert_equal(expected_error, select(2, pcall(function() func_b() end)))
end)
2017-06-27 11:50:27 +00:00
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(function() func_c() end)))
2017-06-27 11:50:27 +00:00
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(function() func_c() end)))
2017-06-27 11:50:27 +00:00
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)
2017-06-27 11:50:27 +00:00
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]: Cannot 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 error 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]: Cannot index string hello, for sequence with length 4.', msg)
end)
SUITE:addTest('Strict tables throws error 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]: Cannot index integer 0, for sequence with length 4.', msg)
end)
SUITE:addTest('Strict tables throws error 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]: Cannot index integer 7, for sequence with length 4.', msg)
end)
SUITE:addTest('Strict tables throws error when indexing non-integer in sequence', function()
local t = error.strict_table { 'turing', 'church', 'ada', 'hopper' }
local msg = bad_call(function() return t[2.5] end)
assert_equal('./test/test_errors.lua:'..curline(-1)..': [test_errors]: Cannot index non-integer 2.5, for sequence with length 4.', msg)
end)
--------------------------------------------------------------------------------
SUITE:addTest('Strict global bad key', function()
local glob = { require = require, print = print }
glob._G = glob
local f = setfenv(function()
print(_G)
local error = require 'errors' 'test_strict_global'
error.enable_strict_globals()
local a = some_global
end, glob)
local _, msg = pcall(f)
assert_equal('./test/test_errors.lua:'..curline(-3)..': [test_strict_global]: Attempting to access unknown global: \'some_global\'. Perhaps you meant _G, require or print?', msg)
end)
SUITE:addTest('Strict global bad insert', function()
local glob = { require = require }
glob._G = glob
local f = setfenv(function()
local error = require 'errors' 'test_strict_global'
error.enable_strict_globals()
some_global = "hello"
end, glob)
local _, msg = pcall(f)
assert_equal('./test/test_errors.lua:'..curline(-3)..': [test_strict_global]: Attempting to create new global \'some_global\' with value: hello. Perhaps you misspelled _G or require?', msg)
end)
--------------------------------------------------------------------------------
return SUITE