From 906044cb31569d7681ccf9d161f98fe3bd409277 Mon Sep 17 00:00:00 2001 From: Thijs Schreijer Date: Thu, 20 Jun 2024 10:07:32 +0200 Subject: add tests for autotermrestore --- spec/04-term_spec.lua | 84 +++++++++++++++++++++++++++++++++++++++++++++++++-- system/init.lua | 7 +++++ 2 files changed, 89 insertions(+), 2 deletions(-) diff --git a/spec/04-term_spec.lua b/spec/04-term_spec.lua index e888920..57fb4d0 100644 --- a/spec/04-term_spec.lua +++ b/spec/04-term_spec.lua @@ -1,4 +1,3 @@ --- Import the library that contains the environment-related functions local system = require("system") require("spec.helpers") @@ -221,7 +220,7 @@ describe("Terminal:", function() describe("tcsetattr()", function() nix_it("sets the terminal flags, if called with flags #manual", function() - system.listtermflags(io.stdin) + -- system.listtermflags(io.stdin) local old_flags = assert(system.tcgetattr(io.stdin)) finally(function() system.tcsetattr(io.stdin, system.TCSANOW, old_flags) -- ensure we restore the original ones @@ -673,6 +672,87 @@ describe("Terminal:", function() + describe("autotermrestore()", function() + + local old_backup + local old_restore + local result + + + before_each(function() + _G._TEST = true + + package.loaded["system"] = nil + system = require("system") + + old_backup = system.termbackup + old_restore = system.termrestore + system.termbackup = function(...) + table.insert(result,"backup") + return old_backup(...) + end + + system.termrestore = function(...) + table.insert(result,"restore") + return old_restore(...) + end + + result = {} + end) + + + after_each(function() + -- system.termbackup = old_backup + -- system.termrestore = old_restore + _G._TEST = false + + package.loaded["system"] = nil + system = require("system") + end) + + + + it("calls backup", function() + local ok, err = system.autotermrestore() + assert.is_nil(err) + assert.is_true(ok) + + assert.are.same({"backup"}, result) + end) + + + it("returns an error on the second call", function() + local ok, err = system.autotermrestore() + assert.is_nil(err) + assert.is_true(ok) + + local ok, err = system.autotermrestore() + assert.are.equal("global terminal backup was already set up", err) + assert.is_nil(ok) + end) + + + it("calls restore upon being garbage collected", function() + local ok, err = system.autotermrestore() + assert.is_nil(err) + assert.is_true(ok) + + -- ensure tables from previous tests are GC'ed + collectgarbage() + collectgarbage() + -- clear references + result = {} + system._reset_global_backup() + collectgarbage() + collectgarbage() + + assert.are.same({"restore"}, result) + end) + + end) + + + describe("keyboard input", function() local old_readkey = system._readkey diff --git a/system/init.lua b/system/init.lua index ee43c4b..926370c 100644 --- a/system/init.lua +++ b/system/init.lua @@ -124,6 +124,13 @@ do -- autotermrestore system.termrestore(self) end) return true end + + -- export a reset function only upon testing + if _G._TEST then + function system._reset_global_backup() + global_backup = nil + end + end end -- cgit v1.2.3