Module spec.helpers
Helper functions for testing the terminal.lua library.
This module has no use for the library user, only for the developers.
Test helper for Busted specs: load and unload the terminal library (and LuaSystem)
for test isolation. Cleans from package.loaded: "terminal", any "terminal.*",
and "system". Intended for use from Busted setup/teardown or before_each/after_each.
This module does not require terminal or system at load time.
Overview
load loads the terminal and system modules, whilst patching the required functions to enable the mocks. Returns the terminal module. Also sets the
_TESTglobal to true, which can be used to export some internals (only when testing).unload removes terminal and system from package.loaded, and does some cleanup. Idempotent.
The functions provided by this module can set terminal size, push keyboard input, and collect output to make it easier to test functionality.
Functions
| set_termsize (rows, columns) | Sets the terminal size. |
output
| clear_output () | Clears the output written to the output stream. |
| get_output () | Gets the output written to the output stream. |
| set_output_isatty (value) | Sets what terminal.output.isatty() returns. |
input
| keys | A lookup table for key sequences to push into the keyboard buffer. |
| push_kb_input (seq, err) | Pushes input into the keyboard buffer mock. |
| set_input_isatty (value) | Sets what terminal.input.isatty() returns. |
setup
| load () | Load the main terminal and system modules after cleaning package.loaded. |
teardown
| unload () | Removes terminal and LuaSystem from package.loaded. |
Functions
- set_termsize (rows, columns)
-
Sets the terminal size.
If not set, the defaults size will be 25 x 80.
Parameters:
- rows number number of rows
- columns number number of columns
output
- clear_output ()
- Clears the output written to the output stream. and recreates an empty output file.
- get_output ()
-
Gets the output written to the output stream.
Returns:
-
string
the output written to the output stream, empty string if no output
was written yet.
- set_output_isatty (value)
-
Sets what
terminal.output.isatty()returns. If not set, defaults totrue.Parameters:
- value boolean the value to return
input
- keys
-
A lookup table for key sequences to push into the keyboard buffer.
Usage:
helper.push_kb_input(helper.keys.esc) helper.push_kb_input(helper.keys.ctrl_c) helper.push_kb_input(helper.keys.enter)
- push_kb_input (seq, err)
-
Pushes input into the keyboard buffer mock.
Parameters:
- seq string the sequence of input, individual bytes of this string will be returned
- err
string
an eror to return, in this case
seqMUST be nil.
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
- set_input_isatty (value)
-
Sets what
terminal.input.isatty()returns. If not set, defaults totrue.Parameters:
- value boolean the value to return
setup
- load ()
-
Load the main terminal and system modules after cleaning package.loaded.
First calls helpers.unload, then requires the main terminal module (which
requires
systemin turn). Will patch both libraries to enable the mocks. Call from Bustedsetuporbefore_each.Returns:
-
table
the main terminal module
Usage:
local helpers = require "spec.helpers" describe("some test", function() local terminal setup(function() terminal = helpers.load() end) teardown(function() helpers.unload() end) -- tests can use the terminal variable here it("does something", function( -- use terminal here end) end)
teardown
- unload ()
-
Removes terminal and LuaSystem from package.loaded.
Does not load them again. Call from Busted
teardownorafter_eachso the next helpers.load gets a fresh terminal. Idempotent.