Tickit::Test - unit testing for
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
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
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.
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.
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.
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
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" ] );
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) ] ] );
BLANK function is a shortcut to providing a number of blank cells
BLANK(20,bg=>1) is TEXT(" ",bg=>1)
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("")]
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
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 <email@example.com>