blib/lib/TAP/Formatter/Camelcade/Session.pm

filter: Statementstmt 89.8% 176 / 196 Branchbran 75.0% 48 / 64 Conditioncond 67.8% 19 / 28 MC/DCmcdc 35.2% 6 / 17 Subroutinesub 88.8% 32 / 36 Podpod 7.4% 2 / 27 totaltotal 76.9% 283 / 368 SCAR 39.4
CC
44
Cov
84%
result
13.4
close_pending_test
9.3
_compute_test_name
7.0
CRAP 51.4
↑ Summary blib/lib/TAP/Formatter/Camelcade.pm »
1 package TAP::Formatter::Camelcade::Session;
2 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 19 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 19 ▶use base 'TAP::Formatter::Session';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 19 ▶use TAP::Formatter::Camelcade::MessageBuilder;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 19 ▶use Time::HiRes qw/time/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 19 ▶use Digest::MD5 qw/md5_hex/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 #@returns TAP::Formatter::Camelcade::MessageBuilder
10 sub builder {
11 544 ▶ return 'TAP::Formatter::Camelcade::MessageBuilder';
Subroutine builder: called CC=1 SCAR=0.0
Pod undocumented
12 }
13  
14 =pod
15  
16 Invoked for every line of parsed result
17  
18 =cut
19  
20 sub result {
21 668 ▶ my $self = shift;
Subroutine result: called CC=9 SCAR=26.0
Pod documented
22 #@type TAP::Parser::Result
23 668 my $result = shift;
24  
25 668 my $type = $result->type;
26 668 ▶ if ($type eq 'plan') {
Branch: 8 branches
BranchTrueFalseTotal
if ($type eq 'plan') { } 64 604 668
elsif ($type eq 'pragma') { } 0 604 604
elsif ($type eq 'test') { } 152 452 604
elsif ($type eq 'comment') { } 340 112 452
elsif ($type eq 'bailout') { } 0 112 112
elsif ($type eq 'version') { } 0 112 112
elsif ($type eq 'unknown') { } 112 0 112
elsif ($type eq 'yaml') { } 0 0 0
27 64 $self->handle_plan($result);
28 }
29 elsif ($type eq 'pragma') {
30 0 $self->handle_pragma($result);
31 }
32 elsif ($type eq 'test') {
33 152 $self->handle_test($result);
34 }
35 elsif ($type eq 'comment') {
36 340 $self->handle_comment($result);
37 }
38 elsif ($type eq 'bailout') {
39 0 $self->handle_bailout($result);
40 }
41 elsif ($type eq 'version') {
42 0 $self->handle_version($result);
43 }
44 elsif ($type eq 'unknown') {
45 112 $self->handle_unknown($result);
46 }
47 elsif ($type eq 'yaml') {
48 0 $self->handle_yaml($result);
49 }
50 else {
51 0 die "Unknown type: $type";
52 }
53 }
54  
55 =pod
56  
57 We are keeping last test failure to append comments into it
58  
59 =cut
60  
61 sub close_pending_test {
62 296 ▶ my $self = shift;
Subroutine close_pending_test: called CC=9 SCAR=22.3
Pod undocumented
63 296 my $pending_test = $self->get_pending_test;
64 296 $self->{last_test} = undef;
65 296 ▶ if (!defined $pending_test) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pending_test) 160 136 296
66 160 return;
67 }
68 136 $self->set_last_time;
69 136 my $test_output = join "\n", @{$pending_test->{output}};
70 136 ▶ $test_output //= "";
Condition50%1/2
$test_output //= ""
l!l
1360
MC/DC0%0/1
$test_output //= ""
$test_output
Truth table50%1/2
A
A= $test_output
Aresult
11
00
71 136 $test_output =~ s{(\A\n+|\n+\z)}{}gs;
72 136 ▶ if ($pending_test->{is_ok}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pending_test->{'is_ok'}) { } 116 20 136
73 116 ▶ if ($pending_test->{is_skip}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pending_test->{'is_skip'}) { } 48 68 116
elsif (length $test_output > 0) { } 0 68 68
74 builder->test_ignored(
75 $pending_test->{name},
76 $test_output,
77 $pending_test->{nodeId},
78 $pending_test->{parentNodeId},
79 48 );
80 }
81 elsif (length $test_output > 0) {
82 builder->stderr(
83 $pending_test->{name},
84 $test_output,
85 $pending_test->{nodeId},
86 $pending_test->{parentNodeId},
87 0 );
88 }
89 }
90 else {
91 20 my @extra = ();
92 20 ▶ if ($test_output) {
Branch: 1 branch
BranchTrueFalseTotal
if ($test_output) 20 0 20
93 20 my $normalized_output = $test_output;
94 20 $normalized_output =~ s{^\s*# }{}gmi;
95 # use v5.10; say STDERR "Normalized as $normalized_output";
96 20 ▶ if ($normalized_output =~ m{ got: '(.*?)'\n expected: '(.*)'}s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($normalized_output =~ / got: '(.*?)'\n expected: '(.*)'/s) 20 0 20
97 20 @extra = (
98 type => 'comparisonFailure',
99 actual => $1,
100 expected => $2
101 );
102 }
103 }
104 20 $test_output =~ s/^\s+//gm;
105  
106 builder->test_failed(
107 $pending_test->{name},
108 $pending_test->{explanation} // "",
109 $pending_test->{nodeId},
110 $pending_test->{parentNodeId},
111 20 ▶ details => $test_output,
Condition50%1/2
$pending_test->{"explanation"} // ""
l!l
200
MC/DC0%0/1
$pending_test->{"explanation"} // ""
$pending_test->{"explanation"}
Truth table50%1/2
A
A= $pending_test->{"explanation"}
Aresult
11
00
112 @extra
113 );
114 }
115 136 builder->test_finished(@$pending_test{qw/name duration nodeId parentNodeId/});
116 }
117  
118 sub start_test {
119 136 ▶ my $self = shift;
Subroutine start_test: called CC=2 SCAR=7.0
Pod undocumented
120 #@type TAP::Parser::Result::Test
121 136 my $test = shift;
122  
123 136 my $last_time = $self->get_last_time;
124 136 my $test_name = $self->_compute_test_name($test);
125 136 my $test_duration = int((time - $last_time) * 1000);
126 136 $self->close_pending_test;
127 $self->{last_test} = {
128 name => $test_name,
129 136 ▶ duration => $ENV{TAP_FORMATTER_CAMELCADE_DURATION} // $test_duration,
Condition33%1/3
$ENV{"TAP_FORMATTER_CAMELCADE_DURATION"} // $test_duration
l!l&&r!l&&!r
13600
MC/DC0%0/2
$ENV{"TAP_FORMATTER_CAMELCADE_DURATION"} // $test_duration
$ENV{"TAP_FORMATTER_CAMELCADE_DURATION"} $test_duration
Truth table33%1/3
A // B
A= $ENV{"TAP_FORMATTER_CAMELCADE_DURATION"}B= $test_duration
ABresult
1X1
011
000
130 is_ok => $test->is_ok,
131 is_skip => $test->has_directive,
132 output => [],
133 explanation => $test->explanation,
134 nodeId => $self->generate_test_id($test_name),
135 parentNodeId => $self->get_parent_node_id
136 };
137 136 builder->test_started(@{$self->{last_test}}{qw/name nodeId parentNodeId/});
138 }
139  
140 sub set_last_time {
141 200 ▶ my $self = shift;
Subroutine set_last_time: called CC=1 SCAR=0.0
Pod undocumented
142 200 $self->{last_time} = time;
143 }
144  
145 sub get_last_time {
146 136 ▶ my $self = shift;
Subroutine get_last_time: called CC=2 SCAR=8.1
Pod undocumented
147 136 ▶ return $self->{last_time} // time;
Condition33%1/3
$self->{"last_time"} // &TAP::Formatter::Camelcade::Session::time()
l!l&&r!l&&!r
13600
MC/DC0%0/2
$self->{"last_time"} // &TAP::Formatter::Camelcade::Session::time()
$self->{"last_time"} &TAP::Formatter::Camelcade::Session::time()
Truth table33%1/3
A // B
A= $self->{"last_time"}B= &TAP::Formatter::Camelcade::Session::time()
ABresult
1X1
011
000
148 }
149  
150 sub get_pending_test {
151 620 ▶ my $self = shift;
Subroutine get_pending_test: called CC=1 SCAR=0.0
Pod undocumented
152 620 return $self->{last_test};
153 }
154  
155 sub _compute_test_name {
156 288 ▶ shift;
Subroutine _compute_test_name: called CC=7 SCAR=19.5
157 #@type TAP::Parser::Result::Test
158 288 my $result = shift;
159 288 my $base_name = "";
160 288 my $description = $result->description;
161 288 my $explanation = $result->explanation;
162 288 ▶ if( $description ){
Branch: 2 branches
BranchTrueFalseTotal
if ($description) { } 176 112 288
elsif ($explanation) { } 80 32 112
163 176 $base_name = $description;
164 176 ▶ if( $explanation ){
Branch: 1 branch
BranchTrueFalseTotal
if ($explanation) 16 160 176
165 16 $base_name .= " ($explanation)";
166 }
167 }
168 elsif( $explanation){
169 80 $base_name = $explanation;
170 }
171  
172 288 $base_name =~ s/^-\s//;
173 288 ▶ $base_name ||= 'Unnamed test (#' . $result->number . ')';
Condition100%2/2
$base_name ||= 'Unnamed test (#' . $result->number . ')'
l!l
25632
MC/DC100%1/1
$base_name ||= 'Unnamed test (#' . $result->number . ')'
$base_name
Truth table100%2/2
A
A= $base_name
Aresult
11
00
174 288 ▶ my $directive = $result->directive // "";
Condition50%1/2
$result->directive // ""
l!l
2880
MC/DC0%0/1
$result->directive // ""
$result->directive
Truth table50%1/2
A
A= $result->directive
Aresult
11
00
175 288 ▶ return join" ", ($directive ? ($directive) : ()), $base_name;
Branch: 1 branch
BranchTrueFalseTotal
$directive ? : 96 192 288
176 }
177  
178 sub handle_plan {
179 64 ▶ my $self = shift;
Subroutine handle_plan: called CC=2 SCAR=7.4
Pod undocumented
180 #@type TAP::Parser::Result::Plan
181 64 my $result = shift;
182 64 $self->close_pending_test;
183  
184 64 ▶ if ($result->directive eq 'SKIP') {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->directive eq "SKIP") 0 64 64
185 0 builder->output($result->explanation);
186 }
187 }
188  
189 sub handle_pragma {
190 0 ▶ my $self = shift;
Subroutine handle_pragma: not called CC=1 SCAR=6.9
Pod undocumented
191 #@type TAP::Parser::Result::Pragma
192 0 my $result = shift;
193 0 $self->process_as_comment($result);
194 }
195  
196 =pod
197  
198 Invoked for every test result
199  
200 =cut
201  
202 sub handle_test {
203 152 ▶ my $self = shift;
Subroutine handle_test: called CC=2 SCAR=6.9
Pod undocumented
204 #@type TAP::Parser::Result::Test
205 152 my $result = shift;
206  
207 152 my $test_name = $self->_compute_test_name($result);
208 152 ▶ if ($self->is_subtest($test_name)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_subtest($test_name)) { } 16 136 152
209 16 $self->finish_subtest;
210 }
211 else {
212 136 $self->start_test($result);
213 }
214 }
215  
216 sub handle_comment {
217 340 ▶ my $self = shift;
Subroutine handle_comment: called CC=3 SCAR=11.0
Pod undocumented
218 #@type TAP::Parser::Result::Comment
219 340 my $result = shift;
220  
221 340 my $comment = $result->raw;
222  
223 340 ▶ if ($comment =~ /^\s*# Subtest: (.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($comment =~ /^\s*# Subtest: (.+)$/) { } 16 324 340
224 16 $self->start_subtest($1);
225 }
226 else {
227 324 my $last_test = $self->get_pending_test;
228 324 ▶ if ($last_test) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_test) { } 280 44 324
229 280 push @{$last_test->{output}}, $comment;
230 }
231 else {
232 44 builder->warning($comment);
233 }
234 }
235 }
236  
237 sub handle_bailout {
238 0 ▶ my $self = shift;
Subroutine handle_bailout: not called CC=1 SCAR=6.9
Pod undocumented
239 #@type TAP::Parser::Result::Bailout
240 0 my $result = shift;
241 0 $self->process_as_comment($result);
242 }
243  
244 sub handle_version {
245 0 ▶ my $self = shift;
Subroutine handle_version: not called CC=1 SCAR=6.9
Pod undocumented
246 #@type TAP::Parser::Result::Version
247 0 my $result = shift;
248 0 $self->process_as_comment($result);
249 }
250  
251 sub handle_unknown {
252 112 ▶ my $self = shift;
Subroutine handle_unknown: called CC=6 SCAR=18.0
Pod undocumented
253 #@type TAP::Parser::Result::Unknown
254 112 my $result = shift;
255 112 my $raw = $result->raw;
256 112 ▶ if ($self->in_in_subtest && $raw =~ /^\s*(((?:not )?ok) ([0-9]+)(?: (- .*))?)$/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->in_in_subtest and $raw =~ /^\s*(((?:not )?ok) ([0-9]+)(?: (- .*))?)$/) { } 16 96 112
elsif ($raw =~ /^\s+# Subtest: (.+)$/) { } 0 96 96
elsif ($raw =~ /^\s*\d+\.\.\d+$/) { } 16 80 96
Condition100%3/3
$self->in_in_subtest and $raw =~ /^\s*(((?:not )?ok) ([0-9]+)(?: (- .*))?)$/
!ll&&!rl&&r
207616
MC/DC100%2/2
$self->in_in_subtest and $raw =~ /^\s*(((?:not )?ok) ([0-9]+)(?: (- .*))?)$/
$self->in_in_subtest $raw =~ /^\s*(((?:not )?ok) ([0-9]+)(?: (- .*))?)$/
Truth table100%3/3
A and B
A= $self->in_in_subtestB= $raw =~ /^\s*(((?:not )?ok) ([0-9]+)(?: (- .*))?)$/
ABresult
0X0
100
111
257 16 my $test_raw = $1;
258 16 my $is_ok = $2;
259 16 my $test_num = $3;
260 16 ▶ my $description = $4 // "";
Condition100%2/2
$4 // ""
l!l
610
MC/DC100%1/1
$4 // ""
$4
Truth table100%2/2
A
A= $4
Aresult
11
00
261 16 $self->result(TAP::Parser::Result::Test->new({
262 raw => $test_raw,
263 ok => $is_ok,
264 test_num => $test_num,
265 description => $description,
266 type => 'test',
267 explanation => '',
268 directive => '',
269 }));
270 }
271 elsif ($raw =~ /^\s+# Subtest: (.+)$/) {
272 0 $self->start_subtest($1);
273 }
274 elsif ($raw =~ /^\s*\d+\.\.\d+$/) {
275 # ignore
276 }
277 else {
278 80 $self->process_as_comment($result);
279 }
280 }
281  
282 #@returns TAP::Parser::Result::Comment
283 sub process_as_comment {
284 80 ▶ my $self = shift;
Subroutine process_as_comment: called CC=1 SCAR=0.0
Pod undocumented
285 #@type TAP::Parser::Result
286 80 my $result = shift;
287 80 my $comment = $result->raw;
288 80 $comment =~ s/^\s+//;
289 # use Data::Dumper; print STDERR "Handling: ".Dumper($result);
290 80 $self->result(TAP::Parser::Result::Comment->new({
291 raw => $result->raw,
292 type => 'comment',
293 comment => $comment
294 }));
295 }
296  
297 sub handle_yaml {
298 0 ▶ my $self = shift;
Subroutine handle_yaml: not called CC=1 SCAR=6.9
Pod undocumented
299 #@type TAP::Parser::Result::YAML
300 0 my $result = shift;
301 0 $self->process_as_comment($result);
302 }
303  
304 sub close_test {
305 64 ▶ my $self = shift;
Subroutine close_test: called CC=1 SCAR=0.0
Pod documented
306 64 $self->finish_suite;
307 }
308  
309 #@override
310 sub _initialize {
311 64 ▶ my ($self, $arg_for) = @_;
Subroutine _initialize: called CC=1 SCAR=0.0
312  
313 64 $self->{location} = delete $$arg_for{location};
314  
315 64 $self->SUPER::_initialize($arg_for);
316 64 $self->{subtests} = [];
317 64 $self->{suites} = [];
318 64 $self->{counter} = 0;
319 # $arg_for has parser, name, formatter
320 64 $self->start_suite($self->name, $self->{location});
321 64 $self->set_last_time;
322 64 return $self;
323 }
324  
325 sub start_suite {
326 80 ▶ my $self = shift;
Subroutine start_suite: called CC=2 SCAR=7.0
Pod undocumented
327 80 my $name = shift;
328 80 my $location = shift;
329 80 ▶ my $suite = {
Condition66%2/3
$location || $name
l!l&&r!l&&!r
64160
MC/DC0%0/2
$location || $name
$location $name
Truth table66%2/3
A || B
A= $locationB= $name
ABresult
1X1
011
000
330 name => $name,
331 nodeId => $self->generate_suite_id($name, $location),
332 parentNodeId => $self->get_parent_node_id(),
333 location => $location || $name
334 };
335 80 push @{$self->{suites}}, $suite;
336 80 builder->test_suite_started(@$suite{qw/name location nodeId parentNodeId/});
337 }
338  
339 sub generate_suite_id {
340 80 ▶ my $self = shift;
Subroutine generate_suite_id: called CC=4 SCAR=14.5
Pod undocumented
341 80 my $name = shift;
342 80 my $location = shift;
343 80 ▶ my $new_id = join '-', $location || $name, $ENV{TAP_FORMATTER_CAMELCADE_DURATION} ? () : ($$, time), $self->{counter}++;
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'TAP_FORMATTER_CAMELCADE_DURATION'} ? : 80 0 80
Condition66%2/3
$location || $name
l!l&&r!l&&!r
64160
MC/DC0%0/2
$location || $name
$location $name
Truth table66%2/3
A || B
A= $locationB= $name
ABresult
1X1
011
000
344 80 ▶ return $ENV{TAP_FORMATTER_CAMELCADE_DURATION} ? $new_id : md5_hex($new_id);
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'TAP_FORMATTER_CAMELCADE_DURATION'} ? : 80 0 80
345 }
346  
347 sub generate_test_id {
348 136 ▶ my $self = shift;
Subroutine generate_test_id: called CC=2 SCAR=7.0
Pod undocumented
349 136 my $name = shift;
350 136 my $new_id = join '-', $self->get_parent_node_id, $name, $self->{counter}++;
351 136 ▶ return $ENV{TAP_FORMATTER_CAMELCADE_DURATION} ? $new_id : md5_hex($new_id);
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'TAP_FORMATTER_CAMELCADE_DURATION'} ? : 136 0 136
352 }
353  
354 sub get_current_suite {
355 352 ▶ my $self = shift;
Subroutine get_current_suite: called CC=2 SCAR=6.9
Pod undocumented
356 352 ▶ if (scalar @{$self->{suites}} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->{"suites"};} == 0) 64 288 352
357 64 return undef;
358 }
359 288 return $self->{suites}->[$#{$self->{suites}}];
360 }
361  
362 sub get_parent_node_id {
363 352 ▶ my $self = shift;
Subroutine get_parent_node_id: called CC=2 SCAR=6.9
Pod undocumented
364 352 my $current_suite = $self->get_current_suite;
365 352 ▶ return defined $current_suite ? $current_suite->{nodeId} : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $current_suite ? : 288 64 352
366 }
367  
368 sub finish_suite {
369 80 ▶ my $self = shift;
Subroutine finish_suite: called CC=2 SCAR=7.0
Pod undocumented
370 80 $self->close_pending_test;
371  
372 80 my $current_suite = pop @{$self->{suites}};
373 80 ▶ return unless $current_suite;
Branch: 1 branch
BranchTrueFalseTotal
unless $current_suite 0 80 80
374 80 builder->test_suite_finished(@$current_suite{qw/name nodeId parentNodeId/});
375 }
376  
377 sub start_subtest {
378 16 ▶ my $self = shift;
Subroutine start_subtest: called CC=1 SCAR=0.0
Pod undocumented
379 16 my $subtest_name = shift;
380 16 $self->close_pending_test;
381 16 push @{$self->{subtests}}, $subtest_name;
382 16 $self->start_suite($subtest_name);
383 }
384  
385 sub is_subtest {
386 152 ▶ my $self = shift;
Subroutine is_subtest: called CC=2 SCAR=6.9
Pod undocumented
387 152 my $test_name = shift;
388 152 my $last_index = $#{$self->{subtests}};
389 152 ▶ return $last_index > -1 && $self->{subtests}->[$last_index] eq $test_name;
Condition100%3/3
$last_index > -1 && $self->{'subtests'}[$last_index] eq $test_name
!ll&&!rl&&r
1201616
MC/DC100%2/2
$last_index > -1 && $self->{'subtests'}[$last_index] eq $test_name
$last_index > -1 $self->{'subtests'}[$last_index] eq $test_name
Truth table100%3/3
A && B
A= $last_index > -1B= $self->{'subtests'}[$last_index] eq $test_name
ABresult
0X0
100
111
390 }
391  
392 sub in_in_subtest {
393 112 ▶ my $self = shift;
Subroutine in_in_subtest: called CC=1 SCAR=0.0
Pod undocumented
394 112 return scalar @{$self->{subtests}};
395 }
396  
397 sub finish_subtest {
398 16 ▶ my $self = shift;
Subroutine finish_subtest: called CC=2 SCAR=7.0
Pod undocumented
399 16 my $subtest_name = pop @{$self->{subtests}};
400 16 ▶ return unless $subtest_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $subtest_name 0 16 16
401 16 $self->finish_suite;
402 }
403  
404 #@method
405 #@override
406 sub _should_show_count {
407 64 ▶ 0;
Subroutine _should_show_count: called CC=1 SCAR=0.0
408 }
409  
410 19 1;
↑ Summary blib/lib/TAP/Formatter/Camelcade.pm »