/
Text2.pm
206 lines (158 loc) · 4.86 KB
/
Text2.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package Devel::Cover::Report::Text2;
use strict;
use warnings;
our $VERSION = "0.65";
use Devel::Cover::DB 0.65;
use Devel::Cover::Truth_Table;
my %format = (
line => "%4s ",
err => "%3s ",
statement => "%4s ",
condition => "%-24s ",
branch => "%-6s ",
time => "%6s ",
code => "| %s\n",
);
#-------------------------------------------------------------------------------
# Subroutine : headers()
# Purpose : Determine field headers for report.
# Notes :
#-------------------------------------------------------------------------------
sub headers {
my ($db, $options) = @_;
my ($fmt, @data);
for (qw/line err/) {
$fmt .= $format{$_};
push @data, $_;
}
my %cr;
@cr{$db->criteria} = $db->criteria_short;
foreach my $c ($db->criteria) {
next unless $options->{show}{$c};
$fmt .= $format{$c};
push @data, $cr{$c};
}
$fmt .= $format{code};
push @data, 'code';
return $fmt, @data;
}
#-------------------------------------------------------------------------------
# Subroutine : get_metrics()
# Purpose : Determine which metrics to include in report.
# Notes :
#-------------------------------------------------------------------------------
sub get_metrics {
my ($db, $options, $file_data, $line) = @_;
my %m;
for my $c ($db->criteria) { # find all metrics available in db
next unless $options->{show}{$c}; # skip those we don't want in report
my $criterion = $file_data->$c(); # check if metric collected for this file
if ($criterion) { # if it exists...
my $li = $criterion->location($line); # get the metric info for the current line
$m{$c} = $li ? [@$li] : undef; # and stash it
}
}
return %m;
}
#-------------------------------------------------------------------------------
# Subroutine : print_file()
# Purpose : Print report for file.
# Notes :
#-------------------------------------------------------------------------------
sub print_file {
my ($db, $file, $options) = @_;
open(F, '<', $file) or warn("Unable to open '$file' [$!]\n"), return;
my $pct = sprintf("%.1f%%", $db->{summary}{$file}{total}{percentage});
my $pver = join('.', map {ord} split(//, $^V));
print <<EOT;
# File: $file
# Coverage: $pct
# Perl Version: $pver
# Platform: $^O
EOT
my ($fmt, @out) = headers($db, $options);
printf $fmt, @out;
my $file_data = $db->cover->file($file);
while (my $line = <F>) {
chomp $line;
my $error;
my %metric = get_metrics($db, $options, $file_data, $.);
my @out = ([$.], ['']);
foreach my $c ($db->criteria) {
next unless $options->{show}{$c};
push(@out, []), next unless $metric{$c};
my $value = [];
if ($c eq 'branch') {
@$value = $file_data->branch->branch_coverage($.);
$error ||= $file_data->branch->error($.);
}
elsif ($c eq 'condition') {
@$value = map {$_->[0]->text} $file_data->condition->truth_table($.);
$error ||= $file_data->condition->error($.);
}
else {
while (my $o = shift @{$metric{$c}}) {
push @$value, ($c =~ /statement|pod|time/)
? $o->covered : $o->percentage;
$error ||= $o->error;
}
}
push @out, $value;
}
$out[1] = ['***'] if $error; # flag missing coverage
push @out, [$line];
foreach my $i (0 .. max(map {$#$_} @out)) {
no warnings 'uninitialized';
printf $fmt, map{$_->[$i]} @out;
}
last if $line =~ /^__(END|DATA)__/;
}
close F or die "Unable to close '$file' [$!]";
print "\n\n";
}
#-------------------------------------------------------------------------------
# Subroutine : max()
# Purpose : Return the maximum from a list of numbers.
# Notes :
#-------------------------------------------------------------------------------
sub max {
my $max = shift;
foreach (@_) {
$max = $_ if $_ > $max;
}
return $max;
}
#-------------------------------------------------------------------------------
# Subroutine : report()
# Purpose : Entry point for creating textual reports.
# Notes :
#-------------------------------------------------------------------------------
sub report {
my ($pkg, $db, $options) = @_;
foreach my $file (@{$options->{file}}) {
print_file($db, $file, $options);
}
}
1;
__END__
=head1 NAME
Devel::Cover::Report::Text - Backend for textual reporting of coverage
statistics
=head1 SYNOPSIS
use Devel::Cover::Report::Text;
Devel::Cover::Report::Text->report($db, $options);
=head1 DESCRIPTION
This module provides a textual reporting mechanism for coverage data.
It is designed to be called from the C<cover> program.
=head1 SEE ALSO
Devel::Cover
=head1 BUGS
Huh?
=head1 VERSION
Version 0.65 - 8th August 2009
=head1 LICENCE
Copyright 2001-2009, Paul Johnson (pjcj@cpan.org)
This software is free. It is licensed under the same terms as Perl itself.
The latest version of this software should be available from my homepage:
http://www.pjcj.net
=cut