2017-06-09 13:22:25 +00:00
2017-06-27 11:50:27 +00:00
local SUITE = require ' TestSuite ' ' errors '
2017-06-09 13:22:25 +00:00
SUITE : setEnviroment {
2017-06-27 11:50:27 +00:00
error = require ' errors ' ' test_errors '
2017-06-09 13:22:25 +00:00
}
--------------------------------------------------------------------------------
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 ( 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 )
2017-06-09 13:22:25 +00:00
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 ( 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 )
2017-06-09 13:22:25 +00:00
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 ( error.internal , error , ' André the Giant ' ) ) )
2017-06-09 13:22:25 +00:00
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 ( func_a ) ) )
2017-06-09 13:22:25 +00:00
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 ( ) 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 ) ) )
2017-06-09 13:22:25 +00:00
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-09 13:22:25 +00:00
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 ( func_b ) ) )
2017-06-09 13:22:25 +00:00
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 ( 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 )
2017-06-09 13:22:25 +00:00
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 )
2017-06-09 13:22:25 +00:00
end )
--------------------------------------------------------------------------------
return SUITE