Skip to content

Commit

Permalink
Lua API: Ensure unit test assertions specify a message string
Browse files Browse the repository at this point in the history
  • Loading branch information
CelticMinstrel committed Jul 28, 2021
1 parent dfb52a8 commit 18e4a64
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
25 changes: 25 additions & 0 deletions data/lua/core/unit_test.lua
Expand Up @@ -43,6 +43,31 @@ if rawget(_G, 'unit_test') ~= nil then
function unit_test.log(prefix, message)
std_print(prefix .. ': ' .. message)
end

-- This is a way to ensure that all assertions contain a descriptive message
setmetatable(unit_test, {
__newindex = function(self, key, val)
std_print('adding unit_test.' .. key)
if string.sub(key, 1, 6) == 'assert' then
local info = debug.getinfo(val, 'u')
local recursion_guard = false
local underlying_fcn = val
local fcn = function(...)
if not recursion_guard then
-- Last argument is the message
local message = select(info.nparams, ...)
recursion_guard = true
std_print('verifying assert message', message)
unit_test.assert_not_equal(message, nil, string.format('unit_test.%s missing a message', key))
recursion_guard = false
end
underlying_fcn(...)
end
val = fcn
end
rawset(self, key, val)
end
})

--! Fail the test with a message unless the condition is true
function unit_test.assert(condition, message)
Expand Down
30 changes: 15 additions & 15 deletions data/test/scenarios/as_text.cfg
Expand Up @@ -26,26 +26,26 @@

[lua]
code = <<
unit_test.assert_equal(wesnoth.as_text("a"), '"a"')
unit_test.assert_equal(wesnoth.as_text(1), "1")
unit_test.assert_equal(wesnoth.as_text(true), "true")
unit_test.assert_equal(wesnoth.as_text({ "a", "b", "c" }), '{"1":"a","2":"b","3":"c"}')
unit_test.assert_equal(wesnoth.as_text("a"), '"a"', 'convert string to string')
unit_test.assert_equal(wesnoth.as_text(1), "1", 'convert number to string')
unit_test.assert_equal(wesnoth.as_text(true), "true", 'convert boolean to string')
unit_test.assert_equal(wesnoth.as_text({ "a", "b", "c" }), '{"1":"a","2":"b","3":"c"}', 'convert array to string')

-- associative table iteration order not defined and can vary between runs even when the data remains identical
local tab_txt = wesnoth.as_text({ a = 1, b = false, c = "d" })
unit_test.assert_contains(tab_txt, '"a":1')
unit_test.assert_contains(tab_txt, '"b":false')
unit_test.assert_contains(tab_txt, '"c":"d"')
unit_test.assert_contains(tab_txt, '"a":1', 'convert table to string: first key')
unit_test.assert_contains(tab_txt, '"b":false', 'convert table to string: second key')
unit_test.assert_contains(tab_txt, '"c":"d"', 'convert table to string: third key')

local wml_tab_txt = wesnoth.as_text(wml.variables["var"])
unit_test.assert_contains(wml_tab_txt, '{"1":{"1":"one","2":{"1":{"1":"first","2":{')
unit_test.assert_contains(wml_tab_txt, '"a":1')
unit_test.assert_contains(wml_tab_txt, '"b":5')
unit_test.assert_contains(wml_tab_txt, '"c":true')
unit_test.assert_contains(wml_tab_txt, ',"2":{"1":"two","2":{"1":{"1":"second","2":{')
unit_test.assert_contains(wml_tab_txt, '"x":9')
unit_test.assert_contains(wml_tab_txt, '"y":3')
unit_test.assert_contains(wml_tab_txt, '"z":false')
unit_test.assert_contains(wml_tab_txt, '{"1":{"1":"one","2":{"1":{"1":"first","2":{', 'convert wml table to string: first tag opener')
unit_test.assert_contains(wml_tab_txt, '"a":1', 'convert wml table to string: first tag first key')
unit_test.assert_contains(wml_tab_txt, '"b":5', 'convert wml table to string: first tag second key')
unit_test.assert_contains(wml_tab_txt, '"c":true', 'convert wml table to string: first tag third key')
unit_test.assert_contains(wml_tab_txt, ',"2":{"1":"two","2":{"1":{"1":"second","2":{', 'convert wml table to string: second tag opener')
unit_test.assert_contains(wml_tab_txt, '"x":9', 'convert wml table to string: second tag first key')
unit_test.assert_contains(wml_tab_txt, '"y":3', 'convert wml table to string: second tag second key')
unit_test.assert_contains(wml_tab_txt, '"z":false', 'convert wml table to string: second tag third key')

-- Pass the test. Doesn't do anything if any of the above assertions has failed.
unit_test.succeed()
Expand Down

0 comments on commit 18e4a64

Please sign in to comment.