refactor(tests): update screen:snapshot_util() to use new-style highlights

This makes screen:snapshot_util() generate code with the new
screen:add_extra_attr_ids { ... } pattern. For convenience,
the old-style configuration is still detected and supported (until
all tests have been refactored, which is my goal for the 0.11 cycle)

Remove the last traces of the "ignore" attr anti-pattern. This code
is no longer functional, it is just "ignore" argument being passed around
like a hot potato at this point.
This commit is contained in:
bfredl 2024-05-25 10:20:10 +02:00
parent 52389e7243
commit a4b5549655

View File

@ -255,6 +255,7 @@ end
function Screen:set_default_attr_ids(attr_ids) function Screen:set_default_attr_ids(attr_ids)
self._default_attr_ids = attr_ids self._default_attr_ids = attr_ids
self._attrs_overridden = true
end end
function Screen:add_extra_attr_ids(extra_attr_ids) function Screen:add_extra_attr_ids(extra_attr_ids)
@ -699,9 +700,9 @@ screen:redraw_debug() to show all intermediate screen states.]]
end, expected) end, expected)
end end
function Screen:expect_unchanged(intermediate, waittime_ms, ignore_attrs) function Screen:expect_unchanged(intermediate, waittime_ms)
-- Collect the current screen state. -- Collect the current screen state.
local kwargs = self:get_snapshot(nil, ignore_attrs) local kwargs = self:get_snapshot()
if intermediate then if intermediate then
kwargs.intermediate = true kwargs.intermediate = true
@ -1536,13 +1537,14 @@ end
-- Use snapshot_util({}) to generate a text-only (no attributes) test. -- Use snapshot_util({}) to generate a text-only (no attributes) test.
-- --
-- @see Screen:redraw_debug() -- @see Screen:redraw_debug()
function Screen:snapshot_util(attrs, ignore, request_cb) function Screen:snapshot_util(request_cb)
-- TODO: simplify this later when existing tests have been updated
self:sleep(250, request_cb) self:sleep(250, request_cb)
self:print_snapshot(attrs, ignore) self:print_snapshot()
end end
function Screen:redraw_debug(attrs, ignore, timeout) function Screen:redraw_debug(timeout)
self:print_snapshot(attrs, ignore) self:print_snapshot()
local function notification_cb(method, args) local function notification_cb(method, args)
assert(method == 'redraw') assert(method == 'redraw')
for _, update in ipairs(args) do for _, update in ipairs(args) do
@ -1552,7 +1554,7 @@ function Screen:redraw_debug(attrs, ignore, timeout)
end end
end end
self:_redraw(args) self:_redraw(args)
self:print_snapshot(attrs, ignore) self:print_snapshot()
return true return true
end end
if timeout == nil then if timeout == nil then
@ -1596,23 +1598,12 @@ end
-- Returns the current screen state in the form of a screen:expect() -- Returns the current screen state in the form of a screen:expect()
-- keyword-args map. -- keyword-args map.
function Screen:get_snapshot(attrs, ignore) function Screen:get_snapshot()
if ignore == nil then
ignore = self._default_attr_ignore
end
local attr_state = { local attr_state = {
ids = {}, ids = {},
ignore = ignore,
mutable = true, -- allow _row_repr to add missing highlights mutable = true, -- allow _row_repr to add missing highlights
} }
if attrs == nil then local attrs = self._default_attr_ids
attrs = self._default_attr_ids
elseif isempty(attrs) then
attrs = nil
attr_state.ids = nil
else
attr_state.modified = true
end
if attrs ~= nil then if attrs ~= nil then
for i, a in pairs(attrs) do for i, a in pairs(attrs) do
@ -1708,9 +1699,10 @@ local function fmt_ext_state(name, state)
end end
end end
function Screen:_print_snapshot(attrs, ignore) function Screen:_print_snapshot()
local kwargs, ext_state, attr_state = self:get_snapshot(attrs, ignore) local kwargs, ext_state, attr_state = self:get_snapshot()
local attrstr = '' local attrstr = ''
local modify_attrs = not self._attrs_overridden
if attr_state.modified then if attr_state.modified then
local attrstrs = {} local attrstrs = {}
for i, a in pairs(attr_state.ids) do for i, a in pairs(attr_state.ids) do
@ -1721,16 +1713,20 @@ function Screen:_print_snapshot(attrs, ignore)
dict = '{ ' .. self:_pprint_attrs(a) .. ' }' dict = '{ ' .. self:_pprint_attrs(a) .. ' }'
end end
local keyval = (type(i) == 'number') and '[' .. tostring(i) .. ']' or i local keyval = (type(i) == 'number') and '[' .. tostring(i) .. ']' or i
table.insert(attrstrs, ' ' .. keyval .. ' = ' .. dict .. ',') if not (type(i) == 'number' and modify_attrs and i <= 30) then
table.insert(attrstrs, ' ' .. keyval .. ' = ' .. dict .. ',')
end
if modify_attrs then
self._default_attr_ids = attr_state.ids
end
end end
attrstr = (',\n attr_ids = {\n ' .. table.concat(attrstrs, '\n ') .. '\n },') local fn_name = modify_attrs and 'add_extra_attr_ids' or 'set_default_attr_ids'
elseif isempty(attrs) then attrstr = ('screen:' .. fn_name .. ' {\n' .. table.concat(attrstrs, '\n') .. '\n}\n\n')
attrstr = ',\n attr_ids = {},'
end end
local result = ('screen:expect({\n grid = [[\n %s\n ]]%s'):format( local result = ('%sscreen:expect({\n grid = [[\n %s\n ]]'):format(
kwargs.grid:gsub('\n', '\n '), attrstr,
attrstr kwargs.grid:gsub('\n', '\n ')
) )
for _, k in ipairs(ext_keys) do for _, k in ipairs(ext_keys) do
if ext_state[k] ~= nil and not (k == 'win_viewport' and not self.options.ext_multigrid) then if ext_state[k] ~= nil and not (k == 'win_viewport' and not self.options.ext_multigrid) then
@ -1742,8 +1738,8 @@ function Screen:_print_snapshot(attrs, ignore)
return result return result
end end
function Screen:print_snapshot(attrs, ignore) function Screen:print_snapshot()
print('\n' .. self:_print_snapshot(attrs, ignore) .. '\n') print('\n' .. self:_print_snapshot() .. '\n')
io.stdout:flush() io.stdout:flush()
end end