xref: /openbsd-src/gnu/usr.bin/perl/cpan/Test-Simple/lib/Test2/API/InterceptResult.pm (revision 5486feefcc8cb79b19e014ab332cc5dfd05b3b33)
1256a93a4Safresh1package Test2::API::InterceptResult;
2256a93a4Safresh1use strict;
3256a93a4Safresh1use warnings;
4256a93a4Safresh1
5*5486feefSafresh1our $VERSION = '1.302199';
6256a93a4Safresh1
7256a93a4Safresh1use Scalar::Util qw/blessed/;
8256a93a4Safresh1use Test2::Util  qw/pkg_to_file/;
9256a93a4Safresh1use Storable     qw/dclone/;
10256a93a4Safresh1use Carp         qw/croak/;
11256a93a4Safresh1
12256a93a4Safresh1use Test2::API::InterceptResult::Squasher;
13256a93a4Safresh1use Test2::API::InterceptResult::Event;
14256a93a4Safresh1use Test2::API::InterceptResult::Hub;
15256a93a4Safresh1
16256a93a4Safresh1sub new {
17256a93a4Safresh1    croak "Called a method that creates a new instance in void context" unless defined wantarray;
18256a93a4Safresh1    my $class = shift;
19256a93a4Safresh1    bless([@_], $class);
20256a93a4Safresh1}
21256a93a4Safresh1
22256a93a4Safresh1sub new_from_ref {
23256a93a4Safresh1    croak "Called a method that creates a new instance in void context" unless defined wantarray;
24256a93a4Safresh1    bless($_[1], $_[0]);
25256a93a4Safresh1}
26256a93a4Safresh1
27256a93a4Safresh1sub clone { blessed($_[0])->new(@{dclone($_[0])}) }
28256a93a4Safresh1
29256a93a4Safresh1sub event_list { @{$_[0]} }
30256a93a4Safresh1
31256a93a4Safresh1sub _upgrade {
32256a93a4Safresh1    my $self = shift;
33256a93a4Safresh1    my ($event, %params) = @_;
34256a93a4Safresh1
35256a93a4Safresh1    my $blessed = blessed($event);
36256a93a4Safresh1
37256a93a4Safresh1    my $upgrade_class = $params{upgrade_class} ||= 'Test2::API::InterceptResult::Event';
38256a93a4Safresh1
39256a93a4Safresh1    return $event if $blessed && $event->isa($upgrade_class) && !$params{_upgrade_clone};
40256a93a4Safresh1
41256a93a4Safresh1    my $fd = dclone($blessed ? $event->facet_data : $event);
42256a93a4Safresh1
43256a93a4Safresh1    my $class = $params{result_class} ||= blessed($self);
44256a93a4Safresh1
45256a93a4Safresh1    if (my $parent = $fd->{parent}) {
46256a93a4Safresh1        $parent->{children} = $class->new_from_ref($parent->{children} || [])->upgrade(%params);
47256a93a4Safresh1    }
48256a93a4Safresh1
49256a93a4Safresh1    my $uc_file = pkg_to_file($upgrade_class);
50256a93a4Safresh1    require($uc_file) unless $INC{$uc_file};
51256a93a4Safresh1    return $upgrade_class->new(facet_data => $fd, result_class => $class);
52256a93a4Safresh1}
53256a93a4Safresh1
54256a93a4Safresh1sub hub {
55256a93a4Safresh1    my $self = shift;
56256a93a4Safresh1
57256a93a4Safresh1    my $hub = Test2::API::InterceptResult::Hub->new();
58256a93a4Safresh1    $hub->process($_) for @$self;
59256a93a4Safresh1    $hub->set_ended(1);
60256a93a4Safresh1
61256a93a4Safresh1    return $hub;
62256a93a4Safresh1}
63256a93a4Safresh1
64256a93a4Safresh1sub state {
65256a93a4Safresh1    my $self = shift;
66256a93a4Safresh1    my %params = @_;
67256a93a4Safresh1
68256a93a4Safresh1    my $hub = $self->hub;
69256a93a4Safresh1
70256a93a4Safresh1    my $out = {
71256a93a4Safresh1        map {($_ => scalar $hub->$_)} qw/count failed is_passing plan bailed_out skip_reason/
72256a93a4Safresh1    };
73256a93a4Safresh1
74256a93a4Safresh1    $out->{bailed_out} = $self->_upgrade($out->{bailed_out}, %params)->bailout_reason || 1
75256a93a4Safresh1        if $out->{bailed_out};
76256a93a4Safresh1
77256a93a4Safresh1    $out->{follows_plan} = $hub->check_plan;
78256a93a4Safresh1
79256a93a4Safresh1    return $out;
80256a93a4Safresh1}
81256a93a4Safresh1
82256a93a4Safresh1sub upgrade {
83256a93a4Safresh1    my $self = shift;
84256a93a4Safresh1    my %params = @_;
85256a93a4Safresh1
86256a93a4Safresh1    my @out = map { $self->_upgrade($_, %params, _upgrade_clone => 1) } @$self;
87256a93a4Safresh1
88256a93a4Safresh1    return blessed($self)->new_from_ref(\@out)
89256a93a4Safresh1        unless $params{in_place};
90256a93a4Safresh1
91256a93a4Safresh1    @$self = @out;
92256a93a4Safresh1    return $self;
93256a93a4Safresh1}
94256a93a4Safresh1
95256a93a4Safresh1sub squash_info {
96256a93a4Safresh1    my $self = shift;
97256a93a4Safresh1    my %params = @_;
98256a93a4Safresh1
99256a93a4Safresh1    my @out;
100256a93a4Safresh1
101256a93a4Safresh1    {
102256a93a4Safresh1        my $squasher = Test2::API::InterceptResult::Squasher->new(events => \@out);
103256a93a4Safresh1        # Clone to make sure we do not indirectly modify an existing one if it
104256a93a4Safresh1        # is already upgraded
105256a93a4Safresh1        $squasher->process($self->_upgrade($_, %params)->clone) for @$self;
106256a93a4Safresh1        $squasher->flush_down();
107256a93a4Safresh1    }
108256a93a4Safresh1
109256a93a4Safresh1    return blessed($self)->new_from_ref(\@out)
110256a93a4Safresh1        unless $params{in_place};
111256a93a4Safresh1
112256a93a4Safresh1    @$self = @out;
113256a93a4Safresh1    return $self;
114256a93a4Safresh1}
115256a93a4Safresh1
116256a93a4Safresh1sub asserts        { shift->grep(has_assert     => @_) }
117256a93a4Safresh1sub subtests       { shift->grep(has_subtest    => @_) }
118256a93a4Safresh1sub diags          { shift->grep(has_diags      => @_) }
119256a93a4Safresh1sub notes          { shift->grep(has_notes      => @_) }
120256a93a4Safresh1sub errors         { shift->grep(has_errors     => @_) }
121256a93a4Safresh1sub plans          { shift->grep(has_plan       => @_) }
122256a93a4Safresh1sub causes_fail    { shift->grep(causes_fail    => @_) }
123256a93a4Safresh1sub causes_failure { shift->grep(causes_failure => @_) }
124256a93a4Safresh1
125256a93a4Safresh1sub flatten         { shift->map(flatten        => @_) }
126256a93a4Safresh1sub briefs          { shift->map(brief          => @_) }
127256a93a4Safresh1sub summaries       { shift->map(summary        => @_) }
128256a93a4Safresh1sub subtest_results { shift->map(subtest_result => @_) }
129256a93a4Safresh1sub diag_messages   { shift->map(diag_messages  => @_) }
130256a93a4Safresh1sub note_messages   { shift->map(note_messages  => @_) }
131256a93a4Safresh1sub error_messages  { shift->map(error_messages => @_) }
132256a93a4Safresh1
133256a93a4Safresh1no warnings 'once';
134256a93a4Safresh1
135256a93a4Safresh1*map = sub {
136256a93a4Safresh1    my $self = shift;
137256a93a4Safresh1    my ($call, %params) = @_;
138256a93a4Safresh1
139256a93a4Safresh1    my $args = $params{args} ||= [];
140256a93a4Safresh1
141256a93a4Safresh1    return [map { local $_ = $self->_upgrade($_, %params); $_->$call(@$args) } @$self];
142256a93a4Safresh1};
143256a93a4Safresh1
144256a93a4Safresh1*grep = sub {
145256a93a4Safresh1    my $self = shift;
146256a93a4Safresh1    my ($call, %params) = @_;
147256a93a4Safresh1
148256a93a4Safresh1    my $args = $params{args} ||= [];
149256a93a4Safresh1
150256a93a4Safresh1    my @out = grep { local $_ = $self->_upgrade($_, %params); $_->$call(@$args) } @$self;
151256a93a4Safresh1
152256a93a4Safresh1    return blessed($self)->new_from_ref(\@out)
153256a93a4Safresh1        unless $params{in_place};
154256a93a4Safresh1
155256a93a4Safresh1    @$self = @out;
156256a93a4Safresh1    return $self;
157256a93a4Safresh1};
158256a93a4Safresh1
159256a93a4Safresh11;
160256a93a4Safresh1
161256a93a4Safresh1__END__
162256a93a4Safresh1
163256a93a4Safresh1=pod
164256a93a4Safresh1
165256a93a4Safresh1=encoding UTF-8
166256a93a4Safresh1
167256a93a4Safresh1=head1 NAME
168256a93a4Safresh1
169256a93a4Safresh1Test2::API::InterceptResult - Representation of a list of events.
170256a93a4Safresh1
171256a93a4Safresh1=head1 DESCRIPTION
172256a93a4Safresh1
173256a93a4Safresh1This class represents a list of events, normally obtained using C<intercept()>
174256a93a4Safresh1from L<Test2::API>.
175256a93a4Safresh1
176256a93a4Safresh1This class is intended for people who with to verify the results of test tools
177256a93a4Safresh1they write.
178256a93a4Safresh1
179256a93a4Safresh1This class provides methods to normalize, summarize, or map the list of events.
180256a93a4Safresh1The output of these operations makes verifying your testing tools and the
181256a93a4Safresh1events they generate significantly easier. In most cases this spares you from
182256a93a4Safresh1needing a deep understanding of the event/facet model.
183256a93a4Safresh1
184256a93a4Safresh1=head1 SYNOPSIS
185256a93a4Safresh1
186256a93a4Safresh1Usually you get an instance of this class when you use C<intercept()> from
187256a93a4Safresh1L<Test2::API>.
188256a93a4Safresh1
189256a93a4Safresh1    use Test2::V0;
190256a93a4Safresh1    use Test2::API qw/intercept/;
191256a93a4Safresh1
192256a93a4Safresh1    my $events = intercept {
193256a93a4Safresh1        ok(1, "pass");
194256a93a4Safresh1        ok(0, "fail");
195256a93a4Safresh1        todo "broken" => sub { ok(0, "fixme") };
196256a93a4Safresh1        plan 3;
197256a93a4Safresh1    };
198256a93a4Safresh1
199256a93a4Safresh1    # This is typically the most useful construct
200256a93a4Safresh1    # squash_info() merges assertions and diagnostics that are associated
201256a93a4Safresh1    #   (and returns a new instance with the modifications)
202256a93a4Safresh1    # flatten() condenses the facet data into the key details for each event
203256a93a4Safresh1    #   (and returns those structures in an arrayref)
204256a93a4Safresh1    is(
205256a93a4Safresh1        $events->squash_info->flatten(),
206256a93a4Safresh1        [
207256a93a4Safresh1            {
208256a93a4Safresh1                causes_failure => 0,
209256a93a4Safresh1
210256a93a4Safresh1                name => 'pass',
211256a93a4Safresh1                pass => 1,
212256a93a4Safresh1
213256a93a4Safresh1                trace_file => 'xxx.t',
214256a93a4Safresh1                trace_line => 5,
215256a93a4Safresh1            },
216256a93a4Safresh1            {
217256a93a4Safresh1                causes_failure => 1,
218256a93a4Safresh1
219256a93a4Safresh1                name => 'fail',
220256a93a4Safresh1                pass => 0,
221256a93a4Safresh1
222256a93a4Safresh1                trace_file => 'xxx.t',
223256a93a4Safresh1                trace_line => 6,
224256a93a4Safresh1
225256a93a4Safresh1                # There can be more than one diagnostics message so this is
226256a93a4Safresh1                # always an array when present.
227256a93a4Safresh1                diag => ["Failed test 'fail'\nat xxx.t line 6."],
228256a93a4Safresh1            },
229256a93a4Safresh1            {
230256a93a4Safresh1                causes_failure => 0,
231256a93a4Safresh1
232256a93a4Safresh1                name => 'fixme',
233256a93a4Safresh1                pass => 0,
234256a93a4Safresh1
235256a93a4Safresh1                trace_file => 'xxx.t',
236256a93a4Safresh1                trace_line => 7,
237256a93a4Safresh1
238256a93a4Safresh1                # There can be more than one diagnostics message or todo
239256a93a4Safresh1                # reason, so these are always an array when present.
240256a93a4Safresh1                todo => ['broken'],
241256a93a4Safresh1
242256a93a4Safresh1                # Diag message was turned into a note since the assertion was
243256a93a4Safresh1                # TODO
244256a93a4Safresh1                note => ["Failed test 'fixme'\nat xxx.t line 7."],
245256a93a4Safresh1            },
246256a93a4Safresh1            {
247256a93a4Safresh1                causes_failure => 0,
248256a93a4Safresh1
249256a93a4Safresh1                plan => 3,
250256a93a4Safresh1
251256a93a4Safresh1                trace_file => 'xxx.t',
252256a93a4Safresh1                trace_line => 8,
253256a93a4Safresh1            },
254256a93a4Safresh1        ],
255256a93a4Safresh1        "Flattened events look like we expect"
256256a93a4Safresh1    );
257256a93a4Safresh1
258256a93a4Safresh1See L<Test2::API::InterceptResult::Event> for a full description of what
259256a93a4Safresh1C<flatten()> provides for each event.
260256a93a4Safresh1
261256a93a4Safresh1=head1 METHODS
262256a93a4Safresh1
263256a93a4Safresh1Please note that no methods modify the original instance unless asked to do so.
264256a93a4Safresh1
265256a93a4Safresh1=head2 CONSTRUCTION
266256a93a4Safresh1
267256a93a4Safresh1=over 4
268256a93a4Safresh1
269256a93a4Safresh1=item $events = Test2::API::InterceptResult->new(@EVENTS)
270256a93a4Safresh1
271256a93a4Safresh1=item $events = Test2::API::InterceptResult->new_from_ref(\@EVENTS)
272256a93a4Safresh1
273256a93a4Safresh1These create a new instance of Test2::API::InterceptResult from the given
274256a93a4Safresh1events.
275256a93a4Safresh1
276256a93a4Safresh1In the first form a new blessed arrayref is returned. In the 'new_from_ref'
277256a93a4Safresh1form the reference you pass in is directly blessed.
278256a93a4Safresh1
279256a93a4Safresh1Both of these will throw an exception if called in void context. This is mainly
280256a93a4Safresh1important for the 'filtering' methods listed below which normally return a new
281256a93a4Safresh1instance, they throw an exception in such cases as it probably means someone
282256a93a4Safresh1meant to filter the original in place.
283256a93a4Safresh1
284256a93a4Safresh1=item $clone = $events->clone()
285256a93a4Safresh1
286256a93a4Safresh1Make a clone of the original events. Note that this is a deep copy, the entire
287256a93a4Safresh1structure is duplicated. This uses C<dclone> from L<Storable> to achieve the
288256a93a4Safresh1deep clone.
289256a93a4Safresh1
290256a93a4Safresh1=back
291256a93a4Safresh1
292256a93a4Safresh1=head2 NORMALIZATION
293256a93a4Safresh1
294256a93a4Safresh1=over 4
295256a93a4Safresh1
296256a93a4Safresh1=item @events = $events->event_list
297256a93a4Safresh1
298256a93a4Safresh1This returns all the events in list-form.
299256a93a4Safresh1
300256a93a4Safresh1=item $hub = $events->hub
301256a93a4Safresh1
302256a93a4Safresh1This returns a new L<Test2::Hub> instance that has processed all the events
303256a93a4Safresh1contained in the instance. This gives you a simple way to inspect the state
304256a93a4Safresh1changes your events cause.
305256a93a4Safresh1
306256a93a4Safresh1=item $state = $events->state
307256a93a4Safresh1
308256a93a4Safresh1This returns a summary of the state of a hub after processing all the events.
309256a93a4Safresh1
310256a93a4Safresh1    {
311256a93a4Safresh1        count        => 2,      # Number of assertions made
312256a93a4Safresh1        failed       => 1,      # Number of test failures seen
313256a93a4Safresh1        is_passing   => 0,      # Boolean, true if the test would be passing
314256a93a4Safresh1                                # after the events are processed.
315256a93a4Safresh1
316256a93a4Safresh1        plan         => 2,      # Plan, either a number, undef, 'SKIP', or 'NO PLAN'
317256a93a4Safresh1        follows_plan => 1,      # True if there is a plan and it was followed.
318256a93a4Safresh1                                # False if the plan and assertions did not
319256a93a4Safresh1                                # match, undef if no plan was present in the
320256a93a4Safresh1                                # event list.
321256a93a4Safresh1
322256a93a4Safresh1        bailed_out   => undef,  # undef unless there was a bail-out in the
323256a93a4Safresh1                                # events in which case this will be a string
324256a93a4Safresh1                                # explaining why there was a bailout, if no
325256a93a4Safresh1                                # reason was given this will simply be set to
326256a93a4Safresh1                                # true (1).
327256a93a4Safresh1
328256a93a4Safresh1        skip_reason  => undef,  # If there was a skip_all this will give the
329256a93a4Safresh1                                # reason.
330256a93a4Safresh1    }
331256a93a4Safresh1
332256a93a4Safresh1
333256a93a4Safresh1=item $new = $events->upgrade
334256a93a4Safresh1
335256a93a4Safresh1=item $events->upgrade(in_place => $BOOL)
336256a93a4Safresh1
337256a93a4Safresh1B<Note:> This normally returns a new instance, leaving the original unchanged.
338256a93a4Safresh1If you call it in void context it will throw an exception. If you want to
339256a93a4Safresh1modify the original you must pass in the C<< in_place => 1 >> option. You may
340256a93a4Safresh1call this in void context when you ask to modify it in place. The in-place form
341256a93a4Safresh1returns the instance that was modified so you can chain methods.
342256a93a4Safresh1
343256a93a4Safresh1This will create a clone of the list where all events have been converted into
344256a93a4Safresh1L<Test2::API::InterceptResult::Event> instances. This is extremely helpful as
345256a93a4Safresh1L<Test2::API::InterceptResult::Event> provide a much better interface for
346256a93a4Safresh1working with events. This allows you to avoid thinking about legacy event
347256a93a4Safresh1types.
348256a93a4Safresh1
349256a93a4Safresh1This also means your tests against the list are not fragile if the tool
350256a93a4Safresh1you are testing randomly changes what type of events it generates (IE Changing
351256a93a4Safresh1from L<Test2::Event::Ok> to L<Test2::Event::Pass>, both make assertions and
352256a93a4Safresh1both will normalize to identical (or close enough)
353256a93a4Safresh1L<Test2::API::InterceptResult::Event> instances.
354256a93a4Safresh1
355256a93a4Safresh1Really you almost always want this, the only reason it is not done
356256a93a4Safresh1automatically is to make sure the C<intercept()> tool is backwards compatible.
357256a93a4Safresh1
358256a93a4Safresh1=item $new = $events->squash_info
359256a93a4Safresh1
360256a93a4Safresh1=item $events->squash_info(in_place => $BOOL)
361256a93a4Safresh1
362256a93a4Safresh1B<Note:> This normally returns a new instance, leaving the original unchanged.
363256a93a4Safresh1If you call it in void context it will throw an exception. If you want to
364256a93a4Safresh1modify the original you must pass in the C<< in_place => 1 >> option. You may
365256a93a4Safresh1call this in void context when you ask to modify it in place. The in-place form
366256a93a4Safresh1returns the instance that was modified so you can chain methods.
367256a93a4Safresh1
368256a93a4Safresh1B<Note:> All events in the new or modified instance will be converted to
369256a93a4Safresh1L<Test2::API::InterceptResult::Event> instances. There is no way to avoid this,
370256a93a4Safresh1the squash operation requires the upgraded event class.
371256a93a4Safresh1
372256a93a4Safresh1L<Test::More> and many other legacy tools would send notes, diags, and
373*5486feefSafresh1assertions as separate events. A subtest in L<Test::More> would send a note
374256a93a4Safresh1with the subtest name, the subtest assertion, and finally a diagnostics event
375256a93a4Safresh1if the subtest failed. This method will normalize things by squashing the note
376256a93a4Safresh1and diag into the same event as the subtest (This is different from putting
377256a93a4Safresh1them into the subtest, which is not what happens).
378256a93a4Safresh1
379256a93a4Safresh1=back
380256a93a4Safresh1
381256a93a4Safresh1=head2 FILTERING
382256a93a4Safresh1
383256a93a4Safresh1B<Note:> These normally return new instances, leaving the originals unchanged.
384256a93a4Safresh1If you call them in void context they will throw exceptions. If you want to
385256a93a4Safresh1modify the originals you must pass in the C<< in_place => 1 >> option. You may
386256a93a4Safresh1call these in void context when you ask to modify them in place. The in-place
387256a93a4Safresh1forms return the instance that was modified so you can chain methods.
388256a93a4Safresh1
389256a93a4Safresh1=head3 %PARAMS
390256a93a4Safresh1
391256a93a4Safresh1These all accept the same 2 optional parameters:
392256a93a4Safresh1
393256a93a4Safresh1=over 4
394256a93a4Safresh1
395256a93a4Safresh1=item in_place => $BOOL
396256a93a4Safresh1
397256a93a4Safresh1When true the method will modify the instance in place instead of returning a
398256a93a4Safresh1new instance.
399256a93a4Safresh1
400256a93a4Safresh1=item args => \@ARGS
401256a93a4Safresh1
402256a93a4Safresh1If you wish to pass parameters into the event method being used for filtering,
403256a93a4Safresh1you may do so here.
404256a93a4Safresh1
405256a93a4Safresh1=back
406256a93a4Safresh1
407256a93a4Safresh1=head3 METHODS
408256a93a4Safresh1
409256a93a4Safresh1=over 4
410256a93a4Safresh1
411256a93a4Safresh1=item $events->grep($CALL, %PARAMS)
412256a93a4Safresh1
413256a93a4Safresh1This is essentially:
414256a93a4Safresh1
415256a93a4Safresh1    Test2::API::InterceptResult->new(
416256a93a4Safresh1        grep { $_->$CALL( @{$PARAMS{args}} ) } $self->event_list,
417256a93a4Safresh1    );
418256a93a4Safresh1
419256a93a4Safresh1B<Note:> that $CALL is called on an upgraded version of the event, though
420256a93a4Safresh1the events returned will be the original ones, not the upgraded ones.
421256a93a4Safresh1
422256a93a4Safresh1$CALL may be either the name of a method on
423256a93a4Safresh1L<Test2::API::InterceptResult::Event>, or a coderef.
424256a93a4Safresh1
425256a93a4Safresh1=item $events->asserts(%PARAMS)
426256a93a4Safresh1
427256a93a4Safresh1This is essentially:
428256a93a4Safresh1
429256a93a4Safresh1    $events->grep(has_assert => @{$PARAMS{args}})
430256a93a4Safresh1
431256a93a4Safresh1It returns a new instance containing only the events that made assertions.
432256a93a4Safresh1
433256a93a4Safresh1=item $events->subtests(%PARAMS)
434256a93a4Safresh1
435256a93a4Safresh1This is essentially:
436256a93a4Safresh1
437256a93a4Safresh1    $events->grep(has_subtest => @{$PARAMS{args}})
438256a93a4Safresh1
439256a93a4Safresh1It returns a new instance containing only the events that have subtests.
440256a93a4Safresh1
441256a93a4Safresh1=item $events->diags(%PARAMS)
442256a93a4Safresh1
443256a93a4Safresh1This is essentially:
444256a93a4Safresh1
445256a93a4Safresh1    $events->grep(has_diags => @{$PARAMS{args}})
446256a93a4Safresh1
447256a93a4Safresh1It returns a new instance containing only the events that have diags.
448256a93a4Safresh1
449256a93a4Safresh1=item $events->notes(%PARAMS)
450256a93a4Safresh1
451256a93a4Safresh1This is essentially:
452256a93a4Safresh1
453256a93a4Safresh1    $events->grep(has_notes => @{$PARAMS{args}})
454256a93a4Safresh1
455256a93a4Safresh1It returns a new instance containing only the events that have notes.
456256a93a4Safresh1
457256a93a4Safresh1=item $events->errors(%PARAMS)
458256a93a4Safresh1
459256a93a4Safresh1B<Note:> Errors are NOT failing assertions. Failing assertions are a different
460256a93a4Safresh1thing.
461256a93a4Safresh1
462256a93a4Safresh1This is essentially:
463256a93a4Safresh1
464256a93a4Safresh1    $events->grep(has_errors => @{$PARAMS{args}})
465256a93a4Safresh1
466256a93a4Safresh1It returns a new instance containing only the events that have errors.
467256a93a4Safresh1
468256a93a4Safresh1=item $events->plans(%PARAMS)
469256a93a4Safresh1
470256a93a4Safresh1This is essentially:
471256a93a4Safresh1
472256a93a4Safresh1    $events->grep(has_plan => @{$PARAMS{args}})
473256a93a4Safresh1
474256a93a4Safresh1It returns a new instance containing only the events that set the plan.
475256a93a4Safresh1
476256a93a4Safresh1=item $events->causes_fail(%PARAMS)
477256a93a4Safresh1
478256a93a4Safresh1=item $events->causes_failure(%PARAMS)
479256a93a4Safresh1
480256a93a4Safresh1These are essentially:
481256a93a4Safresh1
482256a93a4Safresh1    $events->grep(causes_fail    => @{$PARAMS{args}})
483256a93a4Safresh1    $events->grep(causes_failure => @{$PARAMS{args}})
484256a93a4Safresh1
485256a93a4Safresh1B<Note:> C<causes_fail()> and C<causes_failure()> are both aliases for
486256a93a4Safresh1eachother in events, so these methods are effectively aliases here as well.
487256a93a4Safresh1
488256a93a4Safresh1It returns a new instance containing only the events that cause failure.
489256a93a4Safresh1
490256a93a4Safresh1=back
491256a93a4Safresh1
492256a93a4Safresh1=head2 MAPPING
493256a93a4Safresh1
494256a93a4Safresh1These methods B<ALWAYS> return an arrayref.
495256a93a4Safresh1
496256a93a4Safresh1B<Note:> No methods on L<Test2::API::InterceptResult::Event> alter the event in
497256a93a4Safresh1any way.
498256a93a4Safresh1
499256a93a4Safresh1B<Important Notes about Events>:
500256a93a4Safresh1
501256a93a4Safresh1L<Test2::API::InterceptResult::Event> was tailor-made to be used in
502256a93a4Safresh1event-lists. Most methods that are not applicable to a given event will return
503256a93a4Safresh1an empty list, so you normally do not need to worry about unwanted C<undef>
504256a93a4Safresh1values or exceptions being thrown. Mapping over event methods is an entended
505256a93a4Safresh1use, so it works well to produce lists.
506256a93a4Safresh1
507256a93a4Safresh1B<Exceptions to the rule:>
508256a93a4Safresh1
509256a93a4Safresh1Some methods such as C<causes_fail> always return a boolean true or false for
510256a93a4Safresh1all events. Any method prefixed with C<the_> conveys the intent that the event
511256a93a4Safresh1should have exactly 1 of something, so those will throw an exception when that
512256a93a4Safresh1condition is not true.
513256a93a4Safresh1
514256a93a4Safresh1=over 4
515256a93a4Safresh1
516256a93a4Safresh1=item $arrayref = $events->map($CALL, %PARAMS)
517256a93a4Safresh1
518256a93a4Safresh1This is essentially:
519256a93a4Safresh1
520256a93a4Safresh1    [ map { $_->$CALL(@{ $PARAMS{args} }) } $events->upgrade->event_list ];
521256a93a4Safresh1
522256a93a4Safresh1$CALL may be either the name of a method on
523256a93a4Safresh1L<Test2::API::InterceptResult::Event>, or a coderef.
524256a93a4Safresh1
525256a93a4Safresh1=item $arrayref = $events->flatten(%PARAMS)
526256a93a4Safresh1
527256a93a4Safresh1This is essentially:
528256a93a4Safresh1
529256a93a4Safresh1    [ map { $_->flatten(@{ $PARAMS{args} }) } $events->upgrade->event_list ];
530256a93a4Safresh1
531256a93a4Safresh1It returns a new list of flattened structures.
532256a93a4Safresh1
533256a93a4Safresh1See L<Test2::API::InterceptResult::Event> for details on what C<flatten()>
534256a93a4Safresh1returns.
535256a93a4Safresh1
536256a93a4Safresh1=item $arrayref = $events->briefs(%PARAMS)
537256a93a4Safresh1
538256a93a4Safresh1This is essentially:
539256a93a4Safresh1
540256a93a4Safresh1    [ map { $_->briefs(@{ $PARAMS{args} }) } $events->upgrade->event_list ];
541256a93a4Safresh1
542256a93a4Safresh1It returns a new list of event briefs.
543256a93a4Safresh1
544256a93a4Safresh1See L<Test2::API::InterceptResult::Event> for details on what C<brief()>
545256a93a4Safresh1returns.
546256a93a4Safresh1
547256a93a4Safresh1=item $arrayref = $events->summaries(%PARAMS)
548256a93a4Safresh1
549256a93a4Safresh1This is essentially:
550256a93a4Safresh1
551256a93a4Safresh1    [ map { $_->summaries(@{ $PARAMS{args} }) } $events->upgrade->event_list ];
552256a93a4Safresh1
553256a93a4Safresh1It returns a new list of event summaries.
554256a93a4Safresh1
555256a93a4Safresh1See L<Test2::API::InterceptResult::Event> for details on what C<summary()>
556256a93a4Safresh1returns.
557256a93a4Safresh1
558256a93a4Safresh1=item $arrayref = $events->subtest_results(%PARAMS)
559256a93a4Safresh1
560256a93a4Safresh1This is essentially:
561256a93a4Safresh1
562256a93a4Safresh1    [ map { $_->subtest_result(@{ $PARAMS{args} }) } $events->upgrade->event_list ];
563256a93a4Safresh1
564256a93a4Safresh1It returns a new list of event summaries.
565256a93a4Safresh1
566256a93a4Safresh1See L<Test2::API::InterceptResult::Event> for details on what
567256a93a4Safresh1C<subtest_result()> returns.
568256a93a4Safresh1
569256a93a4Safresh1=item $arrayref = $events->diag_messages(%PARAMS)
570256a93a4Safresh1
571256a93a4Safresh1This is essentially:
572256a93a4Safresh1
573256a93a4Safresh1    [ map { $_->diag_messages(@{ $PARAMS{args} }) } $events->upgrade->event_list ];
574256a93a4Safresh1
575256a93a4Safresh1It returns a new list of diagnostic messages (strings).
576256a93a4Safresh1
577256a93a4Safresh1See L<Test2::API::InterceptResult::Event> for details on what
578256a93a4Safresh1C<diag_messages()> returns.
579256a93a4Safresh1
580256a93a4Safresh1=item $arrayref = $events->note_messages(%PARAMS)
581256a93a4Safresh1
582256a93a4Safresh1This is essentially:
583256a93a4Safresh1
584256a93a4Safresh1    [ map { $_->note_messages(@{ $PARAMS{args} }) } $events->upgrade->event_list ];
585256a93a4Safresh1
586256a93a4Safresh1It returns a new list of notification messages (strings).
587256a93a4Safresh1
588256a93a4Safresh1See L<Test2::API::InterceptResult::Event> for details on what
589256a93a4Safresh1C<note_messages()> returns.
590256a93a4Safresh1
591256a93a4Safresh1=item $arrayref = $events->error_messages(%PARAMS)
592256a93a4Safresh1
593256a93a4Safresh1This is essentially:
594256a93a4Safresh1
595256a93a4Safresh1    [ map { $_->error_messages(@{ $PARAMS{args} }) } $events->upgrade->event_list ];
596256a93a4Safresh1
597256a93a4Safresh1It returns a new list of error messages (strings).
598256a93a4Safresh1
599256a93a4Safresh1See L<Test2::API::InterceptResult::Event> for details on what
600256a93a4Safresh1C<error_messages()> returns.
601256a93a4Safresh1
602256a93a4Safresh1=back
603256a93a4Safresh1
604256a93a4Safresh1=head1 SOURCE
605256a93a4Safresh1
606256a93a4Safresh1The source code repository for Test2 can be found at
607*5486feefSafresh1L<https://github.com/Test-More/test-more/>.
608256a93a4Safresh1
609256a93a4Safresh1=head1 MAINTAINERS
610256a93a4Safresh1
611256a93a4Safresh1=over 4
612256a93a4Safresh1
613256a93a4Safresh1=item Chad Granum E<lt>exodist@cpan.orgE<gt>
614256a93a4Safresh1
615256a93a4Safresh1=back
616256a93a4Safresh1
617256a93a4Safresh1=head1 AUTHORS
618256a93a4Safresh1
619256a93a4Safresh1=over 4
620256a93a4Safresh1
621256a93a4Safresh1=item Chad Granum E<lt>exodist@cpan.orgE<gt>
622256a93a4Safresh1
623256a93a4Safresh1=back
624256a93a4Safresh1
625256a93a4Safresh1=head1 COPYRIGHT
626256a93a4Safresh1
627256a93a4Safresh1Copyright 2020 Chad Granum E<lt>exodist@cpan.orgE<gt>.
628256a93a4Safresh1
629256a93a4Safresh1This program is free software; you can redistribute it and/or
630256a93a4Safresh1modify it under the same terms as Perl itself.
631256a93a4Safresh1
632*5486feefSafresh1See L<https://dev.perl.org/licenses/>
633256a93a4Safresh1
634256a93a4Safresh1=cut
635