1Tickit::Test(3) User Contributed Perl Documentation Tickit::Test(3)
2
3
4
6 "Tickit::Test" - unit testing for "Tickit"-based code
7
9 use Test::More tests => 2;
10 use Tickit::Test;
11
12 use Tickit::Widget::Static;
13
14 my $win = mk_window;
15
16 my $widget = Tickit::Widget::Static->new( text => "Message" );
17
18 $widget->set_window( $win );
19
20 flush_tickit;
21
22 is_termlog( [ SETPEN,
23 CLEAR,
24 GOTO(0,0),
25 SETPEN,
26 PRINT("Message"),
27 SETBG(undef),
28 ERASECH(73) ] );
29
30 is_display( [ "Message" ] );
31
33 This module helps write unit tests for Tickit-based code, such as
34 Tickit::Widget subclasses. Primarily, it provides a mock terminal
35 implementation, allowing the code under test to affect a virtual
36 terminal, whose state is inspectable by the unit test script.
37
38 This module is used by the "Tickit" unit tests themselves, and provided
39 as an installable module, so that authors of widget subclasses can use
40 it too.
41
43 mk_term
44 $term = mk_term
45
46 Constructs and returns the mock terminal to unit test with. This object
47 will be cached and returned if this function is called again. Most unit
48 tests will want a root window as well; for convenience see instead
49 "mk_term_and_window".
50
51 The mock terminal usually starts with a size of 80 columns and 25
52 lines, though can be overridden by passing named arguments.
53
54 $term = mk_term lines => 30, cols => 100;
55
56 mk_tickit
57 $tickit = mk_tickit
58
59 Constructs and returns the mock toplevel Tickit instance to unit test
60 with. This object will be cached and returned if the function is
61 called again.
62
63 Note that this object is not a full implementation and in particular
64 does not have a real event loop. Any later or timer watches are stored
65 internally and flushed by the "flush_tickit" function. This helps
66 isolate unit tests from real-world effects.
67
68 mk_window
69 $win = mk_window
70
71 Construct a root window using the mock terminal, to unit test with.
72
73 mk_term_and_window
74 ( $term, $win ) = mk_term_and_window
75
76 Constructs and returns the mock terminal and root window; equivalent to
77 calling each of "mk_term" and "mk_window" separately.
78
79 flush_tickit
80 flush_tickit( $timeskip )
81
82 Flushes any pending timer or later events in the testing "Tickit"
83 object. Because the unit test script has no real event loop, this is
84 required instead, to flush any pending events.
85
86 If the optional $timeskip argument has a nonzero value then any queued
87 timers will experience the given amount of time passing; any that
88 should now expire will be invoked.
89
90 drain_termlog
91 drain_termlog
92
93 Drains any pending events from the method log used by the "is_termlog"
94 test. Useful to clear up non-tested events before running a test.
95
96 clear_term
97 clear_term
98
99 Clears the entire content form the mock terminal. Useful at the end of
100 a section of tests before starting another one. Don't forget to
101 "drain_termlog" afterwards.
102
103 resize_term
104 resize_term( $lines, $cols )
105
106 Resize the virtual testing terminal to the size given
107
108 presskey
109 presskey( $type, $str, $mod )
110
111 Fire a key event
112
113 pressmouse
114 pressmouse( $type, $button, $line, $col, $mod )
115
116 Fire a mouse button event
117
119 The following functions can be used like "Test::More" primitives, in
120 unit test scripts.
121
122 is_termlog
123 is_termlog( [ @log ], $name )
124
125 Asserts that the mock terminal log contains exactly the given sequence
126 of methods. See also the helper functions below.
127
128 Because this test is quite fragile, relying on the exact nature and
129 order of drawing methods invoked on the terminal, it should only be
130 used rarely. Most normal cases of widget unit tests should instead only
131 use "is_display".
132
133 is_termlog( { $pos => \@log, ... }, $name )
134
135 The expectation HASH is keyed by strings giving a GOTO position, and
136 the test asserts that a sequence of GOTO and other operations were
137 performed equivalent to the expectations given in the HASH.
138
139 This differs from the simpler ARRAY reference form by being somewhat
140 more robust against rendering order. It checks that every expectation
141 sequence happens exactly once, but does not care which order the
142 sections happen in.
143
144 is_termlog( { "0,0" => [ PRINT("Hello") ],
145 "0,6" => [ PRINT("World!") ] } );
146
147 is_display
148 is_display( $lines, $name )
149
150 Asserts that the mock terminal display is exactly that as given by the
151 content of $lines, which must be an ARRAY reference containing one
152 value for each line of the display. Each item may either be a plain
153 string, or an ARRAY reference.
154
155 If a plain string is given, it asserts that the characters on display
156 are those as given by the string (trailing blanks may be omitted). The
157 pen attributes of the characters do not matter in this case.
158
159 is_display( [ "some lines of",
160 "content here" ] );
161
162 If an ARRAY reference is given, it should contain chunks of content
163 from the "TEXT" function. Each chunk represents content on display for
164 the corresponding columns.
165
166 is_display( [ [TEXT("some"), TEXT(" lines of")],
167 "content here" ] );
168
169 The "TEXT" function accepts pen attributes, to assert that the
170 displayed characters have exactly the attributes given. In character
171 cells containing spaces, only the "bg" attribute is tested.
172
173 is_display( [ [TEXT("This is ",fg=>2), TEXT("bold",fg=>2,b=>1) ] ] );
174
175 The "BLANK" function is a shortcut to providing a number of blank cells
176
177 BLANK(20,bg=>1) is TEXT(" ",bg=>1)
178
179 The "BLANKLINE" and "BLANKLINES" functions are a shortcut to providing
180 an entire line, or several lines, of blank content. They yield an array
181 reference or list of array references directly.
182
183 BLANKLINE is [TEXT("")]
184 BLANKLINES(3) is [TEXT("")], [TEXT("")], [TEXT("")]
185
186 is_cursorpos
187 is_cursorpos( $line, $col, $name )
188
189 Asserts that the mock terminal cursor is at the given position.
190
191 is_termctl
192 is_termctl( $ctl, $value, $name )
193
194 Asserts that the mock terminal has the given value for the given
195 terminal control. $ctl should be a value from the
196 "Tickit::Term::TERMPROP_*" constants.
197
199 The following functions can be used to help write the expected log for
200 a call to "is_termlog".
201
202 CLEAR
203 GOTO($line,$col)
204 ERASECH($count,$move_to_end)
205 SCROLLRECT($top,$left,$lines,$cols,$downward,$rightward)
206 PRINT($string)
207 SETPEN(%attrs)
208 SETBG($bg_attr)
209
211 Paul Evans <leonerd@leonerd.org.uk>
212
213
214
215perl v5.36.0 2023-01-20 Tickit::Test(3)