Debian Codegraph @

upstream/mageia-cauldron/man3pm/Test2::API::InterceptResult.3pm

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