TAP/Formatter/HTML/Session.pm

filter: Statementstmt 94.3% 100 / 106 Branchbran 86.0% 43 / 50 Conditioncond 67.6% 23 / 34 MC/DCmcdc 38.8% 7 / 18 Subroutinesub 100% 13 / 13 Podpod 0.0% 0 / 7 totaltotal 81.5% 186 / 228 CC 42 SCAR 28.1
CC
42
Cov
87%
as_report
27.5
result
16.0
_initialize
4.0
CRAP 16.6
↑ Summary TAP/Formatter/HTML.pm »
1 =head1 NAME
2  
3 TAP::Formatter::HTML::Session - TAP Test Harness output delegate for html output
4  
5 =head1 SYNOPSIS
6  
7 # see TAP::Formatter::HTML
8  
9 =cut
10  
11 package TAP::Formatter::HTML::Session;
12  
13 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # DEBUG:
17 #use Data::Dumper 'Dumper';
18  
19 14 ▶use base qw( TAP::Base );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 14 ▶use accessors qw( test formatter parser results html_id meta closed );
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 14 our $VERSION = '0.13';
23  
24 38 ▶sub _initialize {
Subroutine _initialize: called CC=4 SCAR=14.0
25 38 my ($self, $args) = @_;
26  
27 38 ▶ $args ||= {};
Condition50%1/2
$args ||= {}
l!l
380
MC/DC0%0/1
$args ||= {}
$args
Truth table50%1/2
A
A= $args
Aresult
11
00
28 38 $self->SUPER::_initialize($args);
29  
30 38 $self->results([])->meta({})->closed(0);
31 38 foreach my $arg (qw( test parser formatter )) {
32 114 ▶ $self->$arg($args->{$arg}) if defined $args->{$arg};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args->{$arg} 114 0 114
33 }
34  
35 # make referring to it in HTML easy:
36 38 my $html_id = $self->test;
37 38 $html_id =~ s/[^a-zA-Z\d-]/-/g;
38 38 $self->html_id( $html_id );
39  
40 38 $self->info( $self->test, ':' );
41  
42 38 return $self;
43 }
44  
45 # Called by TAP::Parser to create a result after a session is opened
46 # TODO: override TAP::Parser::ResultFactory and add html-aware results?
47 # OR: mixin some methods to the results.
48 # this logic is getting cumbersome. :-/
49 1746 ▶sub result {
Subroutine result: called CC=16 SCAR=27.7
Pod undocumented
50 1746 my ($self, $result) = @_;
51 #warn ref($self) . "->result called with args: " . Dumper( $result );
52  
53 1746 my $iter = $self->html_id_iterator;
54 1746 ▶ if ($result->is_test) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->is_test) { } 1206 540 1746
55 1206 $self->log( $result->as_string );
56 # make referring to it in HTML easy:
57 1206 ▶ $result->{html_id} = $iter ? $iter->() : $self->html_id . '-' . $result->number;
Branch: 1 branch
BranchTrueFalseTotal
$iter ? : 1206 0 1206
58  
59 # set test status to avoid the hassle of recalculating it in the template:
60 1206 ▶ $result->{test_status} = $result->has_todo ? 'todo-' : '';
Branch: 1 branch
BranchTrueFalseTotal
$result->has_todo ? : 64 1142 1206
61 1206 ▶ $result->{test_status} .= $result->has_skip ? 'skip-' : '';
Branch: 1 branch
BranchTrueFalseTotal
$result->has_skip ? : 30 1176 1206
62 1206 ▶ $result->{test_status} .= $result->is_actual_ok ? 'ok' : 'not-ok';
Branch: 1 branch
BranchTrueFalseTotal
$result->is_actual_ok ? : 1033 173 1206
63  
64 # also provide a 'short' status name to reduce size of html:
65 1206 my $short;
66 1206 ▶ if ($result->has_todo) {
Branch: 3 branches
BranchTrueFalseTotal
if ($result->has_todo) { } 64 1142 1206
elsif ($result->has_skip) { } 30 1112 1142
elsif ($result->is_actual_ok) { } 999 113 1112
67 64 ▶ if ($result->is_actual_ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->is_actual_ok) { } 4 60 64
68 4 $short = 'u'; # todo-ok = "unexpected" ok
69 } else {
70 60 $short = 't'; # todo-not-ok
71 }
72 } elsif ($result->has_skip) {
73 30 $short = 's'; # skip-ok
74 } elsif ($result->is_actual_ok) {
75 999 $short = 'k'; # ok
76 } else {
77 113 $short = 'n'; # not-ok
78 }
79 1206 $result->{short_test_status} = $short;
80  
81 # keep track of passes for percent_passed calcs:
82 1206 ▶ if ($result->is_ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->is_ok) 1091 115 1206
83 1091 $self->meta->{passed}++;
84 }
85  
86 # keep track of passes (including unplanned!) for actual_percent_passed calcs:
87 1206 ▶ if ($result->is_ok || $result->is_unplanned && $result->is_actual_ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->is_ok or $result->is_unplanned and $result->is_actual_ok) 1093 113 1206
Condition83%5/6
$result->is_ok or $result->is_unplanned and $result->is_actual_ok
l!l&&r!l&&!r
10912113
$result->is_unplanned and $result->is_actual_ok
!ll&&!rl&&r
11302
MC/DC66%2/3
$result->is_ok or $result->is_unplanned and $result->is_actual_ok
$result->is_ok $result->is_unplanned $result->is_actual_ok
Truth table75%3/4
A or B and C
A= $result->is_okB= $result->is_unplannedC= $result->is_actual_ok
ABCresult
1XX1
0111
00X0
0100
88 1093 $self->meta->{passed_including_unplanned}++;
89 }
90  
91 # mark passed todo tests for easy reference:
92 1206 ▶ if ($result->has_todo && $result->is_actual_ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->has_todo and $result->is_actual_ok) 4 1202 1206
Condition100%3/3
$result->has_todo and $result->is_actual_ok
!ll&&!rl&&r
1142604
MC/DC100%2/2
$result->has_todo and $result->is_actual_ok
$result->has_todo $result->is_actual_ok
Truth table100%3/3
A and B
A= $result->has_todoB= $result->is_actual_ok
ABresult
0X0
100
111
93 4 $result->{todo_passed} = 1;
94 }
95 } else {
96 540 $self->info( $result->as_string );
97 }
98  
99 1746 $self->set_result_css_type( $result );
100  
101 1746 push @{ $self->results }, $result;
102 1746 return;
103 }
104  
105 # TODO: inheritance was created for a reason... use it
106 14 ▶use constant result_css_type_map =>
Subroutine BEGIN: called CC=1 SCAR=0.0
107 {
108 plan => 'pln',
109 pragma => 'prg',
110 test => 'tst',
111 comment => 'cmt',
112 bailout => 'blt',
113 version => 'ver',
114 unknown => 'unk',
115 yaml => 'yml',
116 14 };
117  
118 1746 ▶sub set_result_css_type {
Subroutine set_result_css_type: called CC=3 SCAR=11.3
Pod undocumented
119 1746 my ($self, $result) = @_;
120 1746 ▶ my $type = $result->type || 'unknown';
Condition50%1/2
$result->type || 'unknown'
l!l
17460
MC/DC0%0/1
$result->type || 'unknown'
$result->type
Truth table50%1/2
A
A= $result->type
Aresult
11
00
121 1746 ▶ my $css_type = $self->result_css_type_map->{$type} || 'unk';
Condition50%1/2
$self->result_css_type_map->{$type} || 'unk'
l!l
17460
MC/DC0%0/1
$self->result_css_type_map->{$type} || 'unk'
$self->result_css_type_map->{$type}
Truth table50%1/2
A
A= $self->result_css_type_map->{$type}
Aresult
11
00
122 1746 $result->{css_type} = $css_type;
123 1746 return $self;
124 }
125  
126 # Called by TAP::?? to indicate there are no more test results coming
127 38 ▶sub close_test {
Subroutine close_test: called CC=1 SCAR=0.0
Pod undocumented
128 38 my ($self, @args) = @_;
129 # warn ref($self) . "->close_test called with args: " . Dumper( [@args] );
130 #print STDERR 'end of: ', $self->test, "\n\n";
131 38 $self->closed(1);
132 38 return;
133 }
134  
135 38 ▶sub as_report {
Subroutine as_report: called CC=22 SCAR=33.1
Pod undocumented
136 38 my ($self) = @_;
137 38 my $p = $self->parser;
138 38 my $r = {
139 test => $self->test,
140 html_id => $self->html_id,
141 results => $self->results,
142 };
143  
144 # add parser info:
145 38 for my $key (qw(
146 tests_planned
147 tests_run
148 start_time
149 end_time
150 skip_all
151 has_problems
152 passed
153 failed
154 todo_passed
155 actual_passed
156 actual_failed
157 wait
158 exit
159 )) {
160 494 $r->{$key} = $p->$key;
161 }
162  
163 38 $r->{num_parse_errors} = scalar $p->parse_errors;
164 38 $r->{parse_errors} = [ $p->parse_errors ];
165 38 $r->{passed_tests} = [ $p->passed ];
166 38 $r->{failed_tests} = [ $p->failed ];
167  
168 # do some other handy calcs:
169 38 ▶ $r->{test_status} = $r->{has_problems} ? 'failed' : 'passed';
Branch: 1 branch
BranchTrueFalseTotal
$r->{'has_problems'} ? : 19 19 38
170 38 $r->{elapsed_time} = $r->{end_time} - $r->{start_time};
171 38 $r->{severity} = '';
172 38 ▶ if ($r->{tests_planned}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($r->{'tests_planned'}) { } 34 4 38
elsif ($r->{'skip_all'}) { } 4 0 4
173 # Calculate percentage passed as # passes *excluding* unplanned passes
174 # so we can't get > 100%. Also calc # passes _including_ unplanned
175 # in case that's useful for someone.
176 34 ▶ my $num_passed = $self->meta->{passed} || 0;
Condition50%1/2
$self->meta->{'passed'} || 0
l!l
340
MC/DC0%0/1
$self->meta->{'passed'} || 0
$self->meta->{'passed'}
Truth table50%1/2
A
A= $self->meta->{'passed'}
Aresult
11
00
177 34 ▶ my $num_actual_passed = $self->meta->{passed_including_unplanned} || 0;
Condition50%1/2
$self->meta->{'passed_including_unplanned'} || 0
l!l
340
MC/DC0%0/1
$self->meta->{'passed_including_unplanned'} || 0
$self->meta->{'passed_including_unplanned'}
Truth table50%1/2
A
A= $self->meta->{'passed_including_unplanned'}
Aresult
11
00
178 34 my $p = $r->{percent_passed} = sprintf('%.1f', $num_passed / $r->{tests_planned} * 100);
179 34 $r->{percent_actual_passed} = sprintf('%.1f', $num_actual_passed / $r->{tests_planned} * 100);
180 34 ▶ if ($p != 100) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p != 100) 11 23 34
181 11 my $s;
182 0 ▶ if ($p < 25) { $s = 'very-high' }
Branch: 4 branches
BranchTrueFalseTotal
if ($p < 25) { } 0 11 11
elsif ($p < 50) { } 0 11 11
elsif ($p < 75) { } 9 2 11
elsif ($p < 95) { } 2 0 2
183 0 elsif ($p < 50) { $s = 'high' }
184 9 elsif ($p < 75) { $s = 'med' }
185 2 elsif ($p < 95) { $s = 'low' }
186 0 else { $s = 'very-low' }
187 # classify >100% as very-low
188 11 $r->{severity} = $s;
189 }
190 } elsif ($r->{skip_all}) {
191 ; # do nothing
192 } else {
193 0 $r->{percent_passed} = 0;
194 0 $r->{severity} = 'very-high';
195 }
196  
197 38 ▶ if (my $num = $r->{num_parse_errors}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $num = $r->{"num_parse_errors"}) 2 36 38
198 2 ▶ if ($num == 1 && ! $p->is_good_plan) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num == 1 and not $p->is_good_plan) { } 2 0 2
Condition33%1/3
$num == 1 and not $p->is_good_plan
!ll&&!rl&&r
002
MC/DC0%0/2
$num == 1 and not $p->is_good_plan
$num == 1 not $p->is_good_plan
Truth table33%1/3
A and not(B)
A= $num == 1B= not $p->is_good_plan
ABresult
0X0
100
111
199 2 ▶ $r->{severity} ||= 'low'; # prefer value set calculating % passed
Condition50%1/2
$r->{"severity"} ||= "low"
l!l
02
MC/DC0%0/1
$r->{"severity"} ||= "low"
$r->{"severity"}
Truth table50%1/2
A
A= $r->{"severity"}
Aresult
11
00
200 } else {
201 0 $r->{severity} = 'very-high';
202 }
203 }
204  
205 # check for scripts that died abnormally:
206 38 ▶ if ($r->{exit} && $r->{exit} == 255 && $p->is_good_plan) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"exit"} and $r->{"exit"} == 255 and $p->is_good_plan) 6 32 38
Condition100%6/6
$r->{"exit"} and $r->{"exit"} == 255 and $p->is_good_plan
!ll&&!rl&&r
3026
$r->{"exit"} and $r->{"exit"} == 255
!ll&&!rl&&r
19118
MC/DC100%3/3
$r->{"exit"} and $r->{"exit"} == 255 and $p->is_good_plan
$r->{"exit"} $r->{"exit"} == 255 $p->is_good_plan
Truth table100%4/4
A and B and C
A= $r->{"exit"}B= $r->{"exit"} == 255C= $p->is_good_plan
ABCresult
0XX0
10X0
1100
1111
207 6 ▶ $r->{severity} ||= 'very-high';
Condition50%1/2
$r->{"severity"} ||= "very-high"
l!l
06
MC/DC0%0/1
$r->{"severity"} ||= "very-high"
$r->{"severity"}
Truth table50%1/2
A
A= $r->{"severity"}
Aresult
11
00
208 }
209  
210 # catch-all:
211 38 ▶ if ($r->{has_problems}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"has_problems"}) 19 19 38
212 19 ▶ $r->{severity} ||= 'high';
Condition50%1/2
$r->{"severity"} ||= "high"
l!l
190
MC/DC0%0/1
$r->{"severity"} ||= "high"
$r->{"severity"}
Truth table50%1/2
A
A= $r->{"severity"}
Aresult
11
00
213 }
214  
215 38 return $r;
216 }
217  
218 1746 ▶sub html_id_iterator {
Subroutine html_id_iterator: called CC=1 SCAR=0.0
Pod undocumented
219 1746 shift->formatter->html_id_iterator;
220 }
221  
222 1206 ▶sub log {
Subroutine log: called CC=1 SCAR=0.0
Pod undocumented
223 1206 my ($self, @args) = @_;
224 1206 $self->formatter->log_test(@args);
225 }
226  
227 578 ▶sub info {
Subroutine info: called CC=1 SCAR=0.0
Pod undocumented
228 578 my ($self, @args) = @_;
229 578 $self->formatter->log_test_info(@args);
230 }
231  
232  
233 14 1;
234  
235 __END__
↑ Summary TAP/Formatter/HTML.pm »