aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDanilo M. <danix@danix.xyz>2026-09-16 19:41:08 +0200
committerDanilo M. <danix@danix.xyz>2026-09-16 19:41:08 +0200
commit9c8947ab8dea5709ea83992c14e663214a571833 (patch)
tree23cfafef5f447fb32d0bcfb929f2a0a0eb11cdd1
parent9c9be25bdb0b2ec48e3078fe5e119b3d0f56631c (diff)
downloadconky-theme-udt-9c8947ab8dea5709ea83992c14e663214a571833.tar.gz
conky-theme-udt-9c8947ab8dea5709ea83992c14e663214a571833.zip
test: check grid maths on both real screen sizes
The grid's justification is that one layout table works on differently shaped screens, so assert it: every cell lands inside the margins at 2560x1080 and 1920x1080, a spanning cell absorbs the gap it covers, and no shipped layout entry falls outside the declared grid. dashboard.lua exports its internals for this; cairo and lib.card are stubbed via package.preload since they only exist inside Conky. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
-rw-r--r--dashboard.lua5
-rw-r--r--test/test_layout.lua60
2 files changed, 65 insertions, 0 deletions
diff --git a/dashboard.lua b/dashboard.lua
index b814be7..af6e51c 100644
--- a/dashboard.lua
+++ b/dashboard.lua
@@ -95,6 +95,11 @@ local function rect_for(entry, sw, sh)
}
end
+-- Exported for test/test_layout.lua. The grid's whole claim is that one layout
+-- table works on differently shaped screens, which is worth checking.
+conky_dashboard_internal = { rect_for = rect_for, COLS = COLS, ROWS = ROWS,
+ GAP = GAP, MARGIN = MARGIN, layout = layout }
+
-- Widget modules, loaded once and cached. A widget that fails to load must not
-- take the frame down with it, so the require is wrapped.
local widgets = {}
diff --git a/test/test_layout.lua b/test/test_layout.lua
new file mode 100644
index 0000000..f3f47e3
--- /dev/null
+++ b/test/test_layout.lua
@@ -0,0 +1,60 @@
+-- Grid maths check.
+-- Run from the repo root: lua test/test_layout.lua
+--
+-- dashboard.lua requires cairo, which only exists inside Conky, so stub the
+-- pieces it touches at load time before requiring it.
+package.path = './?.lua;' .. package.path
+package.preload['cairo'] = function() return {} end
+package.preload['lib.card'] = function() return {} end
+conky_window = nil
+function conky_parse(s) return '#000000' end
+-- dashboard.lua parses its colours out of the config file at load time, so
+-- conky_config must point at something readable. The rendered conky.conf is
+-- gitignored and may not exist, so aim the stub at the template: it parses to
+-- no colours (its values are still @PLACEHOLDER@), which is fine here because
+-- this test only exercises the grid maths.
+conky_config = 'conky.conf.in'
+
+dofile('dashboard.lua')
+local G = conky_dashboard_internal
+assert(G, 'dashboard.lua must export conky_dashboard_internal')
+
+-- Every cell must land inside the surface, on both real monitors.
+for _, screen in ipairs({ { 2560, 1080 }, { 1920, 1080 } }) do
+ local sw, sh = screen[1], screen[2]
+ for col = 1, G.COLS do
+ for row = 1, G.ROWS do
+ local r = G.rect_for({ col = col, row = row, w = 1, h = 1 }, sw, sh)
+ assert(r.x >= G.MARGIN - 0.01,
+ ('x underflows margin at %dx%d col %d'):format(sw, sh, col))
+ assert(r.y >= G.MARGIN - 0.01,
+ ('y underflows margin at %dx%d row %d'):format(sw, sh, row))
+ assert(r.x + r.w <= sw - G.MARGIN + 0.01,
+ ('cell overflows width at %dx%d col %d: x=%f w=%f'):format(sw, sh, col, r.x, r.w))
+ assert(r.y + r.h <= sh - G.MARGIN + 0.01,
+ ('cell overflows height at %dx%d row %d'):format(sw, sh, row))
+ assert(r.w > 0 and r.h > 0, 'cell must have positive size')
+ end
+ end
+end
+
+-- A spanning cell must cover its cells plus the gap between them, so two
+-- side-by-side 1-wide cards and one 2-wide card occupy the same pixels.
+local a = G.rect_for({ col = 1, row = 1, w = 1, h = 1 }, 2560, 1080)
+local b = G.rect_for({ col = 2, row = 1, w = 1, h = 1 }, 2560, 1080)
+local span = G.rect_for({ col = 1, row = 1, w = 2, h = 1 }, 2560, 1080)
+assert(math.abs((b.x + b.w) - (span.x + span.w)) < 0.01,
+ ('a 2-wide card must end where the second 1-wide card ends: %f vs %f')
+ :format(b.x + b.w, span.x + span.w))
+assert(math.abs(span.w - (a.w * 2 + G.GAP)) < 0.01, 'span must absorb the gap')
+
+-- The shipped layout must not place anything outside the declared grid, which
+-- is the mistake a user editing the table will actually make.
+for _, e in ipairs(G.layout or {}) do
+ assert(e.col >= 1 and e.col + (e.w or 1) - 1 <= G.COLS,
+ 'layout entry out of columns: ' .. tostring(e.widget))
+ assert(e.row >= 1 and e.row + (e.h or 1) - 1 <= G.ROWS,
+ 'layout entry out of rows: ' .. tostring(e.widget))
+end
+
+print('test_layout: all assertions passed')