NAME
Tickit::Test
- unit testing for Tickit
-based code
SYNOPSIS
use Test::More tests => 2;
use Tickit::Test;
use Tickit::Widget::Static;
my $win = mk_window;
my $widget = Tickit::Widget::Static->new( text => "Message" );
$widget->set_window( $win );
flush_tickit;
is_termlog( [ SETPEN,
CLEAR,
GOTO(0,0),
SETPEN,
PRINT("Message"),
SETBG(undef),
ERASECH(73) ] );
is_display( [ "Message" ] );
DESCRIPTION
This module helps write unit tests for Tickit-based code, such as Tickit::Widget subclasses. Primarily, it provides a mock terminal implementation, allowing the code under test to affect a virtual terminal, whose state is inspectable by the unit test script.
This module is used by the Tickit
unit tests themselves, and provided as an installable module, so that authors of widget subclasses can use it too.
FUNCTIONS
mk_term
$term = mk_term
Constructs and returns the mock terminal to unit test with. This object will be cached and returned if this function is called again. Most unit tests will want a root window as well; for convenience see instead mk_term_and_window
.
The mock terminal usually starts with a size of 80 columns and 25 lines, though can be overridden by passing named arguments.
$term = mk_term lines => 30, cols => 100;
mk_window
$win = mk_window
Construct a root window using the mock terminal, to unit test with.
mk_term_and_window
( $term, $win ) = mk_term_and_window
Constructs and returns the mock terminal and root window; equivalent to calling each of mk_term
and mk_window
separately.
flush_tickit
flush_tickit
Flushes any pending later
events in the testing Tickit
object. Because the unit test script has no real event loop, this is required instead, to flush any pending events.
drain_termlog
drain_termlog
Drains any pending events from the method log used by the is_termlog
test. Useful to clear up non-tested events before running a test.
clear_term
clear_term
Clears the entire content form the mock terminal. Useful at the end of a section of tests before starting another one. Don't forget to drain_termlog
afterwards.
resize_term
resize_term( $lines, $cols )
Resize the virtual testing terminal to the size given
presskey
presskey( $type, $str, $mod )
Fire a key event
pressmouse
pressmouse( $type, $button, $line, $col, $mod )
Fire a mouse button event
TEST FUNCTIONS
The following functions can be used like Test::More
primitives, in unit test scripts.
is_termlog
is_termlog( [ @log ], $name )
Asserts that the mock terminal log contains exactly the given sequence of methods. See also the helper functions below.
Because this test is quite fragile, relying on the exact nature and order of drawing methods invoked on the terminal, it should only be used rarely. Most normal cases of widget unit tests should instead only use is_display
.
is_termlog( { $pos => \@log, ... }, $name )
The expectation HASH is keyed by strings giving a GOTO position, and the test asserts that a sequence of GOTO and other operations were performed equivalent to the expectations given in the HASH.
This differs from the simpler ARRAY reference form by being somewhat more robust against rendering order. It checks that every expectation sequence happens exactly once, but does not care which order the sections happen in.
is_termlog( { "0,0" => [ PRINT("Hello") ],
"0,6" => [ PRINT("World!") ] } );
is_display
is_display( $lines, $name )
Asserts that the mock terminal display is exactly that as given by the content of $lines
, which must be an ARRAY reference containing one value for each line of the display. Each item may either be a plain string, or an ARRAY reference.
If a plain string is given, it asserts that the characters on display are those as given by the string (trailing blanks may be omitted). The pen attributes of the characters do not matter in this case.
is_display( [ "some lines of",
"content here" ] );
If an ARRAY reference is given, it should contain chunks of content from the TEXT
function. Each chunk represents content on display for the corresponding columns.
is_display( [ [TEXT("some"), TEXT(" lines of")],
"content here" ] );
The TEXT
function accepts pen attributes, to assert that the displayed characters have exactly the attributes given. In character cells containing spaces, only the bg
attribute is tested.
is_display( [ [TEXT("This is ",fg=>2), TEXT("bold",fg=>2,b=>1) ] ] );
The BLANK
function is a shortcut to providing a number of blank cells
BLANK(20,bg=>1) is TEXT(" ",bg=>1)
The BLANKLINE
and BLANKLINES
functions are a shortcut to providing an entire line, or several lines, of blank content. They yield an array reference or list of array references directly.
BLANKLINE is [TEXT("")]
BLANKLINES(3) is [TEXT("")], [TEXT("")], [TEXT("")]
is_cursorpos
is_cursorpos( $line, $col, $name )
Asserts that the mock terminal cursor is at the given position.
METHOD LOG HELPER FUNCTIONS
The following functions can be used to help write the expected log for a call to is_termlog
.
CLEAR
GOTO($line,$col)
ERASECH($count,$move_to_end)
SCROLLRECT($top,$left,$lines,$cols,$downward,$rightward)
PRINT($string)
SETPEN(%attrs)
SETBG($bg_attr)
AUTHOR
Paul Evans <leonerd@leonerd.org.uk>