Debian Codegraph @

upstream/debian-unstable/man3/Test2::Manual::Anatomy::Event.3perl

raw
1 .\" -*- mode: troff; coding: utf-8 -*-
2 .\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45)
3 .\"
4 .\" Standard preamble:
5 .\" ========================================================================
6 .de Sp \" Vertical space (when we can't use .PP)
7 .if t .sp .5v
8 .if n .sp
9 ..
10 .de Vb \" Begin verbatim text
11 .ft CW
12 .nf
13 .ne \\$1
14 ..
15 .de Ve \" End verbatim text
16 .ft R
17 .fi
18 ..
19 .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
20 .ie n \{\
21 . ds C` ""
22 . ds C' ""
23 'br\}
24 .el\{\
25 . ds C`
26 . ds C'
27 'br\}
28 .\"
29 .\" Escape single quotes in literal strings from groff's Unicode transform.
30 .ie \n(.g .ds Aq \(aq
31 .el .ds Aq '
32 .\"
33 .\" If the F register is >0, we'll generate index entries on stderr for
34 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
35 .\" entries marked with X<> in POD. Of course, you'll have to process the
36 .\" output yourself in some meaningful fashion.
37 .\"
38 .\" Avoid warning from groff about undefined register 'F'.
39 .de IX
40 ..
41 .nr rF 0
42 .if \n(.g .if rF .nr rF 1
43 .if (\n(rF:(\n(.g==0)) \{\
44 . if \nF \{\
45 . de IX
46 . tm Index:\\$1\t\\n%\t"\\$2"
47 ..
48 . if !\nF==2 \{\
49 . nr % 0
50 . nr F 2
51 . \}
52 . \}
53 .\}
54 .rr rF
55 .\" ========================================================================
56 .\"
57 .IX Title "Test2::Manual::Anatomy::Event 3perl"
58 .TH Test2::Manual::Anatomy::Event 3perl 2025-11-16 "perl v5.40.1" "Perl Programmers Reference Guide"
59 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
60 .\" way too many mistakes in technical documents.
61 .if n .ad l
62 .nh
63 .SH NAME
64 Test2::Manual::Anatomy::Event \- The internals of events
65 .SH DESCRIPTION
66 .IX Header "DESCRIPTION"
67 Events are how tools effect global state, and pass information along to the
68 harness, or the human running the tests.
69 .SH HISTORY
70 .IX Header "HISTORY"
71 Before proceeding it is important that you know some history of events.
72 Initially there was an event API, and an event would implement the API to
73 produce an effect. This API proved to be lossy and inflexible. Recently the
74 \&'facet' system was introduced, and makes up for the shortcoming and
75 inflexibility of the old API.
76 .PP
77 All events must still implement the old API, but that can be largely automated
78 if you use the facet system effectively. Likewise essential facets can often be
79 deduced from events that only implement the old API, though their information
80 maybe less complete.
81 .SH "THE EVENT OBJECT"
82 .IX Header "THE EVENT OBJECT"
83 All event objects must subclass Test2::Event. If you inherit from this base
84 class, and implement the old API properly, facets will be generated for you for
85 free. On the other hand you can inherit from this, and also import
86 Test2::Util::Facets2Legacy which will instead rely on your facet data, and
87 deduce the old API from them.
88 .PP
89 All new events \f(CW\*(C`MUST\*(C'\fR implement both APIs one way or the other. A common way
90 to do this is to simply implement both APIs directly in your event.
91 .PP
92 Here is a good template for a new event:
93 .PP
94 .Vb 3
95 \& package Test2::Event::Mine;
96 \& use strict;
97 \& use warnings;
98 \&
99 \& use parent \*(AqTest2::Event\*(Aq;
100 \& use Test2::Util::Facets2Legacy \*(Aq:ALL\*(Aq;
101 \&
102 \& sub facet_data {
103 \& my $self = shift;
104 \&
105 \& # Adds \*(Aqabout\*(Aq, \*(Aqamnesty\*(Aq, and \*(Aqtrace\*(Aq facets
106 \& my $out = $self\->common_facet_data;
107 \&
108 \& # Add any additional facets to the $out hashref
109 \& ...
110 \&
111 \& return $out;
112 \& }
113 \&
114 \& 1;
115 .Ve
116 .SH "THE FACET API"
117 .IX Header "THE FACET API"
118 The new API is a single method: \f(CWfacet_data()\fR. This method must return a
119 hashref where each key is specific to a facet type, and the value is either a
120 facet hashref, or an array of hashrefs. Some facets \f(CW\*(C`MUST\*(C'\fR be lone hashrefs,
121 others \f(CW\*(C`MUST\*(C'\fR be hashrefs inside an arrayref.
122 .PP
123 The \fIstandard\fR facet types are as follows:
124 .ie n .IP "assert => {details => $name, pass => $bool, no_debug => $bool, number => $maybe_int}" 4
125 .el .IP "assert => {details => \f(CW$name\fR, pass => \f(CW$bool\fR, no_debug => \f(CW$bool\fR, number => \f(CW$maybe_int\fR}" 4
126 .IX Item "assert => {details => $name, pass => $bool, no_debug => $bool, number => $maybe_int}"
127 Documented in Test2::EventFacet::Assert. An event may only have one.
128 .Sp
129 The 'details' key is the name of the assertion.
130 .Sp
131 The 'pass' key denotes a passing or failing assertion.
132 .Sp
133 The 'no_debug' key tells any harness or formatter that diagnostics should not
134 be added automatically to a failing assertion (used when there are custom
135 diagnostics instead).
136 .Sp
137 The 'number' key is for harness use, never set it yourself.
138 .ie n .IP "about => {details => $string, no_display => $bool, package => $pkg}" 4
139 .el .IP "about => {details => \f(CW$string\fR, no_display => \f(CW$bool\fR, package => \f(CW$pkg\fR}" 4
140 .IX Item "about => {details => $string, no_display => $bool, package => $pkg}"
141 Documented in Test2::EventFacet::About. An event may only have one.
142 .Sp
143 \&'details' is a human readable string describing the overall event.
144 .Sp
145 \&'no_display' means that a formatter/harness should hide the event.
146 .Sp
147 \&'package' is the package of the event the facet describes (IE: Test2::Event::Ok)
148 .ie n .IP "amnesty => [{details => $string, tag => $short_string, inherited => $bool}]" 4
149 .el .IP "amnesty => [{details => \f(CW$string\fR, tag => \f(CW$short_string\fR, inherited => \f(CW$bool\fR}]" 4
150 .IX Item "amnesty => [{details => $string, tag => $short_string, inherited => $bool}]"
151 Documented in Test2::EventFacet::Amnesty. An event may have multiple.
152 .Sp
153 This event is how things like 'todo' are implemented. Amnesty prevents a
154 failing assertion from causing a global test failure.
155 .Sp
156 \&'details' is a human readable description of why the failure is being granted
157 amnesty (IE The 'todo' reason)
158 .Sp
159 \&'tag' is a short human readable string, or category for the amnesty. This is
160 typically 'TODO' or 'SKIP'.
161 .Sp
162 \&'inherited' is true if the amnesty was applied in a parent context (true if
163 this test is run in a subtest that is marked todo).
164 .ie n .IP "control => {details => $string, global => $bool, terminate => $maybe_int, halt => $bool, has_callback => $bool, encoding => $enc}" 4
165 .el .IP "control => {details => \f(CW$string\fR, global => \f(CW$bool\fR, terminate => \f(CW$maybe_int\fR, halt => \f(CW$bool\fR, has_callback => \f(CW$bool\fR, encoding => \f(CW$enc\fR}" 4
166 .IX Item "control => {details => $string, global => $bool, terminate => $maybe_int, halt => $bool, has_callback => $bool, encoding => $enc}"
167 Documented in Test2::EventFacet::Control. An event may have one.
168 .Sp
169 This facet is used to apply extra behavior when the event is processed.
170 .Sp
171 \&'details' is a human readable explanation for the behavior.
172 .Sp
173 \&'global' true if this event should be forwarded to, and processed by, all hubs
174 everywhere. (bail-out uses this)
175 .Sp
176 \&'terminate' this should either be undef, or an integer. When defined this will
177 cause the test to exit with the specific exit code.
178 .Sp
179 \&'halt' is used to signal any harness that no further test files should be run
180 (bail-out uses this).
181 .Sp
182 \&'has_callback' is set to true if the event has a callback sub defined.
183 .Sp
184 \&'encoding' used to tell the formatter what encoding to use.
185 .ie n .IP "errors => [{details => $string, tag => $short_string, fail => $bool}]" 4
186 .el .IP "errors => [{details => \f(CW$string\fR, tag => \f(CW$short_string\fR, fail => \f(CW$bool\fR}]" 4
187 .IX Item "errors => [{details => $string, tag => $short_string, fail => $bool}]"
188 Documented in Test2::EventFacet::Error. An event may have multiple.
189 .Sp
190 \&'details' is a human readable explanation of the error.
191 .Sp
192 \&'tag' is a short human readable category for the error.
193 .Sp
194 \&'fail' is true if the error should cause test failure. If this is false the
195 error is simply informative, but not fatal.
196 .ie n .IP "info => [{details => $string, tag => $short_string, debug => $bool, important => $bool}]" 4
197 .el .IP "info => [{details => \f(CW$string\fR, tag => \f(CW$short_string\fR, debug => \f(CW$bool\fR, important => \f(CW$bool\fR}]" 4
198 .IX Item "info => [{details => $string, tag => $short_string, debug => $bool, important => $bool}]"
199 Documented in Test2::EventFacet::Info. An event may have multiple.
200 .Sp
201 This is how diag and note are implemented.
202 .Sp
203 \&'details' human readable message.
204 .Sp
205 \&'tag' short category for the message, such as 'diag' or 'note'.
206 .Sp
207 \&'debug' is true if the message is diagnostics in nature, this is the main
208 difference between a note and a diag.
209 .Sp
210 \&'important' is true if the message is not diagnostics, but is important to have
211 it shown anyway. This is primarily used to communicate with a harness.
212 .ie n .IP "parent => {details => $string, hid => $hid, children => [...], buffered => 1}" 4
213 .el .IP "parent => {details => \f(CW$string\fR, hid => \f(CW$hid\fR, children => [...], buffered => 1}" 4
214 .IX Item "parent => {details => $string, hid => $hid, children => [...], buffered => 1}"
215 Documented in Test2::EventFacet::Parent. An event may have one.
216 .Sp
217 This is used by subtests.
218 .Sp
219 \&'details' human readable name of the subtest.
220 .Sp
221 \&'hid' subtest hub id.
222 .Sp
223 \&'children' an arrayref containing facet_data instances from all child events.
224 .Sp
225 \&'buffered' true if it was a buffered subtest.
226 .ie n .IP "plan => {details => $string, count => $int, skip => $bool, none => $bool}" 4
227 .el .IP "plan => {details => \f(CW$string\fR, count => \f(CW$int\fR, skip => \f(CW$bool\fR, none => \f(CW$bool\fR}" 4
228 .IX Item "plan => {details => $string, count => $int, skip => $bool, none => $bool}"
229 Documented in Test2::EventFacet::Plan. An event may have one.
230 .Sp
231 \&'details' is a human readable string describing the plan (for instance, why a
232 test is skipped)
233 .Sp
234 \&'count' is the number of expected assertions (0 for skip)
235 .Sp
236 \&'skip' is true if the plan is to skip the test.
237 .Sp
238 \&'none' used for Test::More's 'no_plan' plan.
239 .ie n .IP "trace => {details => $string, frame => [$pkg, $file, $line, $sub], pid => $int, tid => $int, cid => $cid, hid => $hid, nested => $int, buffered => $bool}" 4
240 .el .IP "trace => {details => \f(CW$string\fR, frame => [$pkg, \f(CW$file\fR, \f(CW$line\fR, \f(CW$sub\fR], pid => \f(CW$int\fR, tid => \f(CW$int\fR, cid => \f(CW$cid\fR, hid => \f(CW$hid\fR, nested => \f(CW$int\fR, buffered => \f(CW$bool\fR}" 4
241 .IX Item "trace => {details => $string, frame => [$pkg, $file, $line, $sub], pid => $int, tid => $int, cid => $cid, hid => $hid, nested => $int, buffered => $bool}"
242 Documented in Test2::EventFacet::Trace. An event may have one.
243 .Sp
244 This is how debugging information is tracked. This is taken from the context
245 object at event creation.
246 .Sp
247 \&'details' human readable debug message (otherwise generated from frame)
248 .Sp
249 \&'frame' first 4 fields returned by caller:
250 \&\f(CW\*(C`[$package, $file, $line, $subname]\*(C'\fR.
251 .Sp
252 \&'pid' the process id in which the event was created.
253 .Sp
254 \&'tid' the thread is in which the event was created.
255 .Sp
256 \&'cid' the id of the context used to create the event.
257 .Sp
258 \&'hid' the id of the hub to which the event was sent.
259 .Sp
260 \&'nest' subtest nesting depth of the event.
261 .Sp
262 \&'buffered' is true if the event was generated inside a buffered subtest.
263 .PP
264 Note that ALL facet types have a 'details' key that may have a string. This
265 string should always be human readable, and should be an explanation for the
266 facet. For an assertion this is the test name. For a plan this is the reason
267 for the plan (such as skip reason). For info it is the human readable
268 diagnostics message.
269 .SS "CUSTOM FACETS"
270 .IX Subsection "CUSTOM FACETS"
271 You can write custom facet types as well, simply add a new key to the hash and
272 populated it. The general rule is that any code looking at the facets should
273 ignore any it does not understand.
274 .PP
275 Optionally you can also create a package to document your custom facet. The
276 package should be proper object, and may have additional methods to help work
277 with your facet.
278 .PP
279 .Vb 1
280 \& package Test2::EventFacet::MyFacet;
281 \&
282 \& use parent \*(AqTest2::EventFacet\*(Aq;
283 \&
284 \& sub facet_key { \*(Aqmyfacet\*(Aq }
285 \& sub is_list { 0 }
286 \&
287 \& 1;
288 .Ve
289 .PP
290 Your facet package should always be under the Test2::EventFacet:: namespace if
291 you want any tools to automatically find it. The last part of the namespace
292 should be the non-plural name of your facet with only the first word
293 capitalized.
294 .ie n .IP "$string = $facet_class\->facet_key" 4
295 .el .IP "\f(CW$string\fR = \f(CW$facet_class\fR\->facet_key" 4
296 .IX Item "$string = $facet_class->facet_key"
297 The key for your facet should be the same as the last section of
298 the namespace, but all lowercase. You \fImay\fR append 's' to the key if your
299 facet is a list type.
300 .ie n .IP "$bool = $facet_class\->is_list" 4
301 .el .IP "\f(CW$bool\fR = \f(CW$facet_class\fR\->is_list" 4
302 .IX Item "$bool = $facet_class->is_list"
303 True if an event should put these facets in a list:
304 .Sp
305 .Vb 1
306 \& { myfacet => [{}, {}] }
307 .Ve
308 .Sp
309 False if an event may only have one of this type of facet at a time:
310 .Sp
311 .Vb 1
312 \& { myfacet => {} }
313 .Ve
314 .PP
315 \fIEXAMPLES\fR
316 .IX Subsection "EXAMPLES"
317 .PP
318 The assert facet is not a list type, so its implementation would look like this:
319 .PP
320 .Vb 3
321 \& package Test2::EventFacet::Assert;
322 \& sub facet_key { \*(Aqassert\*(Aq }
323 \& sub is_list { 0 }
324 .Ve
325 .PP
326 The amnesty facet is a list type, but amnesty does not need 's' appended to
327 make it plural:
328 .PP
329 .Vb 3
330 \& package Test2::EventFacet::Amnesty;
331 \& sub facet_key { \*(Aqamnesty\*(Aq }
332 \& sub is_list { 1 }
333 .Ve
334 .PP
335 The error facet is a list type, and appending 's' makes error plural as errors.
336 This means the package name is '::Error', but the key is 'errors'.
337 .PP
338 .Vb 3
339 \& package Test2::EventFacet::Error;
340 \& sub facet_key { \*(Aqerrors\*(Aq }
341 \& sub is_list { 1 }
342 .Ve
343 .PP
344 \&\fBNote2:\fR In practice most tools completely ignore the facet packages, and work
345 with the facet data directly in its raw structure. This is by design and
346 recommended. The facet data is intended to be serialized frequently and passed
347 around. When facets are concerned, data is important, classes and methods are
348 not.
349 .SH "THE OLD API"
350 .IX Header "THE OLD API"
351 The old API was simply a set of methods you were required to implement:
352 .ie n .IP "$bool = $e\->causes_fail" 4
353 .el .IP "\f(CW$bool\fR = \f(CW$e\fR\->causes_fail" 4
354 .IX Item "$bool = $e->causes_fail"
355 Returns true if this event should result in a test failure. In general this
356 should be false.
357 .ie n .IP "$bool = $e\->increments_count" 4
358 .el .IP "\f(CW$bool\fR = \f(CW$e\fR\->increments_count" 4
359 .IX Item "$bool = $e->increments_count"
360 Should be true if this event should result in a test count increment.
361 .ie n .IP $e\->callback($hub) 4
362 .el .IP \f(CW$e\fR\->callback($hub) 4
363 .IX Item "$e->callback($hub)"
364 If your event needs to have extra effects on the Test2::Hub you can override
365 this method.
366 .Sp
367 This is called \fBBEFORE\fR your event is passed to the formatter.
368 .ie n .IP "$num = $e\->nested" 4
369 .el .IP "\f(CW$num\fR = \f(CW$e\fR\->nested" 4
370 .IX Item "$num = $e->nested"
371 If this event is nested inside of other events, this should be the depth of
372 nesting. (This is mainly for subtests)
373 .ie n .IP "$bool = $e\->global" 4
374 .el .IP "\f(CW$bool\fR = \f(CW$e\fR\->global" 4
375 .IX Item "$bool = $e->global"
376 Set this to true if your event is global, that is ALL threads and processes
377 should see it no matter when or where it is generated. This is not a common
378 thing to want, it is used by bail-out and skip_all to end testing.
379 .ie n .IP "$code = $e\->terminate" 4
380 .el .IP "\f(CW$code\fR = \f(CW$e\fR\->terminate" 4
381 .IX Item "$code = $e->terminate"
382 This is called \fBAFTER\fR your event has been passed to the formatter. This
383 should normally return undef, only change this if your event should cause the
384 test to exit immediately.
385 .Sp
386 If you want this event to cause the test to exit you should return the exit
387 code here. Exit code of 0 means exit success, any other integer means exit with
388 failure.
389 .Sp
390 This is used by Test2::Event::Plan to exit 0 when the plan is
391 \&'skip_all'. This is also used by Test2::Event:Bail to force the test
392 to exit with a failure.
393 .Sp
394 This is called after the event has been sent to the formatter in order to
395 ensure the event is seen and understood.
396 .ie n .IP "$msg = $e\->summary" 4
397 .el .IP "\f(CW$msg\fR = \f(CW$e\fR\->summary" 4
398 .IX Item "$msg = $e->summary"
399 This is intended to be a human readable summary of the event. This should
400 ideally only be one line long, but you can use multiple lines if necessary. This
401 is intended for human consumption. You do not need to make it easy for machines
402 to understand.
403 .Sp
404 The default is to simply return the event package name.
405 .ie n .IP "($count, $directive, $reason) = $e\->\fBsets_plan()\fR" 4
406 .el .IP "($count, \f(CW$directive\fR, \f(CW$reason\fR) = \f(CW$e\fR\->\fBsets_plan()\fR" 4
407 .IX Item "($count, $directive, $reason) = $e->sets_plan()"
408 Check if this event sets the testing plan. It will return an empty list if it
409 does not. If it does set the plan it will return a list of 1 to 3 items in
410 order: Expected Test Count, Test Directive, Reason for directive.
411 .ie n .IP "$bool = $e\->diagnostics" 4
412 .el .IP "\f(CW$bool\fR = \f(CW$e\fR\->diagnostics" 4
413 .IX Item "$bool = $e->diagnostics"
414 True if the event contains diagnostics info. This is useful because a
415 non-verbose harness may choose to hide events that are not in this category.
416 Some formatters may choose to send these to STDERR instead of STDOUT to ensure
417 they are seen.
418 .ie n .IP "$bool = $e\->no_display" 4
419 .el .IP "\f(CW$bool\fR = \f(CW$e\fR\->no_display" 4
420 .IX Item "$bool = $e->no_display"
421 False by default. This will return true on events that should not be displayed
422 by formatters.
423 .SH "SEE ALSO"
424 .IX Header "SEE ALSO"
425 Test2::Manual \- Primary index of the manual.
426 .SH SOURCE
427 .IX Header "SOURCE"
428 The source code repository for Test2\-Manual can be found at
429 \&\fIhttps://github.com/Test\-More/Test2\-Suite/\fR.
430 .SH MAINTAINERS
431 .IX Header "MAINTAINERS"
432 .IP "Chad Granum <[email protected]>" 4
433 .IX Item "Chad Granum <[email protected]>"
434 .SH AUTHORS
435 .IX Header "AUTHORS"
436 .PD 0
437 .IP "Chad Granum <[email protected]>" 4
438 .IX Item "Chad Granum <[email protected]>"
439 .PD
440 .SH COPYRIGHT
441 .IX Header "COPYRIGHT"
442 Copyright 2018 Chad Granum <[email protected]>.
443 .PP
444 This program is free software; you can redistribute it and/or
445 modify it under the same terms as Perl itself.
446 .PP
447 See \fIhttp://dev.perl.org/licenses/\fR