| 1 | package Devel::Cover::Report::Text2; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | our $VERSION = '1.52'; # VERSION | ||
| 6 | |||
| 7 | use Devel::Cover::DB; | ||
| 8 | use Devel::Cover::Truth_Table; | ||
| 9 | |||
| 10 | my %format = ( | ||
| 11 | line => "%4s ", | ||
| 12 | err => "%3s ", | ||
| 13 | statement => "%4s ", | ||
| 14 | condition => "%-24s ", | ||
| 15 | branch => "%-6s ", | ||
| 16 | time => "%6s ", | ||
| 17 | code => "| %s\n", | ||
| 18 | ); | ||
| 19 | |||
| 20 | #------------------------------------------------------------------------------- | ||
| 21 | # Subroutine : headers() | ||
| 22 | # Purpose : Determine field headers for report. | ||
| 23 | # Notes : | ||
| 24 | #------------------------------------------------------------------------------- | ||
| 25 | sub headers { | ||
| 26 | my ($db, $options) = @_; | ||
| 27 | my ($fmt, @data); | ||
| 28 | |||
| 29 | for (qw/line err/) { | ||
| 30 | $fmt .= $format{$_}; | ||
| 31 | push @data, $_; | ||
| 32 | } | ||
| 33 | |||
| 34 | my %cr; | ||
| 35 | @cr{ $db->criteria } = $db->criteria_short; | ||
| 36 | foreach my $c ($db->criteria) { | ||
| 37 | next unless $options->{show}{$c}; | ||
| 38 | $fmt .= $format{$c}; | ||
| 39 | push @data, $cr{$c}; | ||
| 40 | } | ||
| 41 | $fmt .= $format{code}; | ||
| 42 | push @data, 'code'; | ||
| 43 | |||
| 44 | return $fmt, @data; | ||
| 45 | } | ||
| 46 | |||
| 47 | #------------------------------------------------------------------------------- | ||
| 48 | # Subroutine : get_metrics() | ||
| 49 | # Purpose : Determine which metrics to include in report. | ||
| 50 | # Notes : | ||
| 51 | #------------------------------------------------------------------------------- | ||
| 52 | sub get_metrics { | ||
| 53 | my ($db, $options, $file_data, $line) = @_; | ||
| 54 | my %m; | ||
| 55 | |||
| 56 | for my $c ($db->criteria) { # find all metrics available in db | ||
| 57 | next unless $options->{show}{$c}; # skip those we don't want in report | ||
| 58 | my $criterion = $file_data->$c(); # check if metric collected for this file | ||
| 59 | if ($criterion) { # if it exists... | ||
| 60 | my $li = $criterion->location($line) | ||
| 61 | ; # get the metric info for the current line | ||
| 62 | $m{$c} = $li ? [@$li] : undef; # and stash it | ||
| 63 | } | ||
| 64 | } | ||
| 65 | return %m; | ||
| 66 | } | ||
| 67 | |||
| 68 | #------------------------------------------------------------------------------- | ||
| 69 | # Subroutine : print_file() | ||
| 70 | # Purpose : Print report for file. | ||
| 71 | # Notes : | ||
| 72 | #------------------------------------------------------------------------------- | ||
| 73 | sub print_file { | ||
| 74 | my ($db, $file, $options) = @_; | ||
| 75 | |||
| 76 | open(F, '<', $file) or warn("Unable to open '$file' [$!]\n"), return; | ||
| 77 | |||
| 78 | my $pct = sprintf("%.1f%%", $db->{summary}{$file}{total}{percentage}); | ||
| 79 | my $pver = join('.', map { ord } split(//, $^V)); | ||
| 80 | print <<EOT; | ||
| 81 | # File: $file | ||
| 82 | # Coverage: $pct | ||
| 83 | # Perl Version: $pver | ||
| 84 | # Platform: $^O | ||
| 85 | |||
| 86 | EOT | ||
| 87 | |||
| 88 | my ($fmt, @out) = headers($db, $options); | ||
| 89 | printf $fmt, @out; | ||
| 90 | |||
| 91 | my $file_data = $db->cover->file($file); | ||
| 92 | while (my $line = <F>) { | ||
| 93 | chomp $line; | ||
| 94 | |||
| 95 | my $error; | ||
| 96 | my %metric = get_metrics($db, $options, $file_data, $.); | ||
| 97 | my @out = ([$.], ['']); | ||
| 98 | |||
| 99 | foreach my $c ($db->criteria) { | ||
| 100 | next unless $options->{show}{$c}; | ||
| 101 | push(@out, []), next unless $metric{$c}; | ||
| 102 | |||
| 103 | my $value = []; | ||
| 104 | if ($c eq 'branch') { | ||
| 105 | @$value = $file_data->branch->branch_coverage($.); | ||
| 106 | $error ||= $file_data->branch->error($.); | ||
| 107 | } elsif ($c eq 'condition') { | ||
| 108 | @$value = map { $_->[0]->text } $file_data->condition->truth_table($.); | ||
| 109 | $error ||= $file_data->condition->error($.); | ||
| 110 | } else { | ||
| 111 | while (my $o = shift @{ $metric{$c} }) { | ||
| 112 | push @$value, | ||
| 113 | ($c =~ /statement|pod|time/) ? $o->covered : $o->percentage; | ||
| 114 | $error ||= $o->error; | ||
| 115 | } | ||
| 116 | } | ||
| 117 | push @out, $value; | ||
| 118 | } | ||
| 119 | |||
| 120 | $out[1] = ['***'] if $error; # flag missing coverage | ||
| 121 | push @out, [$line]; | ||
| 122 | |||
| 123 | foreach my $i (0 .. max(map { $#$_ } @out)) { | ||
| 124 | no warnings 'uninitialized'; | ||
| 125 | printf $fmt, map { $_->[$i] } @out; | ||
| 126 | } | ||
| 127 | |||
| 128 | last if $line =~ /^__(END|DATA)__/; | ||
| 129 | } | ||
| 130 | close F or die "Unable to close '$file' [$!]"; | ||
| 131 | print "\n\n"; | ||
| 132 | } | ||
| 133 | |||
| 134 | #------------------------------------------------------------------------------- | ||
| 135 | # Subroutine : max() | ||
| 136 | # Purpose : Return the maximum from a list of numbers. | ||
| 137 | # Notes : | ||
| 138 | #------------------------------------------------------------------------------- | ||
| 139 | sub max { | ||
| 140 | my $max = shift; | ||
| 141 | foreach (@_) { | ||
| 142 | $max = $_ if $_ > $max; | ||
| 143 | } | ||
| 144 | return $max; | ||
| 145 | } | ||
| 146 | |||
| 147 | #------------------------------------------------------------------------------- | ||
| 148 | # Subroutine : report() | ||
| 149 | # Purpose : Entry point for creating textual reports. | ||
| 150 | # Notes : | ||
| 151 | #------------------------------------------------------------------------------- | ||
| 152 | sub report { | ||
| 153 | my ($pkg, $db, $options) = @_; | ||
| 154 | foreach my $file (@{ $options->{file} }) { | ||
| 155 | print_file($db, $file, $options); | ||
| 156 | } | ||
| 157 | } | ||
| 158 | |||
| 159 | 1; | ||
| 160 | |||
| 161 | __END__ |