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() (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) 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) SUITE:addTest('indexing', function () local _, msg = pcall(function () local a = { b = 39 } assert(type(a.b) == 'string') end) assert_equal('./test/test_assert-gooder.lua:'..curline(-2)..': '..'assertion failed! bad key "b" in local \'a\' (string expected, but got number 39)', msg) end) SUITE:addTest('subscript constant', function () local _, msg = pcall(function () local a = { 4, 2, 3, 6 } assert(type(a.b) == 'string') end) assert_equal('./test/test_assert-gooder.lua:'..curline(-2)..': '..'assertion failed! bad key 2 in local \'a\' (string expected, but got number 2)', msg) end) SUITE:addTest('subscript variable', function () local _, msg = pcall(function () local a, i = { 4, 2, 3, 6 }, 2 assert(type(a.b) == 'string') end) assert_equal('./test/test_assert-gooder.lua:'..curline(-2)..': '..'assertion failed! bad key 2 in local \'a\' (string expected, but got number 2)', 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 the anonymous function from loaded string (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) -------------------------------------------------------------------------------- -- 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\' (number 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\' (number 4 expected, but got string "hi")', msg) end) 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) end) 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) end) SUITE:addTest('truthy indexing', function () local _, msg = pcall(function () local a = { b = false } assert(a.b) end) assert_equal('./test/test_assert-gooder.lua:'..curline(-2)..': '..'assertion failed! bad key "b" in local \'a\' (truthy expected, but got false)', msg) end) SUITE:addTest('truthy subscript', function () local _, msg = pcall(function () local a = {} assert(a[6]) end) assert_equal('./test/test_assert-gooder.lua:'..curline(-2)..': '..'assertion failed! bad key 6 in local \'a\' (truthy expected, but got nil)', msg) end) SUITE:addTest('truthy variable subscript', function () local _, msg = pcall(function () local a, i = {}, 3 assert(a[i]) end) assert_equal('./test/test_assert-gooder.lua:'..curline(-2)..': '..'assertion failed! bad key 3 in local \'a\' (truthy expected, but got nil)', msg) end) 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' }, }}) 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) SUITE:addTest('function as type argument', function () local f = function() end local _, msg = pcall(function () assert(type(f) == 'string') end) assert_equal('./test/test_assert-gooder.lua:'..curline(-2)..': '..'assertion failed! bad upvalue \'f\' (string expected, but got '..tostring(f)..')', msg) end) -------------------------------------------------------------------------------- return SUITE