2017-06-09 13:22:25 +00:00
2017-06-27 11:50:27 +00:00
local SUITE = require ' TestSuite ' ' errors '
2024-07-10 17:13:55 +00:00
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 '
2018-01-10 16:39:34 +00:00
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 '
2018-01-10 16:39:34 +00:00
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-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 '
2018-01-10 16:39:34 +00:00
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 '
2018-01-10 16:39:34 +00:00
assert_equal ( expected_error , select ( 2 , pcall ( function ( ) error.internal ' World Hello ' end ) ) )
2017-06-27 11:50:27 +00:00
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 '
2018-01-10 16:39:34 +00:00
assert_equal ( expected_error , select ( 2 , pcall ( function ( ) error : internal ' André the Giant ' end ) ) )
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 '
2018-01-10 16:39:34 +00:00
assert_equal ( expected_error , select ( 2 , pcall ( function ( ) func_a ( ) end ) ) )
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
2018-01-10 16:39:34 +00:00
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 '
2018-01-10 16:39:34 +00:00
assert_equal ( expected_error , select ( 2 , pcall ( function ( ) func_a ( ) end ) ) )
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 '
2018-01-10 16:39:34 +00:00
assert_equal ( expected_error , select ( 2 , pcall ( function ( ) func_b ( ) end ) ) )
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 '
2018-01-10 16:39:34 +00:00
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 '
2018-01-10 16:39:34 +00:00
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-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 )
2017-07-10 08:14:46 +00:00
--------------------------------------------------------------------------------
-- Strict table
SUITE : addTest ( ' Strict tables can be accessed normally ' , function ( )
local t = error.strict_table { a = 1 , b = 2 , c = 3 }
2017-08-27 10:05:46 +00:00
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 ' )
2017-07-10 08:14:46 +00:00
end )
SUITE : addTest ( ' Strict tables throw errors on unknown indices ' , function ( )
local t = error.strict_table { a = 1 , b = 2 , c = 3 }
2017-09-01 11:01:39 +00:00
bad_call ( function ( ) return t.hello end )
2017-08-27 10:05:46 +00:00
end )
SUITE : addTest ( ' Strict tables throw errors on unknown indices, even if table contains number keys ' , function ( )
2017-09-01 11:01:39 +00:00
local t = error.strict_table { ' hi ' , ' hello ' , ' world ' , aa = 1 , ab = 2 , ac = 3 }
local msg = bad_call ( function ( ) return t.a end )
2017-09-01 11:11:29 +00:00
assert_equal ( ' [test_errors]: Cannot index unknown key a, maybe you meant ac, aa or ab? ' , msg )
2017-07-10 08:14:46 +00:00
end )
2017-07-10 08:51:07 +00:00
SUITE : addTest ( ' Strict tables throw specific error on empty table ' , function ( )
local t = error.strict_table { }
bad_call ( function ( ) return t.hello end )
end )
2017-09-01 11:11:29 +00:00
SUITE : addTest ( ' Strict tables throws error on indexing strings in sequence ' , function ( )
2017-09-01 11:01:39 +00:00
local t = error.strict_table { ' turing ' , ' church ' , ' ada ' , ' hopper ' }
local msg = bad_call ( function ( ) return t.hello end )
2017-09-01 11:11:29 +00:00
assert_equal ( ' ./test/test_errors.lua: ' .. curline ( - 1 ) .. ' : [test_errors]: Cannot index string hello, for sequence with length 4. ' , msg )
2017-09-01 11:01:39 +00:00
end )
2017-09-01 11:11:29 +00:00
SUITE : addTest ( ' Strict tables throws error on sequence under-access ' , function ( )
2017-09-01 11:01:39 +00:00
local t = error.strict_table { ' turing ' , ' church ' , ' ada ' , ' hopper ' }
local msg = bad_call ( function ( ) return t [ 0 ] end )
2017-09-01 11:11:29 +00:00
assert_equal ( ' ./test/test_errors.lua: ' .. curline ( - 1 ) .. ' : [test_errors]: Cannot index integer 0, for sequence with length 4. ' , msg )
2017-09-01 11:01:39 +00:00
end )
2017-09-01 11:11:29 +00:00
SUITE : addTest ( ' Strict tables throws error on sequence over-access ' , function ( )
2017-09-01 11:01:39 +00:00
local t = error.strict_table { ' turing ' , ' church ' , ' ada ' , ' hopper ' }
local msg = bad_call ( function ( ) return t [ 7 ] end )
2017-09-01 11:11:29 +00:00
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 )
2017-09-01 11:01:39 +00:00
end )
2018-01-10 16:39:34 +00:00
--------------------------------------------------------------------------------
2017-07-10 08:14:46 +00:00
2018-01-10 16:39:34 +00:00
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 )
2017-07-10 08:14:46 +00:00
2017-06-09 13:22:25 +00:00
--------------------------------------------------------------------------------
return SUITE
2018-01-10 16:39:34 +00:00