test: make text-only snapshots work

This commit is contained in:
zeertzjq 2023-12-10 07:06:48 +08:00
parent c2249f6abf
commit 20bd03f166

View File

@ -259,12 +259,13 @@ local ext_keys = {
-- row. Last character of each row (typically "|") is stripped. -- row. Last character of each row (typically "|") is stripped.
-- Common indentation is stripped. -- Common indentation is stripped.
-- "{MATCH:x}" in a line is matched against Lua pattern `x`. -- "{MATCH:x}" in a line is matched against Lua pattern `x`.
-- "*count" at the end of a line means it repeats `count` times. -- "*n" at the end of a line means it repeats `n` times.
-- attr_ids: Expected text attributes. Screen rows are transformed according -- attr_ids: Expected text attributes. Screen rows are transformed according
-- to this table, as follows: each substring S composed of -- to this table, as follows: each substring S composed of
-- characters having the same attributes will be substituted by -- characters having the same attributes will be substituted by
-- "{K:S}", where K is a key in `attr_ids`. Any unexpected -- "{K:S}", where K is a key in `attr_ids`. Any unexpected
-- attributes in the final state are an error. -- attributes in the final state are an error.
-- Use an empty table for a text-only (no attributes) expectation.
-- Use screen:set_default_attr_ids() to define attributes for many -- Use screen:set_default_attr_ids() to define attributes for many
-- expect() calls. -- expect() calls.
-- extmarks: Expected win_extmarks accumulated for the grids. For each grid, -- extmarks: Expected win_extmarks accumulated for the grids. For each grid,
@ -344,8 +345,11 @@ function Screen:expect(expected, attr_ids, ...)
end end
end end
local attr_state = { local attr_state = {
ids = attr_ids or self._default_attr_ids, ids = attr_ids or self._default_attr_ids,
} }
if isempty(attr_ids) then
attr_state.ids = nil
end
if self._options.ext_linegrid then if self._options.ext_linegrid then
attr_state.id_to_index = self:linegrid_check_attrs(attr_state.ids or {}) attr_state.id_to_index = self:linegrid_check_attrs(attr_state.ids or {})
end end
@ -1252,7 +1256,7 @@ end
-- Generates tests. Call it where Screen:expect() would be. Waits briefly, then -- Generates tests. Call it where Screen:expect() would be. Waits briefly, then
-- dumps the current screen state in the form of Screen:expect(). -- dumps the current screen state in the form of Screen:expect().
-- Use snapshot_util({},true) 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(attrs, ignore, request_cb)
@ -1312,12 +1316,15 @@ function Screen:get_snapshot(attrs, ignore)
ignore = self._default_attr_ignore ignore = self._default_attr_ignore
end end
local attr_state = { local attr_state = {
ids = {}, ids = {},
ignore = ignore, ignore = ignore,
mutable = true, -- allow _row_repr to add missing highlights mutable = true, -- allow _row_repr to add missing highlights
} }
if attrs == nil then if attrs == nil then
attrs = self._default_attr_ids attrs = self._default_attr_ids
elseif isempty(attrs) then
attrs = nil
attr_state.ids = nil
else else
attr_state.modified = true attr_state.modified = true
end end
@ -1328,7 +1335,7 @@ function Screen:get_snapshot(attrs, ignore)
end end
end end
if self._options.ext_linegrid then if self._options.ext_linegrid then
attr_state.id_to_index = self:linegrid_check_attrs(attr_state.ids) attr_state.id_to_index = self:linegrid_check_attrs(attr_state.ids or {})
end end
local lines = self:render(true, attr_state, true) local lines = self:render(true, attr_state, true)
@ -1415,6 +1422,8 @@ function Screen:_print_snapshot(attrs, ignore)
table.insert(attrstrs, " "..keyval.." = "..dict..";") table.insert(attrstrs, " "..keyval.." = "..dict..";")
end end
attrstr = (", attr_ids={\n"..table.concat(attrstrs, "\n").."\n}") attrstr = (", attr_ids={\n"..table.concat(attrstrs, "\n").."\n}")
elseif isempty(attrs) then
attrstr = ', attr_ids={}'
end end
local result = 'screen:expect{grid=[[\n' .. kwargs.grid .. '\n]]' .. attrstr local result = 'screen:expect{grid=[[\n' .. kwargs.grid .. '\n]]' .. attrstr
@ -1430,6 +1439,7 @@ end
function Screen:print_snapshot(attrs, ignore) function Screen:print_snapshot(attrs, ignore)
print('\n' .. self:_print_snapshot(attrs, ignore) .. '\n') print('\n' .. self:_print_snapshot(attrs, ignore) .. '\n')
io.stdout:flush()
end end
function Screen:_insert_hl_id(attr_state, hl_id) function Screen:_insert_hl_id(attr_state, hl_id)