diff --git a/spec/00-helpers_spec.lua b/spec/00-helpers_spec.lua index 1672be6..0451499 100644 --- a/spec/00-helpers_spec.lua +++ b/spec/00-helpers_spec.lua @@ -84,7 +84,7 @@ describe("Spec helpers", function() it("returns bytes from the helper _readkey buffer", function() helpers.load() - helpers._push_input("ab") + helpers.push_kb_input("ab") local b1 = helpers._readkey() local b2 = helpers._readkey() @@ -99,7 +99,7 @@ describe("Spec helpers", function() it("returns nil and error when pushing an error entry", function() helpers.load() - helpers._push_input(nil, "some-error") + helpers.push_kb_input(nil, "some-error") local b, err = helpers._readkey() assert.is_nil(b) @@ -110,7 +110,7 @@ describe("Spec helpers", function() it("patches system._readkey to use the mock buffer", function() helpers.load() - helpers._push_input("X") + helpers.push_kb_input("X") local system = require("system") local b = system._readkey() @@ -122,7 +122,7 @@ describe("Spec helpers", function() it("terminal.input.readansi() returns data read from the mock buffer", function() local terminal = helpers.load() - helpers._push_input("X") + helpers.push_kb_input("X") local rawkey, keytype = terminal.input.readansi(0.01) assert.equals("X", rawkey) diff --git a/spec/02-input_spec.lua b/spec/02-input_spec.lua index 31cafe8..425ab62 100644 --- a/spec/02-input_spec.lua +++ b/spec/02-input_spec.lua @@ -1,42 +1,17 @@ -describe("input:", function() - - local t, sys, old_readkey - local keyboard_buffer - - setup(function() - sys = require("system") - - -- patch low level readkey, such that it won't block during tests - old_readkey = sys._readkey - sys._readkey = function() - if keyboard_buffer == "" then - return nil - end - local char = keyboard_buffer:sub(1, 1) - keyboard_buffer = keyboard_buffer:sub(2) - return string.byte(char) - end - end) +local helpers = require "spec.helpers" - teardown(function() - sys._readkey = old_readkey - end) +describe("input:", function() + local t before_each(function() - keyboard_buffer = "" - t = require("terminal") + t = helpers.load() end) after_each(function() - -- forcefully unload module - for mod in pairs(package.loaded) do - if mod:match("^terminal") then - package.loaded[mod] = nil - end - end + helpers.unload() end) @@ -45,6 +20,7 @@ describe("input:", function() describe("sys_readansi()", function() it("matches system.readansi()", function() + local sys = require("system") assert.are.equal(sys.readansi, t.input.sys_readansi) end) @@ -68,13 +44,13 @@ describe("input:", function() it("reads a single character", function() - keyboard_buffer = "a" + helpers.push_kb_input("a") assert.are.equal("a", t.input.readansi(0.01)) end) it("reads from the buffer first", function() - keyboard_buffer = "a" + helpers.push_kb_input("a") t.input.push_input("b", "key", nil) assert.are.equal("b", t.input.readansi(0.01)) assert.are.equal("a", t.input.readansi(0.01)) @@ -87,10 +63,8 @@ describe("input:", function() describe("preread()", function() it("empties the keyboard-buffer into the preread-buffer", function() - keyboard_buffer = "abc" + helpers.push_kb_input("abc") t.input.preread() - assert.are.equal("", keyboard_buffer) - assert.are.equal("a", t.input.readansi(0.01)) assert.are.equal("b", t.input.readansi(0.01)) assert.are.equal("c", t.input.readansi(0.01)) @@ -109,7 +83,7 @@ describe("input:", function() setup(function() -- returns an ANSWER sequence to the cursor-position query add_cpos = function(row, col) - keyboard_buffer = keyboard_buffer .. ("\027[%d;%dR"):format(row, col) + helpers.push_kb_input(("\027[%d;%dR"):format(row, col)) end cursor_answer_pattern = "^\27%[(%d+);(%d+)R$" end) @@ -124,11 +98,10 @@ describe("input:", function() it("leaves other 'char' input in the buffers", function() - keyboard_buffer = "abc" + helpers.push_kb_input("abc") add_cpos(12, 34) - keyboard_buffer = keyboard_buffer .. "123" + helpers.push_kb_input("123") assert.are.same({{"12", "34"}}, t.input.read_query_answer(cursor_answer_pattern, 1)) - assert.are.equal("123", keyboard_buffer) assert.are.equal("a", t.input.readansi(0)) assert.are.equal("b", t.input.readansi(0)) assert.are.equal("c", t.input.readansi(0)) @@ -139,9 +112,8 @@ describe("input:", function() it("leaves other 'ansi' input in the buffers", function() - keyboard_buffer = "\27[8;10;80t\027[12;34R\027[56;78R\027[90;12R" + helpers.push_kb_input("\27[8;10;80t\027[12;34R\027[56;78R\027[90;12R") assert.are.same({{"12", "34"},{"56", "78"}}, t.input.read_query_answer(cursor_answer_pattern, 2)) - assert.are.equal("\027[90;12R", keyboard_buffer) local binstring = require("luassert.formatters.binarystring") assert:add_formatter(binstring) local r = {t.input.readansi(0)} diff --git a/spec/04-scroll_spec.lua b/spec/04-scroll_spec.lua index 05be6d5..98b4fd4 100644 --- a/spec/04-scroll_spec.lua +++ b/spec/04-scroll_spec.lua @@ -1,22 +1,18 @@ +local helpers = require "spec.helpers" + + describe("Scroll Module Tests", function() - local scroll, old_sys_termsize + local scroll setup(function() - local sys = require "system" - old_sys_termsize = sys.termsize - if os.getenv("GITHUB_ACTIONS") then - sys.termsize = function() - return 25, 80 - end - end - + helpers.load() scroll = require "terminal.scroll" end) teardown(function() - require("system").termsize = old_sys_termsize + helpers.unload() end) diff --git a/spec/09-editline-format_spec.lua b/spec/09-editline-format_spec.lua index fc7fe3e..30ee6e9 100644 --- a/spec/09-editline-format_spec.lua +++ b/spec/09-editline-format_spec.lua @@ -1,16 +1,19 @@ +local helpers = require "spec.helpers" + + describe("EditLine:", function() local EditLine - before_each(function() - _G._TEST = true + setup(function() + helpers.load() EditLine = require("terminal.editline") end) - after_each(function() + teardown(function() EditLine = nil - _G._TEST = nil + helpers.unload() end) diff --git a/spec/11-screen_spec.lua b/spec/11-screen_spec.lua index 085a3a5..8af085d 100644 --- a/spec/11-screen_spec.lua +++ b/spec/11-screen_spec.lua @@ -7,14 +7,14 @@ describe("terminal.ui.screen", function() local Panel local terminal - before_each(function() + setup(function() terminal = helpers.load() Screen = require("terminal.ui.panel.screen") Panel = require("terminal.ui.panel") end) - after_each(function() + teardown(function() helpers.unload() end) diff --git a/spec/13-bar_spec.lua b/spec/13-bar_spec.lua index 9076b90..7ab0be7 100644 --- a/spec/13-bar_spec.lua +++ b/spec/13-bar_spec.lua @@ -4,34 +4,15 @@ local helpers = require "spec.helpers" describe("terminal.ui.panel.bar", function() local Bar - local terminal setup(function() - -- Load modules - terminal = helpers.load() + helpers.load() Bar = require("terminal.ui.panel.bar") - - -- Mock terminal functions for testing - terminal.cursor = { - position = { - set = function() end, - backup = function() end, - restore = function() end, - left_seq = function() return "" end, - down_seq = function() return "" end, - up_seq = function() return "" end - } - } - terminal.output = { - write = function() end - } end) teardown(function() - -- Unset modules for clean test isolation Bar = nil - terminal = nil -- luacheck: ignore helpers.unload() end) diff --git a/spec/14-text_panel_spec.lua b/spec/14-text_panel_spec.lua index 6488a40..4b0650d 100644 --- a/spec/14-text_panel_spec.lua +++ b/spec/14-text_panel_spec.lua @@ -4,44 +4,16 @@ local helpers = require "spec.helpers" describe("terminal.ui.panel.text", function() local TextPanel - local terminal local text setup(function() - terminal = helpers.load() + helpers.load() TextPanel = require("terminal.ui.panel.text") text = require("terminal.text") - - -- Mock terminal functions - terminal.cursor = { - position = { - set = function(row, col) end, - set_seq = function(row, col) return "pos_seq" end, - backup_seq = function() return "backup_seq" end, - restore_seq = function() return "restore_seq" end - } - } - terminal.output = { - write = function(text) end - } - terminal.text = { - stack = { - push_seq = function(attr) return "push_seq" end, - pop_seq = function() return "pop_seq" end - } - } - terminal.clear = { - box = function(row, col, height, width) end, - box_seq = function(row, col, height, width) return "clear_seq" end, - eol_seq = function() return "eol_seq" end - } end) teardown(function() - TextPanel = nil - terminal = nil -- luacheck: ignore - text = nil helpers.unload() end) diff --git a/spec/15-key_bar_spec.lua b/spec/15-key_bar_spec.lua index 67e8aa6..df9d1bf 100644 --- a/spec/15-key_bar_spec.lua +++ b/spec/15-key_bar_spec.lua @@ -8,35 +8,18 @@ describe("terminal.ui.panel.key_bar", function() local text setup(function() - -- Load modules terminal = helpers.load() KeyBar = require("terminal.ui.panel.key_bar") text = require("terminal.text") - - -- Mock terminal functions for testing - terminal.cursor = { - position = { - set = function() end, - backup = function() end, - restore = function() end, - set_seq = function() return "" end, - backup_seq = function() return "" end, - restore_seq = function() return "" end, - } - } - terminal.output = { - write = function() end - } end) + teardown(function() - -- Unset modules for clean test isolation - KeyBar = nil - terminal = nil -- luacheck: ignore helpers.unload() end) + describe("init()", function() it("creates a key bar with default values", function() @@ -96,6 +79,7 @@ describe("terminal.ui.panel.key_bar", function() end) + describe("_build_lines()", function() it("builds a single-row line with equal columns", function() @@ -263,6 +247,7 @@ describe("terminal.ui.panel.key_bar", function() end) + describe("render()", function() it("calls _draw and respects panel inner coords", function() @@ -299,6 +284,7 @@ describe("terminal.ui.panel.key_bar", function() end) + describe("separator feature", function() it("works correctly in two-row layout", function() diff --git a/spec/16-set_spec.lua b/spec/16-set_spec.lua index 557f3cc..41587cd 100644 --- a/spec/16-set_spec.lua +++ b/spec/16-set_spec.lua @@ -1,17 +1,20 @@ +local helpers = require "spec.helpers" + + describe("terminal.ui.panel.set", function() local Set local Panel setup(function() + helpers.load() Set = require("terminal.ui.panel.set") Panel = require("terminal.ui.panel.init") end) teardown(function() - Set = nil - Panel = nil -- luacheck: ignore + helpers.unload() end) diff --git a/spec/17-tab_strip_spec.lua b/spec/17-tab_strip_spec.lua index de724eb..2993748 100644 --- a/spec/17-tab_strip_spec.lua +++ b/spec/17-tab_strip_spec.lua @@ -7,38 +7,12 @@ describe("terminal.ui.panel.tab_strip", function() local terminal setup(function() - -- Load modules terminal = helpers.load() TabStrip = require("terminal.ui.panel.tab_strip") - -- Mock terminal functions for testing - terminal.cursor = { - position = { - set = function() end, - backup = function() end, - restore = function() end, - set_seq = function() return "" end, - backup_seq = function() return "" end, - restore_seq = function() return "" end, - left_seq = function() return "" end, - down_seq = function() return "" end, - up_seq = function() return "" end - } - } - terminal.output = { - write = function() end - } - terminal.clear = { - eol_seq = function() return "" end, - box = function() return "" end, - box_seq = function() return "" end - } end) teardown(function() - -- Unset modules for clean test isolation - TabStrip = nil - terminal = nil -- luacheck: ignore helpers.unload() end) diff --git a/spec/18-prompt_spec.lua b/spec/18-prompt_spec.lua index b330938..d02a514 100644 --- a/spec/18-prompt_spec.lua +++ b/spec/18-prompt_spec.lua @@ -5,13 +5,13 @@ describe("terminal.cli.prompt", function() local Prompt - before_each(function() + setup(function() helpers.load() Prompt = require("terminal.cli.prompt") end) - after_each(function() + teardown(function() helpers.unload() end) @@ -52,7 +52,7 @@ describe("terminal.cli.prompt", function() } -- Queue Esc key (from keymap) - helpers._push_input(helpers.keys.esc) + helpers.push_kb_input(helpers.keys.esc) local result, err = prompt:run() @@ -69,7 +69,7 @@ describe("terminal.cli.prompt", function() } -- Queue Ctrl+C key (from keymap) - helpers._push_input(helpers.keys.ctrl_c) + helpers.push_kb_input(helpers.keys.ctrl_c) local result, err = prompt:run() @@ -90,7 +90,7 @@ describe("terminal.cli.prompt", function() } -- Queue Enter key (platform-specific) - helpers._push_input(helpers.keys.enter) + helpers.push_kb_input(helpers.keys.enter) local result, err = prompt:run() @@ -106,7 +106,7 @@ describe("terminal.cli.prompt", function() } -- Queue Enter key (platform-specific) - helpers._push_input(helpers.keys.enter) + helpers.push_kb_input(helpers.keys.enter) local result, err = prompt:run() diff --git a/spec/helpers.lua b/spec/helpers.lua index 7297e6a..b8f2411 100644 --- a/spec/helpers.lua +++ b/spec/helpers.lua @@ -97,8 +97,12 @@ end --- Pushes input into the keyboard buffer mock. -- @tparam string seq the sequence of input, individual bytes of this string will be returned -- @tparam string err an eror to return, in this case `seq` MUST be nil. -function M._push_input(seq, err) - -- TODO: rename this function, remove underscore +-- @usage +-- helper.push_kb_input(helper.keys.esc) -- push a specific named key +-- helper.push_kb_input(helper.keys.up) -- push an ansi sequence for the "up" arrow key +-- helper.push_kb_input(nil, "fail reading keyboard") -- push an error +-- helper.push_kb_input("some text to type") -- push a string of bytes +function M.push_kb_input(seq, err) local buffer = get_config().keyboardbuffer if type(seq) == "string" then @@ -161,9 +165,9 @@ end --- A lookup table for key sequences to push into the keyboard buffer. -- @table keys -- @usage --- helper._push_input(helper.keys.esc) --- helper._push_input(helper.keys.ctrl_c) --- helper._push_input(helper.keys.enter) +-- helper.push_kb_input(helper.keys.esc) +-- helper.push_kb_input(helper.keys.ctrl_c) +-- helper.push_kb_input(helper.keys.enter) M.keys = setmetatable({}, { __newindex = function(self, key, value) error("table is read-only", 2)