test: Improve layout assert output

This sohuld now properly position widgets based on their x,y values.
Haven't tested overlap, yet, but it should be "last one wins".
This commit is contained in:
Lucas Schwiderski 2022-03-15 23:02:42 +01:00
parent 3f58ba882e
commit aab8cea46e
No known key found for this signature in database
GPG Key ID: AA12679AAA6DF4D8
1 changed files with 99 additions and 29 deletions

View File

@ -67,6 +67,8 @@ local function draw_layout_comparison(actual, expected)
} }
local left, right = {}, {} local left, right = {}, {}
local too_small = false
local too_big = false
local function wrap_line(width, x, line) local function wrap_line(width, x, line)
-- Two columns occupied by the `|` characters -- Two columns occupied by the `|` characters
@ -81,60 +83,127 @@ local function draw_layout_comparison(actual, expected)
end end
local function draw_widget(widget) local function draw_widget(widget)
local height = widget._height -- We need a minimum size to render the data in.
local width = widget._width -- One line/column on all sides for the boundary.
local x = widget._matrix.x0 -- Three lines, 12 columns for the text.
-- Two lines removed for the horizontal lines, local min_size = { 14, 5 }
-- three for the lines with text
local fixed = 5
local buffer_top = math.floor((height - fixed) / 2)
local buffer_bottom = height - fixed - buffer_top
local widget_lines = {times("-", width)} if widget._width < min_size[1] or widget._height < min_size[2] then
for _ = 1, buffer_top do too_small = true
table.insert(widget_lines, wrap_line(width, x, " ")) elseif widget._width > 25 or widget._height > 25 then
too_big = true
end end
table.insert(widget_lines, wrap_line(width, x, widget._name or "Widget")) local width = math.max(min_size[1], widget._width)
table.insert(widget_lines, wrap_line(width, x, string.format( local height = math.max(min_size[2], widget._height)
"Size %d,%d", local x = widget._matrix.x0
widget._width, widget._height local y = widget._matrix.y0
)))
table.insert(widget_lines, wrap_line(width, x, string.format( local buffer_top = math.floor(height / 2)
"Pos %d,%d", local buffer_bottom = height - buffer_top
widget._matrix.x0, widget._matrix.y0
))) local line_start = x + 1
local lines_index = y + 1
local widget_lines = {}
widget_lines[lines_index] = pad_string(times("-", width), " ", line_start)
for _ = 1, buffer_top do
lines_index = lines_index + 1
widget_lines[lines_index] = pad_string(
wrap_line(width, x, " "),
" ",
line_start
)
end
lines_index = lines_index + 1
widget_lines[lines_index] = pad_string(
wrap_line(width, x, widget._name or "Widget"),
" ",
line_start
)
lines_index = lines_index + 1
widget_lines[lines_index] = pad_string(
wrap_line(width, x, string.format(
"Size %d,%d",
widget._width, widget._height
)),
" ",
line_start
)
lines_index = lines_index + 1
widget_lines[lines_index] = pad_string(
wrap_line(width, x, string.format(
"Pos %d,%d",
widget._matrix.x0, widget._matrix.y0
)),
" ",
line_start
)
for _ = 1, buffer_bottom do for _ = 1, buffer_bottom do
table.insert(widget_lines, wrap_line(width, x, " ")) lines_index = lines_index + 1
widget_lines[lines_index] = pad_string(
wrap_line(width, x, " "),
" ",
line_start
)
end end
table.insert(widget_lines, times("-", width)) lines_index = lines_index + 1
widget_lines[lines_index] = pad_string(times("-", width), " ", line_start)
return widget_lines return widget_lines, lines_index
end end
local max_len = 0 local max_len = 0
local left_len = 0
local right_len = 0
for _, widget in ipairs(actual) do for _, widget in ipairs(actual) do
for _, line in ipairs(draw_widget(widget)) do local widget_lines, last_index = draw_widget(widget)
left_len = math.max(left_len, last_index)
for i, line in pairs(widget_lines) do
max_len = math.max(max_len, #line) max_len = math.max(max_len, #line)
table.insert(left, line) left[i] = line
end end
end end
for _, widget in ipairs(expected) do for _, widget in ipairs(expected) do
for _, line in ipairs(draw_widget(widget)) do local widget_lines, last_index = draw_widget(widget)
table.insert(right, line) right_len = math.max(right_len, last_index)
for i, line in pairs(widget_lines) do
right_len = right_len + 1
right[i] = line
end end
end end
if too_small then
table.insert(lines,
"!!! Widget size is too small for proper drawing. Please " ..
"adjust the test case to use widgets with a dimension " ..
"of at least (14, 5)\n"
)
end
if too_big then
table.insert(lines,
"!!! Widget size is too big for proper drawing. Please " ..
"adjust the test case to keep widgets below a dimension " ..
"of (25, 25)\n"
)
end
table.insert( table.insert(
lines, lines,
pad_string("Actual:", " ", max_len, true) .. " Expected:" pad_string("Actual:", " ", max_len, true) .. " Expected:"
) )
for i = 1, math.max(#left, #right) do for i = 1, math.max(left_len, right_len) do
table.insert( table.insert(
lines, lines,
pad_string(left[i] or "", " ", max_len, true) .. " " .. (right[i] or "") pad_string(left[i] or "", " ", max_len, true) .. " " .. (right[i] or "")
@ -145,7 +214,8 @@ local function draw_layout_comparison(actual, expected)
-- up, we want to force `say`'s trailing quote into a new line. -- up, we want to force `say`'s trailing quote into a new line.
table.insert(lines, "") table.insert(lines, "")
return table.concat(lines, "\n") local rendered = table.concat(lines, "\n")
return rendered:gsub("%s*$", "")
end end
local function widget_layout(state, arguments) local function widget_layout(state, arguments)