diff options
author | Thijs Schreijer <thijs@thijsschreijer.nl> | 2024-06-20 10:07:32 +0200 |
---|---|---|
committer | Thijs Schreijer <thijs@thijsschreijer.nl> | 2024-06-20 10:07:32 +0200 |
commit | 906044cb31569d7681ccf9d161f98fe3bd409277 (patch) | |
tree | 1dace996ca187b7f45197d29fff10891a80d6d61 | |
parent | e0871d7be63dd428d4a2b9a3db4e033894165cef (diff) | |
download | luasystem-906044cb31569d7681ccf9d161f98fe3bd409277.tar.gz luasystem-906044cb31569d7681ccf9d161f98fe3bd409277.tar.bz2 luasystem-906044cb31569d7681ccf9d161f98fe3bd409277.zip |
add tests for autotermrestore
-rw-r--r-- | spec/04-term_spec.lua | 84 | ||||
-rw-r--r-- | 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 @@ | |||
1 | -- Import the library that contains the environment-related functions | ||
2 | local system = require("system") | 1 | local system = require("system") |
3 | require("spec.helpers") | 2 | require("spec.helpers") |
4 | 3 | ||
@@ -221,7 +220,7 @@ describe("Terminal:", function() | |||
221 | describe("tcsetattr()", function() | 220 | describe("tcsetattr()", function() |
222 | 221 | ||
223 | nix_it("sets the terminal flags, if called with flags #manual", function() | 222 | nix_it("sets the terminal flags, if called with flags #manual", function() |
224 | system.listtermflags(io.stdin) | 223 | -- system.listtermflags(io.stdin) |
225 | local old_flags = assert(system.tcgetattr(io.stdin)) | 224 | local old_flags = assert(system.tcgetattr(io.stdin)) |
226 | finally(function() | 225 | finally(function() |
227 | system.tcsetattr(io.stdin, system.TCSANOW, old_flags) -- ensure we restore the original ones | 226 | system.tcsetattr(io.stdin, system.TCSANOW, old_flags) -- ensure we restore the original ones |
@@ -673,6 +672,87 @@ describe("Terminal:", function() | |||
673 | 672 | ||
674 | 673 | ||
675 | 674 | ||
675 | describe("autotermrestore()", function() | ||
676 | |||
677 | local old_backup | ||
678 | local old_restore | ||
679 | local result | ||
680 | |||
681 | |||
682 | before_each(function() | ||
683 | _G._TEST = true | ||
684 | |||
685 | package.loaded["system"] = nil | ||
686 | system = require("system") | ||
687 | |||
688 | old_backup = system.termbackup | ||
689 | old_restore = system.termrestore | ||
690 | system.termbackup = function(...) | ||
691 | table.insert(result,"backup") | ||
692 | return old_backup(...) | ||
693 | end | ||
694 | |||
695 | system.termrestore = function(...) | ||
696 | table.insert(result,"restore") | ||
697 | return old_restore(...) | ||
698 | end | ||
699 | |||
700 | result = {} | ||
701 | end) | ||
702 | |||
703 | |||
704 | after_each(function() | ||
705 | -- system.termbackup = old_backup | ||
706 | -- system.termrestore = old_restore | ||
707 | _G._TEST = false | ||
708 | |||
709 | package.loaded["system"] = nil | ||
710 | system = require("system") | ||
711 | end) | ||
712 | |||
713 | |||
714 | |||
715 | it("calls backup", function() | ||
716 | local ok, err = system.autotermrestore() | ||
717 | assert.is_nil(err) | ||
718 | assert.is_true(ok) | ||
719 | |||
720 | assert.are.same({"backup"}, result) | ||
721 | end) | ||
722 | |||
723 | |||
724 | it("returns an error on the second call", function() | ||
725 | local ok, err = system.autotermrestore() | ||
726 | assert.is_nil(err) | ||
727 | assert.is_true(ok) | ||
728 | |||
729 | local ok, err = system.autotermrestore() | ||
730 | assert.are.equal("global terminal backup was already set up", err) | ||
731 | assert.is_nil(ok) | ||
732 | end) | ||
733 | |||
734 | |||
735 | it("calls restore upon being garbage collected", function() | ||
736 | local ok, err = system.autotermrestore() | ||
737 | assert.is_nil(err) | ||
738 | assert.is_true(ok) | ||
739 | |||
740 | -- ensure tables from previous tests are GC'ed | ||
741 | collectgarbage() | ||
742 | collectgarbage() | ||
743 | -- clear references | ||
744 | result = {} | ||
745 | system._reset_global_backup() | ||
746 | collectgarbage() | ||
747 | collectgarbage() | ||
748 | |||
749 | assert.are.same({"restore"}, result) | ||
750 | end) | ||
751 | |||
752 | end) | ||
753 | |||
754 | |||
755 | |||
676 | describe("keyboard input", function() | 756 | describe("keyboard input", function() |
677 | 757 | ||
678 | local old_readkey = system._readkey | 758 | 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 | |||
124 | system.termrestore(self) end) | 124 | system.termrestore(self) end) |
125 | return true | 125 | return true |
126 | end | 126 | end |
127 | |||
128 | -- export a reset function only upon testing | ||
129 | if _G._TEST then | ||
130 | function system._reset_global_backup() | ||
131 | global_backup = nil | ||
132 | end | ||
133 | end | ||
127 | end | 134 | end |
128 | 135 | ||
129 | 136 | ||