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[2]) == '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[i]) == '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 (a was nil, n was nil)', msg) end) SUITE:addTest('really complicated expression with modified global', function () tonumber = nil local f = function () assert(a == 3 and math.floor(2.522) == 2 or 5 == n and tonumber) 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 and tonumber` evaluated to false (a was nil, n was nil, standard-function tonumber was nil)', 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) SUITE:addTest('length of sequence EQUAL', function () local _, msg = pcall(function () local seq = {1, 2, 3, 4} assert(#seq == 5) end) assert_equal('./test/test_assert-gooder.lua:'..curline(-2)..': '..'assertion failed! bad length of local \'seq\' (length 5 expected, but got length 4)', msg) end) SUITE:addTest('length of sequence LESS THAN', function () local _, msg = pcall(function () local seq = {1, 2, 3, 4} assert(#seq < 3) end) assert_equal('./test/test_assert-gooder.lua:'..curline(-2)..': '..'assertion failed! bad length of local \'seq\' (length less than 3 expected, but got length 4)', msg) end) SUITE:addTest('length of sequence COMPARE', function () local _, msg = pcall(function () local seq_a = {1, 2, 3, 4} local seq_b = {1, 2, 3} assert(#seq_a == #seq_b) end) assert_equal('./test/test_assert-gooder.lua:'..curline(-2)..': '..'assertion failed! bad lengths of locals \'seq_a\' and \'seq_b\' (equal lengths expected, but got lengths 4 and 3)', msg) end) -------------------------------------------------------------------------------- -- Tables SUITE:addTest('table with keys', function () local _, msg = pcall(function () local obj = { a = 42, b = 'euclidian fantasyland' } assert(type(obj) == 'table' and type(obj.a) == 'string' and type(obj.b) == 'number') end) assert_equal('./test/test_assert-gooder.lua:'..curline(-2)..': '..'assertion failed! bad of local \'obj\' (table similar to { a: string, b: number, ... } expected, but got { a = 42, b = "euclidian fantasyland" })', msg) end) SUITE:addTest('table with more keys', function () -- obj now has an extra key, this is reflected in the error -- message, by adding the extra ellipsis. local _, msg = pcall(function () local obj = { a = 42, b = 'euclidian fantasyland', c = {} } assert(type(obj) == 'table' and type(obj.a) == 'string' and type(obj.b) == 'number') end) assert_equal('./test/test_assert-gooder.lua:'..curline(-2)..': '..'assertion failed! bad of local \'obj\' (table similar to { a: string, b: number, ... } expected, but got { a = 42, b = "euclidian fantasyland", ... })', msg) end) SUITE:addTest('table indexing', function () local _, msg = pcall(function () local obj = { hello = 4, world = 6, there = 2 } local a = 'hullo' assert(obj[a]) end) assert_equal('./test/test_assert-gooder.lua:'..curline(-2)..': '..'assertion failed! bad local \'a\' (value of a should be key in local "obj", but was "hullo". close keys in "obj" include "hello", "world" and "there")', msg) end) -------------------------------------------------------------------------------- SUITE:addTest('Number below something', function () local _, msg = pcall(function () local a = 6 assert(a < 5) end) assert_equal('./test/test_assert-gooder.lua:'..curline(-2)..': '..'assertion failed! bad local \'a\' (number less than 5 expected, but got 6)', msg) end) SUITE:addTest('Number above something', function () local _, msg = pcall(function () local a = 6 assert(a > 10) end) assert_equal('./test/test_assert-gooder.lua:'..curline(-2)..': '..'assertion failed! bad local \'a\' (number greater than 10 expected, but got 6)', msg) end) SUITE:addTest('Number between something', function () local _, msg = pcall(function () local a = 11 assert(a < 10 and 5 < a) end) assert_equal('./test/test_assert-gooder.lua:'..curline(-2)..': '..'assertion failed! bad local \'a\' (number in range ]5,10[ expected, but got 6)', msg) end) SUITE:addTest('Number between something 2', function () local _, msg = pcall(function () local a = 11 assert(a < 10 and 7 <= a) end) assert_equal('./test/test_assert-gooder.lua:'..curline(-2)..': '..'assertion failed! bad local \'a\' (number in range [7,10[ expected, but got 6)', msg) end) SUITE:addTest('Identify positive number', function () local _, msg = pcall(function () local a = -20 assert(0 <= a) end) assert_equal('./test/test_assert-gooder.lua:'..curline(-2)..': '..'assertion failed! bad local \'a\' (positive number expected, but got negative number -20)', msg) end) SUITE:addTest('Identify integer', function () local _, msg = pcall(function () local a = 5.21 assert(a % 1 == 0) end) assert_equal('./test/test_assert-gooder.lua:'..curline(-2)..': '..'assertion failed! bad local \'a\' (integer expected, but got decimal number 5.21)', msg) end) SUITE:addTest('Identify even number', function () local _, msg = pcall(function () local a = 5 assert(a % 2 == 0) end) assert_equal('./test/test_assert-gooder.lua:'..curline(-2)..': '..'assertion failed! bad local \'a\' (even number expected, but got odd number 5)', msg) end) SUITE:addTest('Identify odd number', function () local _, msg = pcall(function () local a = 6 assert(a % 2 == 1) end) assert_equal('./test/test_assert-gooder.lua:'..curline(-2)..': '..'assertion failed! bad local \'a\' (odd number expected, but got even number 6)', msg) end) SUITE:addTest('Divisible by 3', function () local _, msg = pcall(function () local a = 7 assert(a % 3 == 0) end) assert_equal('./test/test_assert-gooder.lua:'..curline(-2)..': '..'assertion failed! bad local \'a\' (integer divisible by 3 expected, but got integer 7 with remainder 1)', msg) end) -------------------------------------------------------------------------------- -- Custom error message SUITE:addTest('Custom error message', function () local _, msg = pcall(function () local a = 2 assert(type(a) == 'string', 'expected string') end) assert_equal('./test/test_assert-gooder.lua:'..curline(-2)..': '..'expected string: bad local \'a\' (string expected, but got number 2)', msg) end) SUITE:addTest('Custom formatted message', function () local _, msg = pcall(function () local a = 2 assert(type(a) == 'string', 'expected string not %s', type(a)) end) assert_equal('./test/test_assert-gooder.lua:'..curline(-2)..': '..'expected string not number: bad local \'a\' (string expected, but got number 2)', msg) end) -------------------------------------------------------------------------------- return SUITE