From edcc217ae4a55b4df70bf9fafedf5cc69c6df45f Mon Sep 17 00:00:00 2001 From: Celtic Minstrel Date: Tue, 23 Feb 2021 00:35:18 -0500 Subject: [PATCH] Update [test_condition] to be a little more verbose and make use of newer features --- data/lua/wml/test_condition.lua | 47 +++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/data/lua/wml/test_condition.lua b/data/lua/wml/test_condition.lua index efc10363df211..55764afa15036 100644 --- a/data/lua/wml/test_condition.lua +++ b/data/lua/wml/test_condition.lua @@ -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") - 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 + explanation_args.result = "failed" 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