Debian Codegraph @

upstream/debian-unstable/man3/Test2::API::InterceptResult.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::API::InterceptResult 3perl"
58 .TH Test2::API::InterceptResult 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::API::InterceptResult \- Representation of a list of events.
65 .SH DESCRIPTION
66 .IX Header "DESCRIPTION"
67 This class represents a list of events, normally obtained using \f(CWintercept()\fR
68 from Test2::API.
69 .PP
70 This class is intended for people who with to verify the results of test tools
71 they write.
72 .PP
73 This class provides methods to normalize, summarize, or map the list of events.
74 The output of these operations makes verifying your testing tools and the
75 events they generate significantly easier. In most cases this spares you from
76 needing a deep understanding of the event/facet model.
77 .SH SYNOPSIS
78 .IX Header "SYNOPSIS"
79 Usually you get an instance of this class when you use \f(CWintercept()\fR from
80 Test2::API.
81 .PP
82 .Vb 2
83 \& use Test2::V0;
84 \& use Test2::API qw/intercept/;
85 \&
86 \& my $events = intercept {
87 \& ok(1, "pass");
88 \& ok(0, "fail");
89 \& todo "broken" => sub { ok(0, "fixme") };
90 \& plan 3;
91 \& };
92 \&
93 \& # This is typically the most useful construct
94 \& # squash_info() merges assertions and diagnostics that are associated
95 \& # (and returns a new instance with the modifications)
96 \& # flatten() condenses the facet data into the key details for each event
97 \& # (and returns those structures in an arrayref)
98 \& is(
99 \& $events\->squash_info\->flatten(),
100 \& [
101 \& {
102 \& causes_failure => 0,
103 \&
104 \& name => \*(Aqpass\*(Aq,
105 \& pass => 1,
106 \&
107 \& trace_file => \*(Aqxxx.t\*(Aq,
108 \& trace_line => 5,
109 \& },
110 \& {
111 \& causes_failure => 1,
112 \&
113 \& name => \*(Aqfail\*(Aq,
114 \& pass => 0,
115 \&
116 \& trace_file => \*(Aqxxx.t\*(Aq,
117 \& trace_line => 6,
118 \&
119 \& # There can be more than one diagnostics message so this is
120 \& # always an array when present.
121 \& diag => ["Failed test \*(Aqfail\*(Aq\enat xxx.t line 6."],
122 \& },
123 \& {
124 \& causes_failure => 0,
125 \&
126 \& name => \*(Aqfixme\*(Aq,
127 \& pass => 0,
128 \&
129 \& trace_file => \*(Aqxxx.t\*(Aq,
130 \& trace_line => 7,
131 \&
132 \& # There can be more than one diagnostics message or todo
133 \& # reason, so these are always an array when present.
134 \& todo => [\*(Aqbroken\*(Aq],
135 \&
136 \& # Diag message was turned into a note since the assertion was
137 \& # TODO
138 \& note => ["Failed test \*(Aqfixme\*(Aq\enat xxx.t line 7."],
139 \& },
140 \& {
141 \& causes_failure => 0,
142 \&
143 \& plan => 3,
144 \&
145 \& trace_file => \*(Aqxxx.t\*(Aq,
146 \& trace_line => 8,
147 \& },
148 \& ],
149 \& "Flattened events look like we expect"
150 \& );
151 .Ve
152 .PP
153 See Test2::API::InterceptResult::Event for a full description of what
154 \&\f(CWflatten()\fR provides for each event.
155 .SH METHODS
156 .IX Header "METHODS"
157 Please note that no methods modify the original instance unless asked to do so.
158 .SS CONSTRUCTION
159 .IX Subsection "CONSTRUCTION"
160 .ie n .IP "$events = Test2::API::InterceptResult\->new(@EVENTS)" 4
161 .el .IP "\f(CW$events\fR = Test2::API::InterceptResult\->new(@EVENTS)" 4
162 .IX Item "$events = Test2::API::InterceptResult->new(@EVENTS)"
163 .PD 0
164 .ie n .IP "$events = Test2::API::InterceptResult\->new_from_ref(\e@EVENTS)" 4
165 .el .IP "\f(CW$events\fR = Test2::API::InterceptResult\->new_from_ref(\e@EVENTS)" 4
166 .IX Item "$events = Test2::API::InterceptResult->new_from_ref(@EVENTS)"
167 .PD
168 These create a new instance of Test2::API::InterceptResult from the given
169 events.
170 .Sp
171 In the first form a new blessed arrayref is returned. In the 'new_from_ref'
172 form the reference you pass in is directly blessed.
173 .Sp
174 Both of these will throw an exception if called in void context. This is mainly
175 important for the 'filtering' methods listed below which normally return a new
176 instance, they throw an exception in such cases as it probably means someone
177 meant to filter the original in place.
178 .ie n .IP "$clone = $events\->\fBclone()\fR" 4
179 .el .IP "\f(CW$clone\fR = \f(CW$events\fR\->\fBclone()\fR" 4
180 .IX Item "$clone = $events->clone()"
181 Make a clone of the original events. Note that this is a deep copy, the entire
182 structure is duplicated. This uses \f(CW\*(C`dclone\*(C'\fR from Storable to achieve the
183 deep clone.
184 .SS NORMALIZATION
185 .IX Subsection "NORMALIZATION"
186 .ie n .IP "@events = $events\->event_list" 4
187 .el .IP "\f(CW@events\fR = \f(CW$events\fR\->event_list" 4
188 .IX Item "@events = $events->event_list"
189 This returns all the events in list-form.
190 .ie n .IP "$hub = $events\->hub" 4
191 .el .IP "\f(CW$hub\fR = \f(CW$events\fR\->hub" 4
192 .IX Item "$hub = $events->hub"
193 This returns a new Test2::Hub instance that has processed all the events
194 contained in the instance. This gives you a simple way to inspect the state
195 changes your events cause.
196 .ie n .IP "$state = $events\->state" 4
197 .el .IP "\f(CW$state\fR = \f(CW$events\fR\->state" 4
198 .IX Item "$state = $events->state"
199 This returns a summary of the state of a hub after processing all the events.
200 .Sp
201 .Vb 5
202 \& {
203 \& count => 2, # Number of assertions made
204 \& failed => 1, # Number of test failures seen
205 \& is_passing => 0, # Boolean, true if the test would be passing
206 \& # after the events are processed.
207 \&
208 \& plan => 2, # Plan, either a number, undef, \*(AqSKIP\*(Aq, or \*(AqNO PLAN\*(Aq
209 \& follows_plan => 1, # True if there is a plan and it was followed.
210 \& # False if the plan and assertions did not
211 \& # match, undef if no plan was present in the
212 \& # event list.
213 \&
214 \& bailed_out => undef, # undef unless there was a bail\-out in the
215 \& # events in which case this will be a string
216 \& # explaining why there was a bailout, if no
217 \& # reason was given this will simply be set to
218 \& # true (1).
219 \&
220 \& skip_reason => undef, # If there was a skip_all this will give the
221 \& # reason.
222 \& }
223 .Ve
224 .ie n .IP "$new = $events\->upgrade" 4
225 .el .IP "\f(CW$new\fR = \f(CW$events\fR\->upgrade" 4
226 .IX Item "$new = $events->upgrade"
227 .PD 0
228 .ie n .IP "$events\->upgrade(in_place => $BOOL)" 4
229 .el .IP "\f(CW$events\fR\->upgrade(in_place => \f(CW$BOOL\fR)" 4
230 .IX Item "$events->upgrade(in_place => $BOOL)"
231 .PD
232 \&\fBNote:\fR This normally returns a new instance, leaving the original unchanged.
233 If you call it in void context it will throw an exception. If you want to
234 modify the original you must pass in the \f(CW\*(C`in_place => 1\*(C'\fR option. You may
235 call this in void context when you ask to modify it in place. The in-place form
236 returns the instance that was modified so you can chain methods.
237 .Sp
238 This will create a clone of the list where all events have been converted into
239 Test2::API::InterceptResult::Event instances. This is extremely helpful as
240 Test2::API::InterceptResult::Event provide a much better interface for
241 working with events. This allows you to avoid thinking about legacy event
242 types.
243 .Sp
244 This also means your tests against the list are not fragile if the tool
245 you are testing randomly changes what type of events it generates (IE Changing
246 from Test2::Event::Ok to Test2::Event::Pass, both make assertions and
247 both will normalize to identical (or close enough)
248 Test2::API::InterceptResult::Event instances.
249 .Sp
250 Really you almost always want this, the only reason it is not done
251 automatically is to make sure the \f(CWintercept()\fR tool is backwards compatible.
252 .ie n .IP "$new = $events\->squash_info" 4
253 .el .IP "\f(CW$new\fR = \f(CW$events\fR\->squash_info" 4
254 .IX Item "$new = $events->squash_info"
255 .PD 0
256 .ie n .IP "$events\->squash_info(in_place => $BOOL)" 4
257 .el .IP "\f(CW$events\fR\->squash_info(in_place => \f(CW$BOOL\fR)" 4
258 .IX Item "$events->squash_info(in_place => $BOOL)"
259 .PD
260 \&\fBNote:\fR This normally returns a new instance, leaving the original unchanged.
261 If you call it in void context it will throw an exception. If you want to
262 modify the original you must pass in the \f(CW\*(C`in_place => 1\*(C'\fR option. You may
263 call this in void context when you ask to modify it in place. The in-place form
264 returns the instance that was modified so you can chain methods.
265 .Sp
266 \&\fBNote:\fR All events in the new or modified instance will be converted to
267 Test2::API::InterceptResult::Event instances. There is no way to avoid this,
268 the squash operation requires the upgraded event class.
269 .Sp
270 Test::More and many other legacy tools would send notes, diags, and
271 assertions as separate events. A subtest in Test::More would send a note
272 with the subtest name, the subtest assertion, and finally a diagnostics event
273 if the subtest failed. This method will normalize things by squashing the note
274 and diag into the same event as the subtest (This is different from putting
275 them into the subtest, which is not what happens).
276 .SS FILTERING
277 .IX Subsection "FILTERING"
278 \&\fBNote:\fR These normally return new instances, leaving the originals unchanged.
279 If you call them in void context they will throw exceptions. If you want to
280 modify the originals you must pass in the \f(CW\*(C`in_place => 1\*(C'\fR option. You may
281 call these in void context when you ask to modify them in place. The in-place
282 forms return the instance that was modified so you can chain methods.
283 .PP
284 \fR\f(CI%PARAMS\fR\fI\fR
285 .IX Subsection "%PARAMS"
286 .PP
287 These all accept the same 2 optional parameters:
288 .ie n .IP "in_place => $BOOL" 4
289 .el .IP "in_place => \f(CW$BOOL\fR" 4
290 .IX Item "in_place => $BOOL"
291 When true the method will modify the instance in place instead of returning a
292 new instance.
293 .IP "args => \e@ARGS" 4
294 .IX Item "args => @ARGS"
295 If you wish to pass parameters into the event method being used for filtering,
296 you may do so here.
297 .PP
298 \fIMETHODS\fR
299 .IX Subsection "METHODS"
300 .ie n .IP "$events\->grep($CALL, %PARAMS)" 4
301 .el .IP "\f(CW$events\fR\->grep($CALL, \f(CW%PARAMS\fR)" 4
302 .IX Item "$events->grep($CALL, %PARAMS)"
303 This is essentially:
304 .Sp
305 .Vb 3
306 \& Test2::API::InterceptResult\->new(
307 \& grep { $_\->$CALL( @{$PARAMS{args}} ) } $self\->event_list,
308 \& );
309 .Ve
310 .Sp
311 \&\fBNote:\fR that \f(CW$CALL\fR is called on an upgraded version of the event, though
312 the events returned will be the original ones, not the upgraded ones.
313 .Sp
314 \&\f(CW$CALL\fR may be either the name of a method on
315 Test2::API::InterceptResult::Event, or a coderef.
316 .ie n .IP $events\->asserts(%PARAMS) 4
317 .el .IP \f(CW$events\fR\->asserts(%PARAMS) 4
318 .IX Item "$events->asserts(%PARAMS)"
319 This is essentially:
320 .Sp
321 .Vb 1
322 \& $events\->grep(has_assert => @{$PARAMS{args}})
323 .Ve
324 .Sp
325 It returns a new instance containing only the events that made assertions.
326 .ie n .IP $events\->subtests(%PARAMS) 4
327 .el .IP \f(CW$events\fR\->subtests(%PARAMS) 4
328 .IX Item "$events->subtests(%PARAMS)"
329 This is essentially:
330 .Sp
331 .Vb 1
332 \& $events\->grep(has_subtest => @{$PARAMS{args}})
333 .Ve
334 .Sp
335 It returns a new instance containing only the events that have subtests.
336 .ie n .IP $events\->diags(%PARAMS) 4
337 .el .IP \f(CW$events\fR\->diags(%PARAMS) 4
338 .IX Item "$events->diags(%PARAMS)"
339 This is essentially:
340 .Sp
341 .Vb 1
342 \& $events\->grep(has_diags => @{$PARAMS{args}})
343 .Ve
344 .Sp
345 It returns a new instance containing only the events that have diags.
346 .ie n .IP $events\->notes(%PARAMS) 4
347 .el .IP \f(CW$events\fR\->notes(%PARAMS) 4
348 .IX Item "$events->notes(%PARAMS)"
349 This is essentially:
350 .Sp
351 .Vb 1
352 \& $events\->grep(has_notes => @{$PARAMS{args}})
353 .Ve
354 .Sp
355 It returns a new instance containing only the events that have notes.
356 .ie n .IP $events\->errors(%PARAMS) 4
357 .el .IP \f(CW$events\fR\->errors(%PARAMS) 4
358 .IX Item "$events->errors(%PARAMS)"
359 \&\fBNote:\fR Errors are NOT failing assertions. Failing assertions are a different
360 thing.
361 .Sp
362 This is essentially:
363 .Sp
364 .Vb 1
365 \& $events\->grep(has_errors => @{$PARAMS{args}})
366 .Ve
367 .Sp
368 It returns a new instance containing only the events that have errors.
369 .ie n .IP $events\->plans(%PARAMS) 4
370 .el .IP \f(CW$events\fR\->plans(%PARAMS) 4
371 .IX Item "$events->plans(%PARAMS)"
372 This is essentially:
373 .Sp
374 .Vb 1
375 \& $events\->grep(has_plan => @{$PARAMS{args}})
376 .Ve
377 .Sp
378 It returns a new instance containing only the events that set the plan.
379 .ie n .IP $events\->causes_fail(%PARAMS) 4
380 .el .IP \f(CW$events\fR\->causes_fail(%PARAMS) 4
381 .IX Item "$events->causes_fail(%PARAMS)"
382 .PD 0
383 .ie n .IP $events\->causes_failure(%PARAMS) 4
384 .el .IP \f(CW$events\fR\->causes_failure(%PARAMS) 4
385 .IX Item "$events->causes_failure(%PARAMS)"
386 .PD
387 These are essentially:
388 .Sp
389 .Vb 2
390 \& $events\->grep(causes_fail => @{$PARAMS{args}})
391 \& $events\->grep(causes_failure => @{$PARAMS{args}})
392 .Ve
393 .Sp
394 \&\fBNote:\fR \f(CWcauses_fail()\fR and \f(CWcauses_failure()\fR are both aliases for
395 eachother in events, so these methods are effectively aliases here as well.
396 .Sp
397 It returns a new instance containing only the events that cause failure.
398 .SS MAPPING
399 .IX Subsection "MAPPING"
400 These methods \fBALWAYS\fR return an arrayref.
401 .PP
402 \&\fBNote:\fR No methods on Test2::API::InterceptResult::Event alter the event in
403 any way.
404 .PP
405 \&\fBImportant Notes about Events\fR:
406 .PP
407 Test2::API::InterceptResult::Event was tailor-made to be used in
408 event-lists. Most methods that are not applicable to a given event will return
409 an empty list, so you normally do not need to worry about unwanted \f(CW\*(C`undef\*(C'\fR
410 values or exceptions being thrown. Mapping over event methods is an entended
411 use, so it works well to produce lists.
412 .PP
413 \&\fBExceptions to the rule:\fR
414 .PP
415 Some methods such as \f(CW\*(C`causes_fail\*(C'\fR always return a boolean true or false for
416 all events. Any method prefixed with \f(CW\*(C`the_\*(C'\fR conveys the intent that the event
417 should have exactly 1 of something, so those will throw an exception when that
418 condition is not true.
419 .ie n .IP "$arrayref = $events\->map($CALL, %PARAMS)" 4
420 .el .IP "\f(CW$arrayref\fR = \f(CW$events\fR\->map($CALL, \f(CW%PARAMS\fR)" 4
421 .IX Item "$arrayref = $events->map($CALL, %PARAMS)"
422 This is essentially:
423 .Sp
424 .Vb 1
425 \& [ map { $_\->$CALL(@{ $PARAMS{args} }) } $events\->upgrade\->event_list ];
426 .Ve
427 .Sp
428 \&\f(CW$CALL\fR may be either the name of a method on
429 Test2::API::InterceptResult::Event, or a coderef.
430 .ie n .IP "$arrayref = $events\->flatten(%PARAMS)" 4
431 .el .IP "\f(CW$arrayref\fR = \f(CW$events\fR\->flatten(%PARAMS)" 4
432 .IX Item "$arrayref = $events->flatten(%PARAMS)"
433 This is essentially:
434 .Sp
435 .Vb 1
436 \& [ map { $_\->flatten(@{ $PARAMS{args} }) } $events\->upgrade\->event_list ];
437 .Ve
438 .Sp
439 It returns a new list of flattened structures.
440 .Sp
441 See Test2::API::InterceptResult::Event for details on what \f(CWflatten()\fR
442 returns.
443 .ie n .IP "$arrayref = $events\->briefs(%PARAMS)" 4
444 .el .IP "\f(CW$arrayref\fR = \f(CW$events\fR\->briefs(%PARAMS)" 4
445 .IX Item "$arrayref = $events->briefs(%PARAMS)"
446 This is essentially:
447 .Sp
448 .Vb 1
449 \& [ map { $_\->briefs(@{ $PARAMS{args} }) } $events\->upgrade\->event_list ];
450 .Ve
451 .Sp
452 It returns a new list of event briefs.
453 .Sp
454 See Test2::API::InterceptResult::Event for details on what \f(CWbrief()\fR
455 returns.
456 .ie n .IP "$arrayref = $events\->summaries(%PARAMS)" 4
457 .el .IP "\f(CW$arrayref\fR = \f(CW$events\fR\->summaries(%PARAMS)" 4
458 .IX Item "$arrayref = $events->summaries(%PARAMS)"
459 This is essentially:
460 .Sp
461 .Vb 1
462 \& [ map { $_\->summaries(@{ $PARAMS{args} }) } $events\->upgrade\->event_list ];
463 .Ve
464 .Sp
465 It returns a new list of event summaries.
466 .Sp
467 See Test2::API::InterceptResult::Event for details on what \f(CWsummary()\fR
468 returns.
469 .ie n .IP "$arrayref = $events\->subtest_results(%PARAMS)" 4
470 .el .IP "\f(CW$arrayref\fR = \f(CW$events\fR\->subtest_results(%PARAMS)" 4
471 .IX Item "$arrayref = $events->subtest_results(%PARAMS)"
472 This is essentially:
473 .Sp
474 .Vb 1
475 \& [ map { $_\->subtest_result(@{ $PARAMS{args} }) } $events\->upgrade\->event_list ];
476 .Ve
477 .Sp
478 It returns a new list of event summaries.
479 .Sp
480 See Test2::API::InterceptResult::Event for details on what
481 \&\f(CWsubtest_result()\fR returns.
482 .ie n .IP "$arrayref = $events\->diag_messages(%PARAMS)" 4
483 .el .IP "\f(CW$arrayref\fR = \f(CW$events\fR\->diag_messages(%PARAMS)" 4
484 .IX Item "$arrayref = $events->diag_messages(%PARAMS)"
485 This is essentially:
486 .Sp
487 .Vb 1
488 \& [ map { $_\->diag_messages(@{ $PARAMS{args} }) } $events\->upgrade\->event_list ];
489 .Ve
490 .Sp
491 It returns a new list of diagnostic messages (strings).
492 .Sp
493 See Test2::API::InterceptResult::Event for details on what
494 \&\f(CWdiag_messages()\fR returns.
495 .ie n .IP "$arrayref = $events\->note_messages(%PARAMS)" 4
496 .el .IP "\f(CW$arrayref\fR = \f(CW$events\fR\->note_messages(%PARAMS)" 4
497 .IX Item "$arrayref = $events->note_messages(%PARAMS)"
498 This is essentially:
499 .Sp
500 .Vb 1
501 \& [ map { $_\->note_messages(@{ $PARAMS{args} }) } $events\->upgrade\->event_list ];
502 .Ve
503 .Sp
504 It returns a new list of notification messages (strings).
505 .Sp
506 See Test2::API::InterceptResult::Event for details on what
507 \&\f(CWnote_messages()\fR returns.
508 .ie n .IP "$arrayref = $events\->error_messages(%PARAMS)" 4
509 .el .IP "\f(CW$arrayref\fR = \f(CW$events\fR\->error_messages(%PARAMS)" 4
510 .IX Item "$arrayref = $events->error_messages(%PARAMS)"
511 This is essentially:
512 .Sp
513 .Vb 1
514 \& [ map { $_\->error_messages(@{ $PARAMS{args} }) } $events\->upgrade\->event_list ];
515 .Ve
516 .Sp
517 It returns a new list of error messages (strings).
518 .Sp
519 See Test2::API::InterceptResult::Event for details on what
520 \&\f(CWerror_messages()\fR returns.
521 .SH SOURCE
522 .IX Header "SOURCE"
523 The source code repository for Test2 can be found at
524 <https://github.com/Test\-More/test\-more/>.
525 .SH MAINTAINERS
526 .IX Header "MAINTAINERS"
527 .IP "Chad Granum <[email protected]>" 4
528 .IX Item "Chad Granum <[email protected]>"
529 .SH AUTHORS
530 .IX Header "AUTHORS"
531 .PD 0
532 .IP "Chad Granum <[email protected]>" 4
533 .IX Item "Chad Granum <[email protected]>"
534 .PD
535 .SH COPYRIGHT
536 .IX Header "COPYRIGHT"
537 Copyright 2020 Chad Granum <[email protected]>.
538 .PP
539 This program is free software; you can redistribute it and/or
540 modify it under the same terms as Perl itself.
541 .PP
542 See <https://dev.perl.org/licenses/>