| 1 | # Copyright 2001-2026, Paul Johnson (paul@pjcj.net) | ||
| 2 | |||
| 3 | # This software is free. It is licensed under the same terms as Perl itself. | ||
| 4 | |||
| 5 | # The latest version of this software should be available from my homepage: | ||
| 6 | # https://pjcj.net | ||
| 7 | |||
| 8 | package Devel::Cover::Report::Text; | ||
| 9 | |||
| 10 | use strict; | ||
| 11 | use warnings; | ||
| 12 | |||
| 13 | our $VERSION = '1.52'; # VERSION | ||
| 14 | |||
| 15 | use Devel::Cover::DB; | ||
| 16 | |||
| 17 | sub print_runs { | ||
| 18 | my ($db, $options) = @_; | ||
| 19 | for my $r (sort { $a->{start} <=> $b->{start} } $db->runs) { | ||
| 20 | print "Run: ", $r->run, "\n"; | ||
| 21 | print "Perl version: ", $r->perl, "\n"; | ||
| 22 | print "OS: ", $r->OS, "\n"; | ||
| 23 | print "Start: ", scalar gmtime $r->start, "\n"; | ||
| 24 | print "Finish: ", scalar gmtime $r->finish, "\n"; | ||
| 25 | print "\n"; | ||
| 26 | # use Devel::Cover::Dumper; print Dumper $r; | ||
| 27 | } | ||
| 28 | } | ||
| 29 | |||
| 30 | sub print_statement { | ||
| 31 | my ($db, $file, $options) = @_; | ||
| 32 | |||
| 33 | my $cover = $db->cover; | ||
| 34 | |||
| 35 | print "$file\n\n"; | ||
| 36 | my $f = $cover->file($file); | ||
| 37 | |||
| 38 | my $fmt = "%-5s %3s "; | ||
| 39 | my @args = ("line", "err"); | ||
| 40 | for my $ann (@{ $options->{annotations} }) { | ||
| 41 | for my $a (0 .. $ann->count - 1) { | ||
| 42 | $fmt .= "%-" . $ann->width($a) . "s "; | ||
| 43 | push @args, $ann->header($a); | ||
| 44 | } | ||
| 45 | } | ||
| 46 | my %cr; | ||
| 47 | @cr{ $db->criteria } = $db->criteria_short; | ||
| 48 | for my $c ($db->criteria) { | ||
| 49 | if ($options->{show}{$c}) { | ||
| 50 | $fmt .= "%6s "; | ||
| 51 | push @args, $cr{$c}; | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | $fmt .= " %s\n"; | ||
| 56 | push @args, "code"; | ||
| 57 | |||
| 58 | printf $fmt, @args; | ||
| 59 | |||
| 60 | my $autoloader = 0; | ||
| 61 | |||
| 62 | open F, $file or warn("Unable to open $file: $!\n"), return; | ||
| 63 | |||
| 64 | LINE: while (defined(my $l = <F>)) { | ||
| 65 | chomp $l; | ||
| 66 | my $n = $.; | ||
| 67 | $autoloader ||= $l =~ /use\s+AutoLoader/; | ||
| 68 | |||
| 69 | my %criteria; | ||
| 70 | for my $c ($db->criteria) { | ||
| 71 | next unless $options->{show}{$c}; | ||
| 72 | my $criterion = $f->$c(); | ||
| 73 | if ($criterion) { | ||
| 74 | my $l = $criterion->location($n); | ||
| 75 | $criteria{$c} = $l ? [@$l] : $l; | ||
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | # use Devel::Cover::Dumper; print STDERR Dumper(\%criteria); | ||
| 80 | my $more = 1; | ||
| 81 | while ($more) { | ||
| 82 | my @args = ($n, ""); | ||
| 83 | my $error = 0; | ||
| 84 | |||
| 85 | for my $ann (@{ $options->{annotations} }) { | ||
| 86 | for my $a (0 .. $ann->count - 1) { | ||
| 87 | push @args, substr $ann->text($file, $n, $a), 0, $ann->width($a); | ||
| 88 | $error ||= $ann->error($file, $n, $a); | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | $more = 0; | ||
| 93 | for my $c ($db->criteria) { | ||
| 94 | next unless $options->{show}{$c}; | ||
| 95 | my $o = shift @{ $criteria{$c} }; | ||
| 96 | $more ||= @{ $criteria{$c} }; | ||
| 97 | my $value | ||
| 98 | = $o | ||
| 99 | ? ($c =~ /statement|sub|pod|time/) | ||
| 100 | ? $o->covered | ||
| 101 | : $o->percentage | ||
| 102 | : ""; | ||
| 103 | $value = "-" . $value if $o && $o->uncoverable; | ||
| 104 | $value = "*" . $value if $o && $o->error; | ||
| 105 | push @args, $value; | ||
| 106 | $error ||= $o->error if $o; | ||
| 107 | } | ||
| 108 | |||
| 109 | $args[1] = "***" if $error; | ||
| 110 | push @args, $l; | ||
| 111 | |||
| 112 | # print join(", ", map { "[$_]" } @args), "\n"; | ||
| 113 | printf $fmt, @args; | ||
| 114 | |||
| 115 | last LINE if !$autoloader && $l =~ /^__(END|DATA)__/; | ||
| 116 | $n = $l = ""; | ||
| 117 | } | ||
| 118 | } | ||
| 119 | |||
| 120 | close F or die "Unable to close $file: $!"; | ||
| 121 | print "\n\n"; | ||
| 122 | } | ||
| 123 | |||
| 124 | sub print_branches { | ||
| 125 | my ($db, $file, $options) = @_; | ||
| 126 | |||
| 127 | my $branches = $db->cover->file($file)->branch; | ||
| 128 | |||
| 129 | return unless $branches; | ||
| 130 | |||
| 131 | print "Branches\n"; | ||
| 132 | print "--------\n\n"; | ||
| 133 | |||
| 134 | my $tpl = "%-5s %3s %6s %6s %6s %s\n"; | ||
| 135 | printf $tpl, "line", "err", "%", "true", "false", "branch"; | ||
| 136 | printf $tpl, "-----", "---", ("------") x 3, "------"; | ||
| 137 | |||
| 138 | for my $location (sort { $a <=> $b } $branches->items) { | ||
| 139 | my $n = 0; | ||
| 140 | for my $b (@{ $branches->location($location) }) { | ||
| 141 | printf $tpl, $n ? "" : $location, $b->error ? "***" : "", | ||
| 142 | ($b->uncoverable ? "-" : "") . $b->percentage, | ||
| 143 | map (($b->uncoverable($_) ? "-" : "") . ($b->covered($_) || 0), | ||
| 144 | 0 .. $b->total - 1), | ||
| 145 | $b->text; | ||
| 146 | $n++; | ||
| 147 | } | ||
| 148 | } | ||
| 149 | |||
| 150 | print "\n\n"; | ||
| 151 | } | ||
| 152 | |||
| 153 | sub print_conditions { | ||
| 154 | my ($db, $file, $options) = @_; | ||
| 155 | |||
| 156 | my $conditions = $db->cover->file($file)->condition; | ||
| 157 | |||
| 158 | return unless $conditions; | ||
| 159 | |||
| 160 | my $template = sub { "%-5s %3s %6s " . ("%6s " x shift) . " %s\n" }; | ||
| 161 | |||
| 162 | my %r; | ||
| 163 | for my $location (sort { $a <=> $b } $conditions->items) { | ||
| 164 | my %seen; | ||
| 165 | for my $c (@{ $conditions->location($location) }) { | ||
| 166 | push @{ $r{ $c->type } }, [ $c, $seen{ $c->type }++ ? "" : $location ]; | ||
| 167 | } | ||
| 168 | } | ||
| 169 | |||
| 170 | print "Conditions\n"; | ||
| 171 | print "----------\n\n"; | ||
| 172 | |||
| 173 | my %seen; | ||
| 174 | for my $type (sort keys %r) { | ||
| 175 | my $tpl; | ||
| 176 | for (@{ $r{$type} }) { | ||
| 177 | my ($c, $location) = @$_; | ||
| 178 | unless ($seen{$type}++) { | ||
| 179 | my $headers = $c->headers; | ||
| 180 | my $nh = @$headers; | ||
| 181 | $tpl = $template->($nh); | ||
| 182 | (my $t = $type) =~ s/_/ /g; | ||
| 183 | print "$t conditions\n\n"; | ||
| 184 | printf $tpl, "line", "err", "%", @$headers, "expr"; | ||
| 185 | printf $tpl, "-----", "---", ("------") x ($nh + 1), "----"; | ||
| 186 | } | ||
| 187 | printf $tpl, $location, $c->error ? "***" : "", | ||
| 188 | ($c->uncoverable ? "-" : "") . $c->percentage, | ||
| 189 | map (($c->uncoverable($_) ? "-" : "") . ($c->covered($_) || 0), | ||
| 190 | 0 .. $c->total - 1), | ||
| 191 | $c->text; | ||
| 192 | } | ||
| 193 | print "\n"; | ||
| 194 | } | ||
| 195 | |||
| 196 | print "\n"; | ||
| 197 | } | ||
| 198 | |||
| 199 | sub print_subroutines { | ||
| 200 | |||
| 201 | my ($db, $file, $options) = @_; | ||
| 202 | |||
| 203 | my $dfile = $db->cover->file($file); | ||
| 204 | my $subs = $dfile->subroutine or return; | ||
| 205 | my $pods = $options->{show}{pod} && $dfile->pod; | ||
| 206 | my $maxh = 8; | ||
| 207 | my $maxc = 5; | ||
| 208 | my $maxp = 3; | ||
| 209 | my $maxs = 10; | ||
| 210 | my %subs; | ||
| 211 | |||
| 212 | for my $location ($subs->items) { | ||
| 213 | my $l = $subs->location($location); | ||
| 214 | my $d = $pods && $pods->location($location); | ||
| 215 | for my $sub (@$l) { | ||
| 216 | |||
| 217 | my $h = "$file:$location"; | ||
| 218 | my $c = ($sub->uncoverable ? "-" : "") . $sub->covered; | ||
| 219 | my $e = $pods && shift @$d; | ||
| 220 | my $p = $e ? ($e->uncoverable ? "-" : "") . $e->covered : ""; | ||
| 221 | my $s = $sub->name; | ||
| 222 | |||
| 223 | $maxh = length $h if length $h > $maxh; | ||
| 224 | $maxc = length $c if length $c > $maxc; | ||
| 225 | $maxp = length $p if $p && length $p > $maxp; | ||
| 226 | $maxs = length $s if length $s > $maxs; | ||
| 227 | push @{ $subs{ $sub->covered ? "covered" : "uncovered" }{$s} }, | ||
| 228 | [ $c, $pods ? $p : (), $h ]; | ||
| 229 | } | ||
| 230 | } | ||
| 231 | |||
| 232 | my $template = "%-${maxs}s %${maxc}s "; | ||
| 233 | $template .= "%${maxp}s " if $pods; | ||
| 234 | $template .= "%-${maxh}s\n"; | ||
| 235 | |||
| 236 | for my $type (sort keys %subs) { | ||
| 237 | print ucfirst $type, " Subroutines\n"; | ||
| 238 | print "-" x (12 + length $type), "\n\n"; | ||
| 239 | printf $template, "Subroutine", "Count", $pods ? "Pod" : (), "Location"; | ||
| 240 | printf $template, "-" x $maxs, "-" x $maxc, $pods ? "-" x $maxp : (), | ||
| 241 | "-" x $maxh; | ||
| 242 | |||
| 243 | for my $s (sort keys %{ $subs{$type} }) { | ||
| 244 | printf $template, $s, @$_ | ||
| 245 | for sort { $a->[-1] cmp $b->[-1] } @{ $subs{$type}{$s} }; | ||
| 246 | } | ||
| 247 | print "\n"; | ||
| 248 | } | ||
| 249 | |||
| 250 | print "\n"; | ||
| 251 | } | ||
| 252 | |||
| 253 | sub report { | ||
| 254 | my ($pkg, $db, $options) = @_; | ||
| 255 | |||
| 256 | print_runs($db, $options); | ||
| 257 | for my $file (@{ $options->{file} }) { | ||
| 258 | print_statement($db, $file, $options) if $options->{show}{statement}; | ||
| 259 | print_branches($db, $file, $options) if $options->{show}{branch}; | ||
| 260 | print_conditions($db, $file, $options) if $options->{show}{condition}; | ||
| 261 | print_subroutines($db, $file, $options) | ||
| 262 | if $options->{show}{subroutine} || $options->{show}{pod}; | ||
| 263 | } | ||
| 264 | } | ||
| 265 | |||
| 266 | 1 | ||
| 267 | |||
| 268 | __END__ |