Tickit::Test(3pm)

πŸ’… NAME πŸ’…

Tickit::Test(3pm) β€” unit testing for Tickit-based code, straight from User Contributed Perl Documentation. Bestie, this is the mock-terminal testing fairy godmother you didn't know you needed. 🧚✨

🌸 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 πŸ¦‹

Okay so this module helps you write unit tests for Tickit-based code, like Tickit::Widget subclasses. Its main-character move is providing a mock terminal implementation, so the code under test can affect a virtual terminal, and the unit test script can inspect that terminal’s state. πŸ’–

This module is used by the Tickit unit tests themselves, and it’s also provided as an installable module, so widget-subclass authors can use it too. Slay. πŸŽ€

✨ FUNCTIONS ✨

🌟 mk_term 🌟

$term = mk_term

Constructs and returns the mock terminal you test with. This object gets cached and returned if you call this function again. Most unit tests will want a root window too, so for convenience see instead mk_term_and_window. 🫢

The mock terminal usually starts with a size of 80 columns and 25 lines, though you can override that by passing named arguments.

$term = mk_term lines => 30, cols => 100;

🌟 mk_tickit 🌟

$tickit = mk_tickit

Constructs and returns the mock toplevel Tickit instance you test with. This object gets cached and returned if the function is called again. πŸ’…

Heads up, bestie: this object is not a full implementation, and in particular it does not have a real event loop. Any later or timer watches are stored internally and flushed by the flush_tickit function. This helps isolate unit tests from real-world effects. 😭

🌟 mk_window 🌟

$win = mk_window

Constructs 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. Two-for-one icon behavior. πŸ‘‘

🌟 flush_tickit 🌟

flush_tickit( $timeskip )

Flushes any pending timer or 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. ⭐

If the optional $timeskip argument has a nonzero value, then any queued timers will experience the given amount of time passing; any that should now expire will be invoked. Time-skip, but make it testing. πŸ•°οΈ

🌟 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 from the mock terminal. Useful at the end of a section of tests before starting another one. Don’t forget to drain_termlog afterwards. Don’t forget, okay? πŸ™ˆ

🌟 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. Yes bestie, they’re giving test-assertion realness. ✨

πŸ’– 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. No cap.
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. iykyk

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. πŸ“

πŸ’– is_termctl πŸ’–

is_termctl( $ctl, $value, $name )

Asserts that the mock terminal has the given value for the given terminal control. $ctl should be a value from the Tickit::Term::TERMPROP_* constants. πŸ’…

🌈 METHOD LOG HELPER FUNCTIONS 🌈

The following functions can be used to help write the expected log for a call to is_termlog. These are the little log-building besties. 🫢

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> πŸ’–


Signed off with maximum sparkle by perl v5.42.3, dated 2026-08-19 β€” this was Tickit::Test(3pm), bestie. Periodt. πŸ’…βœ¨

πŸ“¦ where this page comes from