87 lines
3.2 KiB
Lua
87 lines
3.2 KiB
Lua
|
|
||
|
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 ()
|
||
|
local _, msg = pcall(function (a)
|
||
|
assert(type(a) == 'string')
|
||
|
end, 2)
|
||
|
assert_equal('./test/test_assert-gooder.lua:'..curline(-2)..': '..'assertion failed! bad argument #1 \'a\' to anonymous function (string expected, but got number: 2)', msg)
|
||
|
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)
|
||
|
|
||
|
--------------------------------------------------------------------------------
|
||
|
-- Other assert types
|
||
|
|
||
|
SUITE:addTest('compare values', function ()
|
||
|
local _, msg = pcall(function ()
|
||
|
local a = 2
|
||
|
assert(a == 4)
|
||
|
end)
|
||
|
assert_equal('./test/test_assert-gooder.lua:'..curline(-2)..': '..'assertion failed! bad local \'a\' (value of 4 expected, but got: 2)', msg)
|
||
|
end)
|
||
|
|
||
|
SUITE:addTest('compare values across types', function ()
|
||
|
local _, msg = pcall(function ()
|
||
|
local a = "hi"
|
||
|
assert(a == 4)
|
||
|
end)
|
||
|
assert_equal('./test/test_assert-gooder.lua:'..curline(-2)..': '..'assertion failed! bad local \'a\' (value of 4 expected, but got string: "hi")', msg)
|
||
|
end)
|
||
|
|
||
|
--------------------------------------------------------------------------------
|
||
|
|
||
|
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)
|
||
|
assert_equal('[string "return function(a) assert(type(a) == \'string\'..."]:1: '..'assertion failed! bad argument #1 \'a\' to anonymous function (string expected, but got number: 42)', msg)
|
||
|
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)
|
||
|
|
||
|
--------------------------------------------------------------------------------
|
||
|
|
||
|
return SUITE
|
||
|
|