2017-10-28 10:25:10 +00:00
local SUITE = require ' TestSuite ' ' assert-gooder '
SUITE : setEnvironment {
assert = require ' assert-gooder '
}
--------------------------------------------------------------------------------
SUITE : addTest ( ' local variable ' , function ( )
local _ , msg = pcall ( function ( )
local a = 2
assert ( type ( a ) == ' string ' )
end )
assert_equal ( ' ./test/test_assert-gooder.lua: ' .. curline ( - 2 ) .. ' : ' .. ' assertion failed! bad local \' a \' (string expected, but got number: 2) ' , msg )
end )
SUITE : addTest ( ' upvalue variable ' , function ( )
local a = 2
local _ , msg = pcall ( function ( )
assert ( type ( a ) == ' string ' )
end )
assert_equal ( ' ./test/test_assert-gooder.lua: ' .. curline ( - 2 ) .. ' : ' .. ' assertion failed! bad upvalue \' a \' (string expected, but got number: 2) ' , msg )
end )
SUITE : addTest ( ' global variable ' , function ( )
a = 2
local _ , msg = pcall ( function ( )
assert ( type ( a ) == ' string ' )
end )
assert_equal ( ' ./test/test_assert-gooder.lua: ' .. curline ( - 2 ) .. ' : ' .. ' assertion failed! bad global \' a \' (string expected, but got number: 2) ' , msg )
end )
SUITE : addTest ( ' argument to anon function ' , function ( )
2017-10-29 09:16:16 +00:00
local _ , msg = pcall ( function ( )
( function ( a )
assert ( type ( a ) == ' string ' )
end ) ( 2 )
end )
assert_equal ( ' ./test/test_assert-gooder.lua: ' .. curline ( - 3 ) .. ' : ' .. ' assertion failed! bad argument #1 \' a \' to the anonymous function at ./test/test_assert-gooder.lua: ' .. curline ( - 4 ) .. ' (string expected, but got number: 2) ' , msg )
2017-10-28 10:25:10 +00:00
end )
SUITE : addTest ( ' argument to named function ' , function ( )
local f = function ( a )
assert ( type ( a ) == ' string ' )
end
local _ , msg = pcall ( function ( ) f ( 2 ) end )
assert_equal ( ' ./test/test_assert-gooder.lua: ' .. curline ( - 3 ) .. ' : ' .. ' assertion failed! bad argument #1 \' a \' to \' f \' (string expected, but got number: 2) ' , msg )
end )
2017-10-28 11:15:57 +00:00
--------------------------------------------------------------------------------
SUITE : addTest ( ' can improve asserts in loaded strings too ' , function ( )
local func = loadstring " return function(a) assert(type(a) == 'string') end " ( )
local _ , msg = pcall ( setfenv ( func , getfenv ( ) ) , 42 )
2017-10-29 09:16:16 +00:00
assert_equal ( ' [string "return function(a) assert(type(a) == \' string \' ..."]:1: ' .. ' assertion failed! bad argument #1 \' a \' to the anonymous function from loaded string (string expected, but got number: 42) ' , msg )
2017-10-28 11:15:57 +00:00
end )
SUITE : addTest ( ' really complicated expression ' , function ( )
local f = function ( )
assert ( a == 3 and math.floor ( 2.522 ) == 2 or 5 == n )
end
local _ , msg = pcall ( f , 2 )
assert_equal ( ' ./test/test_assert-gooder.lua: ' .. curline ( - 3 ) .. ' : ' .. ' assertion failed! expression `a == 3 and math.floor(2.522) == 2 or 5 == n` evaluated to false ' , msg )
end )
2017-10-28 10:25:10 +00:00
--------------------------------------------------------------------------------
-- Other assert types
SUITE : addTest ( ' compare values ' , function ( )
local _ , msg = pcall ( function ( )
local a = 2
assert ( a == 4 )
end )
2017-10-28 11:15:57 +00:00
assert_equal ( ' ./test/test_assert-gooder.lua: ' .. curline ( - 2 ) .. ' : ' .. ' assertion failed! bad local \' a \' (number 4 expected, but got: 2) ' , msg )
2017-10-28 10:25:10 +00:00
end )
SUITE : addTest ( ' compare values across types ' , function ( )
local _ , msg = pcall ( function ( )
local a = " hi "
assert ( a == 4 )
end )
2017-10-28 11:15:57 +00:00
assert_equal ( ' ./test/test_assert-gooder.lua: ' .. curline ( - 2 ) .. ' : ' .. ' assertion failed! bad local \' a \' (number 4 expected, but got string: "hi") ' , msg )
2017-10-28 10:25:10 +00:00
end )
2017-10-28 11:15:57 +00:00
SUITE : addTest ( ' is nil ' , function ( )
local _ , msg = pcall ( function ( )
local a = " hi "
assert ( a == nil )
end )
assert_equal ( ' ./test/test_assert-gooder.lua: ' .. curline ( - 2 ) .. ' : ' .. ' assertion failed! bad local \' a \' (nil expected, but got string: "hi") ' , msg )
2017-10-28 10:25:10 +00:00
end )
2017-10-28 11:15:57 +00:00
SUITE : addTest ( ' truthy ' , function ( )
local _ , msg = pcall ( function ( )
local a = false
assert ( a )
end )
assert_equal ( ' ./test/test_assert-gooder.lua: ' .. curline ( - 2 ) .. ' : ' .. ' assertion failed! bad local \' a \' (truthy expected, but got false) ' , msg )
2017-10-28 10:25:10 +00:00
end )
2017-10-28 11:15:57 +00:00
SUITE : addTest ( ' not equal ' , function ( constant_value , msg_in_pars )
local func = loadstring ( ( " return function() local a = %s; assert(a ~= %s) end " ) : format ( constant_value , constant_value ) ) ( )
local _ , msg = pcall ( setfenv ( func , getfenv ( ) ) )
assert_equal ( ' [string "return function() ..."]:1: assertion failed! bad local \' a \' ( ' .. msg_in_pars .. ' ) ' , msg )
end , { data = {
{ ' true ' , ' expected anything other than true, but got true ' } ,
{ ' nil ' , ' expected anything other than nil, but got nil ' } ,
{ ' "hi" ' , ' expected anything other than string "hi", but got "hi" ' } ,
{ ' 42 ' , ' expected anything other than number 42, but got 42 ' } ,
} } )
2017-10-29 09:16:16 +00:00
SUITE : addTest ( ' constant false ' , function ( )
local function f ( )
assert ( false )
end
local _ , msg = pcall ( function ( ) f ( ) end )
assert_equal ( ' ./test/test_assert-gooder.lua: ' .. curline ( - 3 ) .. ' : ' .. ' assertion failed! this assert will always fail, as it \' s body is `false`. assumingly this should be an unreachable part of \' f \' ' , msg )
end )
SUITE : addTest ( ' constant nil ' , function ( )
local _ , msg = pcall ( function ( )
assert ( nil )
end )
assert_equal ( ' ./test/test_assert-gooder.lua: ' .. curline ( - 2 ) .. ' : ' .. ' assertion failed! this assert will always fail, as it \' s body is `nil`. assumingly this should be an unreachable part of the anonymous function at ./test/test_assert-gooder.lua: ' .. curline ( - 3 ) , msg )
end )
2017-10-28 10:25:10 +00:00
--------------------------------------------------------------------------------
return SUITE