1 package Test2::Util::Facets2Legacy;
5 our $VERSION = '1.302185';
7 use Carp qw/croak confess/;
8 use Scalar::Util qw/blessed/;
23 our %EXPORT_TAGS = ( ALL => \@EXPORT_OK );
25 our $CYCLE_DETECT = 0;
29 if (blessed($in) && $in->isa('Test2::Event')) {
30 confess "Cycle between Facets2Legacy and $in\->facet_data() (Did you forget to override the facet_data() method?)"
33 local $CYCLE_DETECT = 1;
34 return $in->facet_data;
37 return $in if ref($in) eq 'HASH';
39 croak "'$in' Does not appear to be either a Test::Event or an EventFacet hashref";
43 my $facet_data = _get_facet_data(shift @_);
45 return 1 if $facet_data->{errors} && grep { $_->{fail} } @{$facet_data->{errors}};
47 if (my $control = $facet_data->{control}) {
48 return 1 if $control->{halt};
49 return 1 if $control->{terminate};
52 return 0 if $facet_data->{amnesty} && @{$facet_data->{amnesty}};
53 return 1 if $facet_data->{assert} && !$facet_data->{assert}->{pass};
58 my $facet_data = _get_facet_data(shift @_);
59 return 1 if $facet_data->{errors} && @{$facet_data->{errors}};
60 return 0 unless $facet_data->{info} && @{$facet_data->{info}};
61 return (grep { $_->{debug} } @{$facet_data->{info}}) ? 1 : 0;
65 my $facet_data = _get_facet_data(shift @_);
66 return 0 unless $facet_data->{control};
67 return $facet_data->{control}->{global};
70 sub increments_count {
71 my $facet_data = _get_facet_data(shift @_);
72 return $facet_data->{assert} ? 1 : 0;
76 my $facet_data = _get_facet_data(shift @_);
77 return 0 unless $facet_data->{about};
78 return $facet_data->{about}->{no_display};
82 my $facet_data = _get_facet_data(shift @_);
83 my $plan = $facet_data->{plan} or return;
84 my @out = ($plan->{count} || 0);
88 push @out => $plan->{details} if defined $plan->{details};
90 elsif ($plan->{none}) {
91 push @out => 'NO PLAN'
98 my $facet_data = _get_facet_data(shift @_);
99 return undef unless $facet_data->{parent};
100 return $facet_data->{parent}->{hid};
104 my $facet_data = _get_facet_data(shift @_);
105 return '' unless $facet_data->{about} && $facet_data->{about}->{details};
106 return $facet_data->{about}->{details};
110 my $facet_data = _get_facet_data(shift @_);
111 return undef unless $facet_data->{control};
112 return $facet_data->{control}->{terminate};
119 if (blessed($in) && $in->isa('Test2::Event')) {
120 my $meth = $in->can('uuid');
121 $meth = $in->can('SUPER::uuid') if $meth == \&uuid;
122 my $uuid = $in->$meth if $meth && $meth != \&uuid;
123 return $uuid if $uuid;
129 my $facet_data = _get_facet_data($in);
130 return $facet_data->{about}->{uuid} if $facet_data->{about} && $facet_data->{about}->{uuid};
143 Test2::Util::Facets2Legacy - Convert facet data to the legacy event API.
147 This module exports several subroutines from the older event API (see
148 L<Test2::Event>). These subroutines can be used as methods on any object that
149 provides a custom C<facet_data()> method. These subroutines can also be used as
150 functions that take a facet data hashref as arguments.
158 use Test2::Util::Facets2Legacy ':ALL';
160 sub facet_data { return { ... } }
164 my $e = My::Event->new(...);
166 my $causes_fail = $e->causes_fail;
167 my $summary = $e->summary;
172 use Test2::Util::Facets2Legacy ':ALL';
176 info => [{...}, ...],
181 my $causes_fail = causes_fail($f);
182 my $summary = summary($f);
184 =head1 NOTE ON CYCLES
186 When used as methods, all these subroutines call C<< $e->facet_data() >>. The
187 default C<facet_data()> method in L<Test2::Event> relies on the legacy methods
188 this module emulates in order to work. As a result of this it is very easy to
189 create infinite recursion bugs.
191 These methods have cycle detection and will throw an exception early if a cycle
192 is detected. C<uuid()> is currently the only subroutine in this library that
193 has a fallback behavior when cycles are detected.
197 Nothing is exported by default. You must specify which methods to import, or
202 =item $bool = $e->causes_fail()
204 =item $bool = causes_fail($f)
206 Check if the event or facets result in a failing state.
208 =item $bool = $e->diagnostics()
210 =item $bool = diagnostics($f)
212 Check if the event or facets contain any diagnostics information.
214 =item $bool = $e->global()
216 =item $bool = global($f)
218 Check if the event or facets need to be globally processed.
220 =item $bool = $e->increments_count()
222 =item $bool = increments_count($f)
224 Check if the event or facets make an assertion.
226 =item $bool = $e->no_display()
228 =item $bool = no_display($f)
230 Check if the event or facets should be rendered or hidden.
232 =item ($max, $directive, $reason) = $e->sets_plan()
234 =item ($max, $directive, $reason) = sets_plan($f)
236 Check if the event or facets set a plan, and return the plan details.
238 =item $id = $e->subtest_id()
240 =item $id = subtest_id($f)
242 Get the subtest id, if any.
244 =item $string = $e->summary()
246 =item $string = summary($f)
248 Get the summary of the event or facets hash, if any.
250 =item $undef_or_int = $e->terminate()
252 =item $undef_or_int = terminate($f)
254 Check if the event or facets should result in process termination, if so the
255 exit code is returned (which could be 0). undef is returned if no termination
258 =item $uuid = $e->uuid()
260 =item $uuid = uuid($f)
262 Get the UUID of the facets or event.
264 B<Note:> This will fall back to C<< $e->SUPER::uuid() >> if a cycle is
265 detected and an event is used as the argument.
271 The source code repository for Test2 can be found at
272 F<http://github.com/Test-More/test-more/>.
278 =item Chad Granum E<lt>exodist@cpan.orgE<gt>
286 =item Chad Granum E<lt>exodist@cpan.orgE<gt>
292 Copyright 2020 Chad Granum E<lt>exodist@cpan.orgE<gt>.
294 This program is free software; you can redistribute it and/or
295 modify it under the same terms as Perl itself.
297 See F<http://dev.perl.org/licenses/>