Tickit::Test - unit testing for Tickit-based code
Tickit::Test
Tickit
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" ] );
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.
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.
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;
Construct a root window using the mock terminal, to unit test with.
Constructs and returns the mock terminal and root window; equivalent to calling each of mk_term and mk_window separately.
mk_term
mk_window
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.
later
Resize the virtual testing terminal to the size given
Fire a key event
Fire a mouse button event
The following functions can be used like Test::More primatives, in unit test scripts.
Test::More
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_display
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.
$lines
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.
TEXT
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.
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
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
BLANKLINES
BLANKLINE is [TEXT("")] BLANKLINES(3) is [TEXT("")], [TEXT("")], [TEXT("")]
Asserts that the mock terminal cursor is at the given position.
The following functions can be used to help write the expected log for a call to is_termlog.
is_termlog
CLEAR GOTO($line,$col) ERASECH($count,$move_to_end) INSERTCH($count) DELETECH($count) SCROLLRECT($top,$left,$lines,$cols,$downward,$rightward) PRINT($string) SETPEN(%attrs) SETBG($bg_attr)
Paul Evans <leonerd@leonerd.org.uk>
To install Tickit, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Tickit
CPAN shell
perl -MCPAN -e shell install Tickit
For more information on module installation, please visit the detailed CPAN module installation guide.