blib/lib/Test2/Util/Facets2Legacy.pm

filter: Statementstmt 100% 81 / 81 Branchbran 91.0% 51 / 56 Conditioncond 59.3% 19 / 32 MC/DCmcdc 38.1% 8 / 21 Subroutinesub 100% 16 / 16 Podpod 100% 10 / 10 totaltotal 85.6% 185 / 216 CC 40 SCAR 19.1
CC
40
Cov
89%
uuid
14.1
causes_fail
10.1
diagnostics
6.0
CRAP 6.8
« blib/lib/Test2/Compare/Bag.pm ↑ Summary blib/lib/Test2/Workflow/Build.pm »
1 package Test2::Util::Facets2Legacy;
2 455 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 455 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 455 our $VERSION = '1.302226';
6  
7 455 ▶use Carp qw/croak confess/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 455 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 455 ▶BEGIN { require Exporter; our @ISA = qw(Exporter) }
Subroutine BEGIN: called CC=1 SCAR=0.0
11 455 our @EXPORT_OK = qw{
12 causes_fail
13 diagnostics
14 global
15 increments_count
16 no_display
17 sets_plan
18 subtest_id
19 summary
20 terminate
21 uuid
22 };
23 455 our %EXPORT_TAGS = ( ALL => \@EXPORT_OK );
24  
25 455 our $CYCLE_DETECT = 0;
26 84 ▶sub _get_facet_data {
Subroutine _get_facet_data: called CC=5 SCAR=16.1
27 84 my $in = shift;
28  
29 84 ▶ if (blessed($in) && $in->isa('Test2::Event')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($in) and $in->isa("Test2::Event")) 49 35 84
Condition66%2/3
builtin::blessed($in) and $in->isa("Test2::Event")
!ll&&!rl&&r
35049
MC/DC50%1/2
builtin::blessed($in) and $in->isa("Test2::Event")
builtin::blessed($in) $in->isa("Test2::Event")
Truth table66%2/3
A and B
A= builtin::blessed($in)B= $in->isa("Test2::Event")
ABresult
0X0
100
111
30 49 ▶ confess "Cycle between Facets2Legacy and $in\->facet_data() (Did you forget to override the facet_data() method?)"
Branch: 1 branch
BranchTrueFalseTotal
if $Test2::Util::Facets2Legacy::CYCLE_DETECT 1 48 49
31 if $CYCLE_DETECT;
32  
33 48 local $CYCLE_DETECT = 1;
34 48 return $in->facet_data;
35 }
36  
37 35 ▶ return $in if ref($in) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $in eq "HASH" 34 1 35
38  
39 1 croak "'$in' Does not appear to be either a Test::Event or an EventFacet hashref";
40 }
41  
42 9 ▶sub causes_fail {
Subroutine causes_fail: called CC=10 SCAR=23.1
Pod documented
43 9 my $facet_data = _get_facet_data(shift @_);
44  
45 7 ▶ return 1 if $facet_data->{errors} && grep { $_->{fail} } @{$facet_data->{errors}};
Branch: 1 branch
BranchTrueFalseTotal
if $facet_data->{"errors"} and grep {$_->{"fail"};} @{$$facet_data{"errors"};} 1 6 7
Condition66%2/3
$facet_data->{"errors"} and grep {$_->{"fail"};} @{$$facet_data{"errors"};}
!ll&&!rl&&r
601
MC/DC50%1/2
$facet_data->{"errors"} and grep {$_->{"fail"};} @{$$facet_data{"errors"};}
$facet_data->{"errors"} grep {$_->{"fail"};} @{$$facet_data{"errors"};}
Truth table66%2/3
A and B
A= $facet_data->{"errors"}B= grep {$_->{"fail"};} @{$$facet_data{"errors"};}
ABresult
0X0
100
111
46  
47 6 ▶ if (my $control = $facet_data->{control}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $control = $facet_data->{"control"}) 3 3 6
48 3 ▶ return 1 if $control->{halt};
Branch: 1 branch
BranchTrueFalseTotal
if $control->{"halt"} 1 2 3
49 2 ▶ return 1 if $control->{terminate};
Branch: 1 branch
BranchTrueFalseTotal
if $control->{"terminate"} 1 1 2
50 }
51  
52 4 ▶ return 0 if $facet_data->{amnesty} && @{$facet_data->{amnesty}};
Branch: 1 branch
BranchTrueFalseTotal
if $facet_data->{"amnesty"} and @{$facet_data->{"amnesty"};} 1 3 4
Condition66%2/3
$facet_data->{"amnesty"} and @{$facet_data->{"amnesty"};}
!ll&&!rl&&r
301
MC/DC50%1/2
$facet_data->{"amnesty"} and @{$facet_data->{"amnesty"};}
$facet_data->{"amnesty"} @{$facet_data->{"amnesty"};}
Truth table66%2/3
A and B
A= $facet_data->{"amnesty"}B= @{$facet_data->{"amnesty"};}
ABresult
0X0
100
111
53 3 ▶ return 1 if $facet_data->{assert} && !$facet_data->{assert}->{pass};
Branch: 1 branch
BranchTrueFalseTotal
if $facet_data->{"assert"} and not $facet_data->{"assert"}{"pass"} 1 2 3
Condition66%2/3
$facet_data->{"assert"} and not $facet_data->{"assert"}{"pass"}
!ll&&!rl&&r
201
MC/DC50%1/2
$facet_data->{"assert"} and not $facet_data->{"assert"}{"pass"}
$facet_data->{"assert"} not $facet_data->{"assert"}{"pass"}
Truth table66%2/3
A and not(B)
A= $facet_data->{"assert"}B= not $facet_data->{"assert"}{"pass"}
ABresult
0X0
100
111
54 2 return 0;
55 }
56  
57 4 ▶sub diagnostics {
Subroutine diagnostics: called CC=6 SCAR=18.0
Pod documented
58 4 my $facet_data = _get_facet_data(shift @_);
59 4 ▶ return 1 if $facet_data->{errors} && @{$facet_data->{errors}};
Branch: 1 branch
BranchTrueFalseTotal
if $facet_data->{"errors"} and @{$facet_data->{"errors"};} 1 3 4
Condition66%2/3
$facet_data->{"errors"} and @{$facet_data->{"errors"};}
!ll&&!rl&&r
301
MC/DC50%1/2
$facet_data->{"errors"} and @{$facet_data->{"errors"};}
$facet_data->{"errors"} @{$facet_data->{"errors"};}
Truth table66%2/3
A and B
A= $facet_data->{"errors"}B= @{$facet_data->{"errors"};}
ABresult
0X0
100
111
60 3 ▶ return 0 unless $facet_data->{info} && @{$facet_data->{info}};
Branch: 1 branch
BranchTrueFalseTotal
unless $facet_data->{"info"} and @{$facet_data->{"info"};} 1 2 3
Condition66%2/3
$facet_data->{"info"} and @{$facet_data->{"info"};}
!ll&&!rl&&r
102
MC/DC50%1/2
$facet_data->{"info"} and @{$facet_data->{"info"};}
$facet_data->{"info"} @{$facet_data->{"info"};}
Truth table66%2/3
A and B
A= $facet_data->{"info"}B= @{$facet_data->{"info"};}
ABresult
0X0
100
111
61 2 ▶ return (grep { $_->{debug} } @{$facet_data->{info}}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
grep({$_->{'debug'};} @{$$facet_data{'info'};}) ? : 1 1 2
62 }
63  
64 36 ▶sub global {
Subroutine global: called CC=2 SCAR=6.9
Pod documented
65 36 my $facet_data = _get_facet_data(shift @_);
66 36 ▶ return 0 unless $facet_data->{control};
Branch: 1 branch
BranchTrueFalseTotal
unless $facet_data->{"control"} 1 35 36
67 35 return $facet_data->{control}->{global};
68 }
69  
70 2 ▶sub increments_count {
Subroutine increments_count: called CC=2 SCAR=6.9
Pod documented
71 2 my $facet_data = _get_facet_data(shift @_);
72 2 ▶ return $facet_data->{assert} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$facet_data->{'assert'} ? : 1 1 2
73 }
74  
75 3 ▶sub no_display {
Subroutine no_display: called CC=2 SCAR=6.9
Pod documented
76 3 my $facet_data = _get_facet_data(shift @_);
77 3 ▶ return 0 unless $facet_data->{about};
Branch: 1 branch
BranchTrueFalseTotal
unless $facet_data->{"about"} 1 2 3
78 2 return $facet_data->{about}->{no_display};
79 }
80  
81 6 ▶sub sets_plan {
Subroutine sets_plan: called CC=6 SCAR=17.9
Pod documented
82 6 my $facet_data = _get_facet_data(shift @_);
83 6 ▶ my $plan = $facet_data->{plan} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $plan = $facet_data->{"plan"} or return 5 1 6
84 5 ▶ my @out = ($plan->{count} || 0);
Condition100%2/2
$plan->{'count'} || 0
l!l
14
MC/DC100%1/1
$plan->{'count'} || 0
$plan->{'count'}
Truth table100%2/2
A
A= $plan->{'count'}
Aresult
11
00
85  
86 5 ▶ if ($plan->{skip}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($plan->{'skip'}) { } 2 3 5
elsif ($plan->{'none'}) { } 1 2 3
87 2 push @out => 'SKIP';
88 2 ▶ push @out => $plan->{details} if defined $plan->{details};
Branch: 1 branch
BranchTrueFalseTotal
if defined $plan->{"details"} 1 1 2
89 }
90 elsif ($plan->{none}) {
91 1 push @out => 'NO PLAN'
92 }
93  
94 5 return @out;
95 }
96  
97 2 ▶sub subtest_id {
Subroutine subtest_id: called CC=2 SCAR=6.9
Pod documented
98 2 my $facet_data = _get_facet_data(shift @_);
99 2 ▶ return undef unless $facet_data->{parent};
Branch: 1 branch
BranchTrueFalseTotal
unless $facet_data->{"parent"} 1 1 2
100 1 return $facet_data->{parent}->{hid};
101 }
102  
103 3 ▶sub summary {
Subroutine summary: called CC=3 SCAR=11.0
Pod documented
104 3 my $facet_data = _get_facet_data(shift @_);
105 3 ▶ return '' unless $facet_data->{about} && $facet_data->{about}->{details};
Branch: 1 branch
BranchTrueFalseTotal
unless $facet_data->{"about"} and $facet_data->{"about"}{"details"} 1 2 3
Condition66%2/3
$facet_data->{"about"} and $facet_data->{"about"}{"details"}
!ll&&!rl&&r
102
MC/DC50%1/2
$facet_data->{"about"} and $facet_data->{"about"}{"details"}
$facet_data->{"about"} $facet_data->{"about"}{"details"}
Truth table66%2/3
A and B
A= $facet_data->{"about"}B= $facet_data->{"about"}{"details"}
ABresult
0X0
100
111
106 2 return $facet_data->{about}->{details};
107 }
108  
109 10 ▶sub terminate {
Subroutine terminate: called CC=2 SCAR=6.9
Pod documented
110 10 my $facet_data = _get_facet_data(shift @_);
111 10 ▶ return undef unless $facet_data->{control};
Branch: 1 branch
BranchTrueFalseTotal
unless $facet_data->{"control"} 7 3 10
112 3 return $facet_data->{control}->{terminate};
113 }
114  
115 14 ▶sub uuid {
Subroutine uuid: called CC=10 SCAR=26.4
Pod documented
116 14 my $in = shift;
117  
118 14 ▶ if ($CYCLE_DETECT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Test2::Util::Facets2Legacy::CYCLE_DETECT) 8 6 14
119 8 ▶ if (blessed($in) && $in->isa('Test2::Event')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($in) and $in->isa("Test2::Event")) 8 0 8
Condition33%1/3
builtin::blessed($in) and $in->isa("Test2::Event")
!ll&&!rl&&r
008
MC/DC0%0/2
builtin::blessed($in) and $in->isa("Test2::Event")
builtin::blessed($in) $in->isa("Test2::Event")
Truth table33%1/3
A and B
A= builtin::blessed($in)B= $in->isa("Test2::Event")
ABresult
0X0
100
111
120 8 my $meth = $in->can('uuid');
121 8 ▶ $meth = $in->can('SUPER::uuid') if $meth == \&uuid;
Branch: 1 branch
BranchTrueFalseTotal
if $meth == \&Test2::Util::Facets2Legacy::uuid 8 0 8
122 8 ▶ my $uuid = $in->$meth if $meth && $meth != \&uuid;
Branch: 1 branch
BranchTrueFalseTotal
if $meth and $meth != \&Test2::Util::Facets2Legacy::uuid 0 8 8
Condition33%1/3
$meth and $meth != \&Test2::Util::Facets2Legacy::uuid
!ll&&!rl&&r
800
MC/DC0%0/2
$meth and $meth != \&Test2::Util::Facets2Legacy::uuid
$meth $meth != \&Test2::Util::Facets2Legacy::uuid
Truth table33%1/3
A and B
A= $methB= $meth != \&Test2::Util::Facets2Legacy::uuid
ABresult
0X0
100
111
123 8 ▶ return $uuid if $uuid;
Branch: 1 branch
BranchTrueFalseTotal
if $uuid 0 8 8
124 }
125  
126 8 return undef;
127 }
128  
129 6 my $facet_data = _get_facet_data($in);
130 6 ▶ return $facet_data->{about}->{uuid} if $facet_data->{about} && $facet_data->{about}->{uuid};
Branch: 1 branch
BranchTrueFalseTotal
if $facet_data->{"about"} and $facet_data->{"about"}{"uuid"} 0 6 6
Condition33%1/3
$facet_data->{"about"} and $facet_data->{"about"}{"uuid"}
!ll&&!rl&&r
060
MC/DC0%0/2
$facet_data->{"about"} and $facet_data->{"about"}{"uuid"}
$facet_data->{"about"} $facet_data->{"about"}{"uuid"}
Truth table33%1/3
A and B
A= $facet_data->{"about"}B= $facet_data->{"about"}{"uuid"}
ABresult
0X0
100
111
131  
132 6 return undef;
133 }
134  
135 455 1;
136  
137 =pod
138  
139 =encoding UTF-8
140  
141 =head1 NAME
142  
143 Test2::Util::Facets2Legacy - Convert facet data to the legacy event API.
144  
145 =head1 DESCRIPTION
146  
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.
151  
152 =head1 SYNOPSIS
153  
154 =head2 AS METHODS
155  
156 package My::Event;
157  
158 use Test2::Util::Facets2Legacy ':ALL';
159  
160 sub facet_data { return { ... } }
161  
162 Then to use it:
163  
164 my $e = My::Event->new(...);
165  
166 my $causes_fail = $e->causes_fail;
167 my $summary = $e->summary;
168 ....
169  
170 =head2 AS FUNCTIONS
171  
172 use Test2::Util::Facets2Legacy ':ALL';
173  
174 my $f = {
175 assert => { ... },
176 info => [{...}, ...],
177 control => {...},
178 ...
179 };
180  
181 my $causes_fail = causes_fail($f);
182 my $summary = summary($f);
183  
184 =head1 NOTE ON CYCLES
185  
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.
190  
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.
194  
195 =head1 EXPORTS
196  
197 Nothing is exported by default. You must specify which methods to import, or
198 use the ':ALL' tag.
199  
200 =over 4
201  
202 =item $bool = $e->causes_fail()
203  
204 =item $bool = causes_fail($f)
205  
206 Check if the event or facets result in a failing state.
207  
208 =item $bool = $e->diagnostics()
209  
210 =item $bool = diagnostics($f)
211  
212 Check if the event or facets contain any diagnostics information.
213  
214 =item $bool = $e->global()
215  
216 =item $bool = global($f)
217  
218 Check if the event or facets need to be globally processed.
219  
220 =item $bool = $e->increments_count()
221  
222 =item $bool = increments_count($f)
223  
224 Check if the event or facets make an assertion.
225  
226 =item $bool = $e->no_display()
227  
228 =item $bool = no_display($f)
229  
230 Check if the event or facets should be rendered or hidden.
231  
232 =item ($max, $directive, $reason) = $e->sets_plan()
233  
234 =item ($max, $directive, $reason) = sets_plan($f)
235  
236 Check if the event or facets set a plan, and return the plan details.
237  
238 =item $id = $e->subtest_id()
239  
240 =item $id = subtest_id($f)
241  
242 Get the subtest id, if any.
243  
244 =item $string = $e->summary()
245  
246 =item $string = summary($f)
247  
248 Get the summary of the event or facets hash, if any.
249  
250 =item $undef_or_int = $e->terminate()
251  
252 =item $undef_or_int = terminate($f)
253  
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
256 is requested.
257  
258 =item $uuid = $e->uuid()
259  
260 =item $uuid = uuid($f)
261  
262 Get the UUID of the facets or event.
263  
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.
266  
267 =back
268  
269 =head1 SOURCE
270  
271 The source code repository for Test2 can be found at
272 L<https://github.com/Test-More/test-more/>.
273  
274 =head1 MAINTAINERS
275  
276 =over 4
277  
278 =item Chad Granum E<lt>exodist@cpan.orgE<gt>
279  
280 =back
281  
282 =head1 AUTHORS
283  
284 =over 4
285  
286 =item Chad Granum E<lt>exodist@cpan.orgE<gt>
287  
288 =back
289  
290 =head1 COPYRIGHT
291  
292 Copyright Chad Granum E<lt>exodist@cpan.orgE<gt>.
293  
294 This program is free software; you can redistribute it and/or
295 modify it under the same terms as Perl itself.
296  
297 See L<https://dev.perl.org/licenses/>
298  
299 =cut
« blib/lib/Test2/Compare/Bag.pm ↑ Summary blib/lib/Test2/Workflow/Build.pm »