Update [test_condition] to be a little more verbose and make use of newer features

This commit is contained in:
Celtic Minstrel 2021-02-23 00:35:18 -05:00
parent ef39108f08
commit a97a04e2e9

View file

@ -1,3 +1,24 @@
local explanation_template = [[The following conditional test unexpectedly $result:
$literal
Interpolated to:
$interpolated
]]
local extra_templates = {
variable = function(cfg, args)
args.varname = cfg.name
args.actual = tostring(wml.variables[cfg.name])
return 'Note: The variable $varname currently has the value $actual.'
end,
}
local function stringize(tag, cfg, parse)
cfg = parse and wml.parsed(cfg) or wml.literal(cfg)
local str = wml.tostring{wml.tag[tag](cfg)}
return str:gsub('\n', '\n\t')
end
-- This function returns true if it managed to explain the failure
local function explain(current_cfg, expect, logger)
for i,t in ipairs(current_cfg) do
@ -15,27 +36,19 @@ local function explain(current_cfg, expect, logger)
-- We don't explain these ones.
return true
elseif wesnoth.eval_conditional{t} == expect then
local explanation = "The following conditional test %s:"
local explanation_args = {}
if expect then
explanation = explanation:format("passed")
explanation_args.result = "passed"
else
explanation = explanation:format("failed")
explanation_args.result = "failed"
end
explanation = string.format("%s\n\t[%s]", explanation, tag)
for k,v in pairs(this_cfg) do
if type(k) ~= "number" then
local format = "%s\n\t\t%s=%s"
local literal = tostring(wml.literal(this_cfg)[k])
if literal ~= v then
format = format .. "=%s"
end
explanation = string.format(format, explanation, k, literal, tostring(v))
end
end
explanation = string.format("%s\n\t[/%s]", explanation, tag)
if tag == "variable" then
explanation = string.format("%s\n\tNote: The variable %s currently has the value %q.", explanation, this_cfg.name, tostring(wml.variables[this_cfg.name]))
explanation_args.literal = stringize(tag, this_cfg, false)
explanation_args.interpolated = stringize(tag, this_cfg, true)
local explanation = explanation_template
if extra_templates[tag] then
explanation = explanation .. extra_templates[tag](this_cfg, explanation_args)
end
explanation = explanation:vformat(explanation_args)
wesnoth.log(logger, explanation, true)
return true
end