test: Fix layout assert with large strings

Apparently `string.format` has a limitation on the length of the padding
modifier.

Signed-off-by: Lucas Schwiderski <lucas@lschwiderski.de>
This commit is contained in:
Lucas Schwiderski 2022-01-08 00:23:19 +01:00
parent 392dbc21ab
commit c21a4cf34e
No known key found for this signature in database
GPG Key ID: AA12679AAA6DF4D8
1 changed files with 30 additions and 13 deletions

View File

@ -39,6 +39,28 @@ end
say:set("assertion.widget_fit.positive", "Offering (%s, %s) to widget and expected (%s, %s), but got (%s, %s)")
assert:register("assertion", "widget_fit", widget_fit, "assertion.widget_fit.positive", "assertion.widget_fit.positive")
local function times(char, len)
local chars = {}
for _ = 1, len do
table.insert(chars, char)
end
return table.concat(chars, "")
end
--- Pad the `str` with `len` amount of characters `char`.
-- When `is_after == true`, the padding will be appended.
-- This allows exceeding the limit of 99 in `string.format`.
local function pad_string(str, char, len, is_after)
len = len - #str
local pad = times(char, len)
if is_after == true then
return str .. pad
else
return pad .. str
end
end
local function draw_layout_comparison(actual, expected)
local lines = {
"Widget layout does not match:\n",
@ -46,14 +68,6 @@ local function draw_layout_comparison(actual, expected)
local left, right = {}, {}
local function times(char, len)
local chars = {}
for _ = 1, len do
table.insert(chars, char)
end
return table.concat(chars, "")
end
local function wrap_line(width, x, line)
-- Two columns occupied by the `|` characters
local buffer = width - 2 - #line
@ -115,13 +129,16 @@ local function draw_layout_comparison(actual, expected)
end
end
table.insert(lines, string.format(
"%-"..max_len.."s %-"..max_len.."s",
"Actual:", "Expected:"
))
table.insert(
lines,
pad_string("Actual:", " ", max_len, true) .. " Expected:"
)
for i = 1, math.max(#left, #right) do
table.insert(lines, string.format("%-"..max_len.."s %s", left[i] or "", right[i] or ""))
table.insert(
lines,
pad_string(left[i] or "", " ", max_len, true) .. " " .. (right[i] or "")
)
end
-- Force one additional newline. Due to how the error message has to be set