Test2::...::Event(3pm)
πΈ NAME πΈ
Okay bestie, this is the manual page for Test2::API::InterceptResult::Event β yes, the header squishes it down to Test2::...::Event(3pm) from the Perl Programmers Reference Guide, and honestly? Iconic abbreviation behavior. π
Test2::API::InterceptResult::Event β a representation of an event for use in testing other test tools.
β¨ DESCRIPTION β¨
So, intercept { ... } from Test2::API returns an instance of Test2::API::InterceptResult, which is a blessed arrayref of Test2::API::InterceptResult::Event objects. No cap.
This POD documents the methods of these events. They're mainly provided for you to use when you're testing your own test tools. That's the whole vibe. π«Ά
π¦ SYNOPSIS π¦
use Test2::V0;
use Test2::API qw/intercept/;
my $events = intercept {
ok(1, "A passing assertion");
plan(1);
};
# This will convert all events into instances of
# Test2::API::InterceptResult::Event. Until we do this they are the
# original Test::Event::* instances
$events->upgrade(in_place => 1);
# Now we can get individual events in this form
my $assert = $events->[0];
my $plan = $events->[1];
# Or we can operate on all events at once:
my $flattened = $events->flatten;
is(
$flattened,
[
{
causes_failure => 0,
name => 'A passing assertion',
pass => 1,
trace_file => 'xxx.t',
trace_line => 5,
},
{
causes_failure => 0,
plan => 1,
trace_file => 'xxx.t',
trace_line => 6,
},
],
"Flattened both events and returned an arrayref of the results
);
π METHODS π
π !!! IMPORTANT NOTES ON DESIGN !!! π
Bestie, please pay attention to what these methods return. Many return a scalar when applicable, or an empty list when not β as opposed to undef. Many also always return a list of 0 or more items. Some always return a scalar.
And here's the key: none of the methods care about context. Their behavior is consistent regardless of scalar, list, or void context. Periodt.
This was done because this class was specifically designed to be used in a list and generate more lists in bulk operations. Sometimes in a map you want nothing to show up for the event, and you do not want an undef sitting in its place. In general, single event instances aren't going to be used alone, though that is allowed.
As a general rule, any method prefixed with the_ implies the event should have exactly 1 of the specified item, and an exception will be thrown if there are 0, or more than 1, of the item. So don't get cute with it. π
π ATTRIBUTES π
$hashref = $event->facet_dataβ this returns the facet data hashref, which is literally allTest2cares about for any given event. Main character data structure, honestly.$class = $event->result_classβ this is normallyTest2::API::InterceptResult. It's set at construction so that subtest results can be turned into instances of it on demand. Slay.
π DUPLICATION π
$copy = $event->cloneβ creates a deep copy of the event. Modifying either event will not affect the other. Two separate little icons, no shared drama. π
π CONDENSED MULTI-FACET DATA π
$bool = $event->causes_failure$bool = $event->causes_fail
These are both aliases of the same functionality. They will always return either a true value or a false value. This never returns a list.
This method may be relatively slow (still super fast) because it determines pass or fail by creating an instance of Test2::Hub and asking it to process the event, and then asks the hub for its pass/fail state. That's slower than building in logic to do the check, but it's more reliable because it will always tell you what the hub thinks, so the logic will never be out of date relative to the Test2 logic that actually cares. Trust the hub, bestie. π
STRING_OR_EMPTY_LIST = $event->brief
Not all events have a brief. Some events are not rendered by the formatter, and others have no "brief" data worth seeing. When that's the case, an empty list is returned. This is done intentionally so it can be used in a map operation without having undef included in the result. We love a clean list. β¨
When a brief can be generated, it is always a single 1-line string, and is returned as-is, not in a list.
Possible briefs:
# From control facets
"BAILED OUT"
"BAILED OUT: $why"
# From error facets
"ERROR"
"ERROR: $message"
"ERROR: $partial_message [...]"
"ERRORS: $first_error_message [...]"
# From assert facets
"PASS"
"FAIL"
"PASS with amnesty"
"FAIL with amnesty"
# From plan facets
"PLAN $count"
"NO PLAN"
"SKIP ALL"
"SKIP ALL: $why"
Note that only the first applicable brief is returned. This is essentially a poor-mans TAP that only includes facets that could (but not necessarily do) cause a failure. Lowkey useful, highkey chaotic. π
$hashref = $event->flatten$hashref = $event->flatten(include_subevents => 1)
This ALWAYS returns a hashref. It puts all the most useful data for the most interesting facets into a single hashref for easy validation. Obsessed with a one-stop validation moment.
If there are no meaningful facets, this will return an empty hashref.
If given the include_subevents parameter, it will also include subtest data.
Here is a list of EVERY possible field. If a field is not applicable, it will not be present.
Always present:
causes_failure => 1, # Always present
Present if the event has a trace facet:
trace_line => 42,
trace_file => 'Foo/Bar.pm',
trace_details => 'Extra trace details', # usually not present
If an assertion is present:
pass => 0,
name => "1 + 1 = 2, so math works",
If a plan is present:
plan => $count_or_SKIP_ALL_or_NO_PLAN,
If amnesty facets are present, you get an array for each type that is present:
todo => [ # Yes you could be under multiple todos, this will list them all.
"I will fix this later",
"I promise to fix these",
],
skip => ["This will format the main drive, do not run"],
... => ["Other amnesty"]
If Info (note/diag) facets are present, you get an arrayref for any that are present. The key is not defined if they are not present:
diag => [
"Test failed at Foo/Bar.pm line 42",
"You forgot to tie your boots",
],
note => ["Your boots are red"],
... => ["Other info"],
If error facets are present, always an arrayref:
error => [
"non fatal error (does not cause test failure, just an FYI",
"FATAL: This is a fatal error (causes failure)",
],
# Errors can have alternative tags, but in practice are always 'error',
# listing this for completeness.
... => [ ... ]
Present if the event is a subtest:
subtest => {
count => 2, # Number of assertions made
failed => 1, # Number of test failures seen
is_passing => 0, # Boolean, true if the test would be passing
# after the events are processed.
plan => 2, # Plan, either a number, undef, 'SKIP', or 'NO PLAN'
follows_plan => 1, # True if there is a plan and it was followed.
# False if the plan and assertions did not
# match, undef if no plan was present in the
# event list.
bailed_out => "foo", # if there was a bail-out in the
# events in this will be a string explaining
# why there was a bailout, if no reason was
# given this will simply be set to true (1).
skip_reason => "foo", # If there was a skip_all this will give the
# reason.
},
If "(include_subtest => 1)" was provided as a parameter, then the following will be included. This is the result of turning all subtest child events into a Test2::API::InterceptResult instance and calling the flatten method on it:
subevents => Test2::API::InterceptResult->new(@child_events)->flatten(...),
If a bail-out is being requested, and no reason was given, this will be set to 1:
bailed_out => "reason",
$hashref = $event->summary()
This returns a limited summary. See flatten(), which is usually a better option. Like, flatten() is the main character here, but summary() is still cute. π
{
brief => $event->brief || '',
causes_failure => $event->causes_failure,
trace_line => $event->trace_line,
trace_file => $event->trace_file,
trace_tool => $event->trace_subname,
trace_details => $event->trace_details,
facets => [ sort keys(%{$event->{+FACET_DATA}}) ],
}
π§ DIRECT ARBITRARY FACET ACCESS π§
@list_of_facets = $event->facet($name)β this always returns a list of 0 or more items. It fetches the facet instances from the event. For facets like'assert', this will always return 0 or 1 item. For events like'info'(diags, notes), this will return 0 or more instances, once for each instance of the facet.
These will be blessed into the proper Test2::EventFacet subclass. If no subclass can be found, it will be blessed as an Test2::API::InterceptResult::Facet generic facet class. Respect the fallback. π
$undef_or_facet = $event->the_facet($name)β if you know you will have exactly 1 instance of a facet, you can call this.
If you are correct and there is exactly one instance of the facet, it will always return the hashref.
If there are 0 instances of the facet, this will return undef, not an empty list. Important distinction, bestie.
If there are more than 1 instance, this will throw an exception because your assumption was incorrect. Not me getting caught lacking by facet math. π
β TRACE FACET β
@list_of_facets = $event->traceβTODO$undef_or_hashref = $event->the_traceβ this returns the trace hashref, orundefif it is not present.$undef_or_arrayref = $event->frameβ if a trace is present, and has a caller frame, this will be an arrayref:
[$package, $file, $line, $subname]
$undef_or_string = $event->trace_detailsβ usually this is justundef, but every once in a while it's a string, and that string overrides the file/line number debugging a trace normally hands you on a test failure. iykyk β¨$undef_or_string = $event->trace_packageβ literally the same as(caller())[0], the first element of the trace frame.undefif it isn't there π«Ά$undef_or_string = $event->trace_fileβ same as(caller())[1], the second element of the trace frame.undefif it isn't there π$undef_or_integer = $event->trace_lineβ same as(caller())[2], the third element of the trace frame.undefif it isn't there πΈ$undef_or_string = $event->trace_subname$undef_or_string = $event->trace_toolβ these two are aliases for the exact same thing, no cap. Same as(caller($level))[4], the fourth element of the trace frame.undefif it isn't there π$undef_or_string = $event->trace_signatureβ a string that's a unique signature for the trace. If one single context generates multiple events, they all get the same signature. So you can tie assertions and diagnostics that got sent as separate events back together after the fact β which is honestly iconic π¦
Heads up bestie: if the trace isn't present, or it has no caller frame, all of these come back undef. Not an error, just vibes π
π ASSERT FACET π
$bool = $event->has_assertβtrueif the event has an assert facet,falseif it doesn't. simple as that π$undef_or_hashref = $event->the_assertβ hands you the assert facet if present,undefif not β¨@list_of_facets = $event->assertβ TODOEMPTY_LIST_OR_STRING = $event->assert_briefβ returns a string giving a brief of the assertion when there is an assertion. Returns an empty list if there's no assertion π
π SUBTESTS (PARENT FACET) π
$bool = $event->has_subtestβtrueif a subetest is present in this event π$undef_or_hashref = $event->the_subtestβ get the one subtest if present, otherwiseundefπ«Ά@list_of_facets = $event->subtestβ TODOEMPTY_LIST_OR_OBJECT = $event->subtest_resultβ returns an empty list if there's no subtest. Otherwise you get an instance ofTest2::API::InterceptResultrepresenting the subtest π
π CONTROL FACET (BAILOUT, ENCODING) π
$bool = $event->has_bailoutβtrueif there was a bailout π$undef_hashref = $event->the_bailoutβ return the control facet if it requested a bailout π¦EMPTY_LIST_OR_HASHREF = $event->bailoutβ get a list of 0 or 1 hashrefs. That hashref is the control facet if a bail-out was requested π₯EMPTY_LIST_OR_STRING = $event->bailout_briefβ get the brief of the bailout if present β¨EMPTY_LIST_OR_STRING = $event->bailout_reasonβ get the reason for the bailout, an empty string if no reason was provided, or an empty list if there was no bailout π
β PLAN FACET β
TODO
$bool = $event->has_plan$undef_or_hashref = $event->the_plan@list_if_hashrefs = $event->planEMPTY_LIST_OR_STRING $event->plan_brief
π§ AMNESTY FACET (TODO AND SKIP) π§
TODO
$event->has_amnesty$event->the_amnesty$event->amnesty$event->amnesty_reasons$event->has_todos$event->todos$event->todo_reasons$event->has_skips$event->skips$event->skip_reasons$event->has_other_amnesty$event->other_amnesty$event->other_amnesty_reasons
π ERROR FACET (CAPTURED EXCEPTIONS) π
TODO
$event->has_errors$event->the_errors$event->errors$event->error_messages$event->error_brief
π INFO FACET (DIAG, NOTE) π
TODO
$event->has_info$event->the_info$event->info$event->info_messages$event->has_diags$event->diags$event->diag_messages$event->has_notes$event->notes$event->note_messages$event->has_other_info$event->other_info$event->other_info_messages
πΈ SOURCE πΈ
The source code repository for Test2 can be found at <https://github.com/Test-More/test-more/>. go star it bestie β
π MAINTAINERS π
Chad Granum <exodist@cpan.org>
β¨ AUTHORS β¨
Chad Granum <exodist@cpan.org>
π COPYRIGHT π
Copyright Chad Granum <exodist@cpan.org>.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
See <https://dev.perl.org/licenses/>
okay that's the whole page bestie, you made it to the end π«Ά this one's perl v5.42.3, dated 2026-11-03, and it's the Test2::...::Event(3pm) page β go forth and know your facets π
β¨
π¦ where this page comes from
- alpine 3.24: perl-doc 5.42.3-r0 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms