The repository only shipped a spreadsheet demo, so there was no example of an interactive, stateful program. LuaPaint keeps the whole editor in Lua - canvas primitives with undo, palette, tools and a scriptable session - and its tests run without a terminal.
241 lines
10 KiB
Lua
241 lines
10 KiB
Lua
-- Self tests of the paint application: framebuffer, canvas primitives,
|
|
-- palette, keyboard handling and the recorded session.
|
|
local screen_module = require("tui.screen")
|
|
local canvas_module = require("paint.canvas")
|
|
local palette = require("paint.palette")
|
|
local app_module = require("paint.app")
|
|
local session = require("paint.session")
|
|
|
|
local M = {}
|
|
|
|
local ESC = string.char(27)
|
|
|
|
local function row_text(screen, y)
|
|
local start = (y - 1) * (screen.width + 1) + 1
|
|
local snapshot = screen:snapshot()
|
|
return string.sub(snapshot, start, start + screen.width - 1)
|
|
end
|
|
|
|
function M.run()
|
|
local passed = 0
|
|
local failures = {}
|
|
|
|
local function check(name, actual, expected)
|
|
if actual == expected then
|
|
passed = passed + 1
|
|
else
|
|
failures[#failures + 1] = string.format(
|
|
"%s : attendu %s, obtenu %s",
|
|
name, tostring(expected), tostring(actual)
|
|
)
|
|
end
|
|
end
|
|
|
|
local function check_true(name, condition, detail)
|
|
if condition then
|
|
passed = passed + 1
|
|
else
|
|
failures[#failures + 1] = name .. " : " .. tostring(detail or "condition fausse")
|
|
end
|
|
end
|
|
|
|
-- framebuffer ---------------------------------------------------------
|
|
local screen = screen_module.new(10, 3)
|
|
check("screen rows", #screen:snapshot(), 3 * 10 + 2)
|
|
screen:put(1, 1, "abc")
|
|
check("put writes", row_text(screen, 1), "abc ")
|
|
screen:put(8, 1, "abcdef")
|
|
check("put clips", row_text(screen, 1), "abc abc")
|
|
screen:put(1, 0, "x")
|
|
screen:put(99, 1, "x")
|
|
check("put ignores out of bounds", row_text(screen, 1), "abc abc")
|
|
|
|
screen:clear()
|
|
screen:box(1, 1, 4, 3)
|
|
check("box top", row_text(screen, 1), "+--+ ")
|
|
check("box middle", row_text(screen, 2), "| | ")
|
|
check("box bottom", row_text(screen, 3), "+--+ ")
|
|
|
|
screen:title(1, 1, 4, "T", 5, 6)
|
|
check_true("title visible", string.find(row_text(screen, 1), "T", 1, true) ~= nil, row_text(screen, 1))
|
|
|
|
screen:bar(1, 3, 10, "ok", 2, 3)
|
|
check("bar text", row_text(screen, 3), " ok ")
|
|
|
|
local coloured = screen_module.new(4, 2)
|
|
coloured:fill_rect(1, 1, 2, 1, "#", 9, 10)
|
|
local escaped = screen_module.frame(coloured)
|
|
check_true("frame has colour escapes", string.find(escaped, "48;5;10", 1, true) ~= nil, "no bg escape")
|
|
check_true("frame ends with reset", string.find(escaped, ESC .. "[0m", 1, true) ~= nil, "no reset")
|
|
local plain_frame = screen_module.frame(coloured, { mono = true })
|
|
check("mono frame has no escape", string.find(plain_frame, ESC, 1, true), nil)
|
|
check_true("mono frame keeps characters", string.find(plain_frame, "##", 1, true) ~= nil, plain_frame)
|
|
|
|
-- canvas primitives ---------------------------------------------------
|
|
local canvas = canvas_module.new(10, 10)
|
|
check_true("new canvas is empty", canvas:is_empty(), "not empty")
|
|
check("empty count", canvas:count(), 0)
|
|
canvas:set(3, 4, 7)
|
|
check("set/get", canvas:get(3, 4), 7)
|
|
check("set out of bounds", canvas:get(99, 99), 0)
|
|
canvas:set(99, 99, 7)
|
|
check("set ignores out of bounds", canvas:count(), 1)
|
|
|
|
local horizontal = canvas_module.new(10, 10)
|
|
horizontal:line(1, 1, 5, 1, 8)
|
|
check("horizontal line length", horizontal:count(), 5)
|
|
check("horizontal line middle", horizontal:get(3, 1), 8)
|
|
check("horizontal line ends", horizontal:get(1, 1) + horizontal:get(5, 1), 16)
|
|
|
|
local diagonal = canvas_module.new(10, 10)
|
|
diagonal:line(1, 1, 5, 5, 8)
|
|
check("diagonal line length", diagonal:count(), 5)
|
|
check("diagonal line middle", diagonal:get(3, 3), 8)
|
|
|
|
local reversed = canvas_module.new(10, 10)
|
|
reversed:line(5, 5, 1, 1, 8)
|
|
check("reversed line length", reversed:count(), 5)
|
|
check("reversed line middle", reversed:get(3, 3), 8)
|
|
|
|
local outline = canvas_module.new(10, 10)
|
|
outline:rect(2, 2, 6, 5, 3, false)
|
|
check("rect outline perimeter", outline:count(), 14)
|
|
check("rect outline corner", outline:get(2, 2), 3)
|
|
check("rect outline interior empty", outline:get(4, 3), 0)
|
|
|
|
local solid = canvas_module.new(10, 10)
|
|
solid:rect(2, 2, 6, 5, 3, true)
|
|
check("rect filled area", solid:count(), 20)
|
|
check("rect filled interior", solid:get(4, 3), 3)
|
|
|
|
local oval = canvas_module.new(21, 21)
|
|
oval:ellipse(11, 11, 5, 3, 4, true)
|
|
check("ellipse centre", oval:get(11, 11), 4)
|
|
check_true("ellipse symmetric", oval:get(6, 11) == oval:get(16, 11), "asymmetric")
|
|
check_true("ellipse painted", oval:count() > 10, oval:count())
|
|
|
|
local flat = canvas_module.new(9, 9)
|
|
flat:ellipse(5, 5, 3, 0, 6, true)
|
|
check("flat ellipse is a line", flat:count(), 7)
|
|
|
|
local bucket = canvas_module.new(7, 5)
|
|
bucket:rect(1, 1, 7, 5, 3, false)
|
|
local painted = bucket:fill(4, 3, 7)
|
|
check("fill painted interior", painted, 15)
|
|
check("fill kept outline", bucket:get(1, 1), 3)
|
|
check("fill same colour is a no-op", bucket:fill(4, 3, 7), 0)
|
|
check("fill outside is untouched", canvas_module.new(3, 3):fill(2, 2, 4), 9)
|
|
|
|
-- undo / redo ---------------------------------------------------------
|
|
local history = canvas_module.new(4, 4)
|
|
check("undo without history", history:undo(), false)
|
|
history:push_history()
|
|
history:set(1, 1, 5)
|
|
check("after edit", history:get(1, 1), 5)
|
|
check("undo returns true", history:undo(), true)
|
|
check("undo restores", history:get(1, 1), 0)
|
|
check("redo returns true", history:redo(), true)
|
|
check("redo reapplies", history:get(1, 1), 5)
|
|
check("history size limited", history:history_size() <= 32, true)
|
|
|
|
-- palette -------------------------------------------------------------
|
|
check("palette size", palette.count(), 12)
|
|
check("palette default label", palette.label(palette.default_slot), "rouge")
|
|
check("palette colour index", palette.index_of_slot(6), 226)
|
|
check("palette slot by name", palette.slot_of("jaune"), 6)
|
|
check("palette next wraps", palette.next_slot(12), 1)
|
|
check("palette previous wraps", palette.previous_slot(1), 12)
|
|
check("palette invalid colour", palette.index_of_slot(99), 0)
|
|
|
|
-- text exports --------------------------------------------------------
|
|
local exported = canvas_module.new(2, 1)
|
|
exported:set(1, 1, 17)
|
|
check("hex export", exported:to_hex(), "11 00")
|
|
check("ascii export", exported:to_ascii(), "#.")
|
|
|
|
-- application ---------------------------------------------------------
|
|
local app = app_module.new({ width = 20, height = 10 })
|
|
check("initial cursor x", app.cursor_x, 11)
|
|
check("initial cursor y", app.cursor_y, 6)
|
|
check("initial tool", app:tool_name(), "crayon")
|
|
app:handle("h")
|
|
check("move left", app.cursor_x, 10)
|
|
app:handle("j")
|
|
check("move down", app.cursor_y, 7)
|
|
for _ = 1, 50 do
|
|
app:handle("h")
|
|
end
|
|
check("cursor clamped left", app.cursor_x, 1)
|
|
app:handle("3")
|
|
check("tool selection", app:tool_name(), "rectangle")
|
|
check("anchor tool", app:needs_anchor(), true)
|
|
check("anchor press", app:handle(" "), "anchor")
|
|
app.cursor_x = 8
|
|
app.cursor_y = 6
|
|
check("shape commit", app:handle(" "), "commit")
|
|
check("shape drawn", app.canvas:get(8, 6), app:colour())
|
|
check("anchor cleared", app.anchor_x, nil)
|
|
|
|
check("colour cycle", app:handle("p"), "colour")
|
|
local cycled = app.slot
|
|
app:handle("m")
|
|
check("colour back", app.slot, palette.previous_slot(cycled))
|
|
|
|
check("undo key", app:handle("u"), "undo")
|
|
check("clear key", app:handle("c"), "clear")
|
|
check_true("canvas cleared", app.canvas:is_empty(), "not empty")
|
|
check("quit key", app:handle("q"), "quit")
|
|
check("unknown key", app:handle("@"), nil)
|
|
check("filled toggle", app:handle("f"), "mode")
|
|
check_true("filled enabled", app.filled, "filled is off")
|
|
|
|
local layout_app = app_module.new({ width = 20, height = 10 })
|
|
check("screen width", layout_app:screen_width(), 45)
|
|
check("screen height", layout_app:screen_height(), 15)
|
|
local layout = screen_module.new(layout_app:screen_width(), layout_app:screen_height())
|
|
layout_app:render(layout)
|
|
local snapshot = layout:snapshot()
|
|
check_true("title rendered", string.find(snapshot, "LuaPaint", 1, true) ~= nil, "no title")
|
|
check_true("panel rendered", string.find(snapshot, "Outils", 1, true) ~= nil, "no panel")
|
|
check_true("tools rendered", string.find(snapshot, "crayon", 1, true) ~= nil, "no tools")
|
|
check_true("status rendered", string.find(snapshot, "outil crayon", 1, true) ~= nil, "no status")
|
|
check_true("message rendered", string.find(snapshot, "espace", 1, true) ~= nil, "no message")
|
|
|
|
-- recorded session ----------------------------------------------------
|
|
local artist = app_module.new({ width = 60, height = 22 })
|
|
local stages = {}
|
|
session.run(artist, function(label)
|
|
stages[#stages + 1] = label
|
|
end)
|
|
check("session stages", #stages, 3)
|
|
check_true("session painted a lot", artist.canvas:count() > 800, artist.canvas:count())
|
|
check("sky colour", artist.canvas:get(1, 1), palette.index_of_slot(1))
|
|
check("sun colour", artist.canvas:get(50, 4), palette.index_of_slot(6))
|
|
check("ground colour", artist.canvas:get(5, 20), palette.index_of_slot(7))
|
|
check("wall colour", artist.canvas:get(12, 15), palette.index_of_slot(12))
|
|
check("door colour", artist.canvas:get(18, 15), palette.index_of_slot(1))
|
|
check("window colour", artist.canvas:get(23, 12), palette.index_of_slot(8))
|
|
check("trunk colour", artist.canvas:get(34, 17), palette.index_of_slot(12))
|
|
check("leaf colour", artist.canvas:get(34, 10), palette.index_of_slot(7))
|
|
check_true("session history", artist.canvas:history_size() > 8, artist.canvas:history_size())
|
|
check_true("session message", string.find(artist.message, "Paysage", 1, true) ~= nil, artist.message)
|
|
|
|
local session_screen = screen_module.new(artist:screen_width(), artist:screen_height())
|
|
artist:render(session_screen)
|
|
local session_snapshot = session_screen:snapshot()
|
|
check("session screen height", #session_snapshot, (artist:screen_height()) * (artist:screen_width() + 1) - 1)
|
|
|
|
local preview_app = app_module.new({ width = 30, height = 12 })
|
|
preview_app:handle("2")
|
|
preview_app:handle(" ")
|
|
preview_app.cursor_x = preview_app.cursor_x + 5
|
|
local preview = preview_app:preview()
|
|
check_true("preview exists", preview ~= nil, "no preview")
|
|
check_true("preview painted", preview:count() > 0, preview:count())
|
|
check_true("preview is not committed", preview_app.canvas:is_empty(), "canvas modified")
|
|
|
|
return { passed = passed, failed = #failures, failures = failures }
|
|
end
|
|
|
|
return M
|