blib/lib/Devel/Cover/Report/Html_subtle.pm untested

filter: Statementstmt 0.0% 0 / 228 Branchbran 0.0% 0 / 74 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 369 CC 38 SCAR 40.1
CC
38
Cov
0%
print_file
156.0
print_summary
90.0
cvg_class
30.0
CRAP 55.4
1 package Devel::Cover::Report::Html_subtle;
2 use strict;
3 use warnings;
4  
5 our $VERSION = '1.52'; # VERSION
6  
7 use Devel::Cover::Html_Common qw( launch ); ## no perlimports
8 use Devel::Cover::Truth_Table; ## no perlimports
9  
10 use Getopt::Long qw( GetOptions );
11 use Template 2.00 ();
12 use HTML::Entities qw( encode_entities );
13  
14 my $Template;
15 my %Filenames;
16 my %File_exists;
17  
18 sub get_options {
19 my ($self, $opt) = @_;
20 $opt->{option}{outputfile} = "coverage.html";
21 die "Invalid command line options" unless GetOptions(
22 $opt->{option}, qw(
23 outputfile=s
24 )
25 );
26 }
27  
28 #-------------------------------------------------------------------------------
29 # Subroutine : cvg_class()
30 # Purpose : Determine the CSS class for an element based on its amount of
31 # coverage.
32 # Notes :
33 #-------------------------------------------------------------------------------
34 sub cvg_class {
35 my ($pc, $err) = @_;
36 defined $err && !$err ? "covered"
37 : $pc < 75 ? "uncovered"
38 : $pc < 90 ? "covered75"
39 : $pc < 100 ? "covered90"
40 : "covered";
41 }
42  
43 #-------------------------------------------------------------------------------
44 # Subroutine : print_stylesheet()
45 # Purpose : Create the stylesheet for HTML reports.
46 # Notes :
47 #-------------------------------------------------------------------------------
48 sub print_stylesheet {
49 my $db = shift;
50 my $file = "$db->{db}/cover.css";
51 open(CSS, '>', $file) or return;
52 my $p = tell(DATA);
53 print CSS <DATA>;
54 seek(DATA, $p, 0);
55 close(CSS);
56 }
57  
58 #-------------------------------------------------------------------------------
59 # Subroutine : print_summary()
60 # Purpose : Print the database summary report.
61 # Notes :
62 #-------------------------------------------------------------------------------
63 sub print_summary {
64  
65 my ($db, $options) = @_;
66 my @showing = grep $options->{show}{$_}, $db->all_criteria;
67 my @headers
68 = map { ($db->all_criteria_short)[$_] }
69 grep { $options->{show}{ ($db->all_criteria)[$_] } }
70 (0 .. $db->all_criteria - 1);
71 my @files = (grep($db->{summary}{$_}, @{ $options->{file} }), 'Total');
72  
73 my %vals;
74  
75 for my $file (@files) {
76 my %pvals;
77 my $part = $db->{summary}{$file};
78 for my $criterion (@showing) {
79 my $pc = exists $part->{$criterion}
80 ? do {
81 my $x = sprintf "%5.2f", $part->{$criterion}{percentage};
82 chop $x;
83 $x
84 }
85 : "n/a";
86  
87 if ($pc ne 'n/a') {
88 if ($criterion ne 'time') {
89 $vals{$file}{$criterion}{class} = cvg_class($pc);
90 }
91 if (exists $Filenames{$file}) {
92 if ($criterion eq 'branch') {
93 $vals{$file}{$criterion}{link} = "$Filenames{$file}--branch.html";
94 } elsif ($criterion eq 'condition') {
95 $vals{$file}{$criterion}{link}
96 = "$Filenames{$file}--condition.html";
97 } elsif ($criterion eq 'subroutine') {
98 $vals{$file}{$criterion}{link}
99 = "$Filenames{$file}--subroutine.html";
100 }
101 }
102 my $c = $part->{$criterion};
103 $vals{$file}{$criterion}{details}
104 = ($c->{covered} || 0) . " / " . ($c->{total} || 0);
105 }
106 $vals{$file}{$criterion}{pc} = $pc;
107 }
108 }
109  
110 my $vars = {
111 title => "Coverage Summary: $db->{db}",
112 dbname => $db->{db},
113 showing => \@showing,
114 headers => \@headers,
115 files => \@files,
116 filenames => \%Filenames,
117 file_exists => \%File_exists,
118 vals => \%vals,
119 };
120  
121 my $html = "$options->{outputdir}/$options->{option}{outputfile}";
122 $Template->process("summary", $vars, $html) or die $Template->error();
123  
124 print "HTML output written to $html\n" unless $options->{silent};
125 }
126  
127 #-------------------------------------------------------------------------------
128 # Subroutine : get_metrics()
129 # Purpose : Determine which metrics to include in report.
130 # Notes :
131 #-------------------------------------------------------------------------------
132 sub get_metrics {
133 my ($db, $options, $file_data, $line) = @_;
134 my %m;
135  
136 for my $c ($db->criteria) { # find all metrics available in db
137 next unless $options->{show}{$c}; # skip those we don't want in report
138 my $criterion = $file_data->$c(); # check if metric collected for this file
139 if ($criterion) { # if it exists...
140 my $li = $criterion->location($line)
141 ; # get the metric info for the current line
142 $m{$c} = $li ? [@$li] : undef; # and stash it
143 }
144 }
145 return %m;
146 }
147  
148 #-------------------------------------------------------------------------------
149 # Subroutine : print_file()
150 # Purpose : Print coverage overview report for a file.
151 # Notes :
152 #-------------------------------------------------------------------------------
153 sub print_file {
154 my ($db, $file, $options) = @_;
155  
156 open(F, '<', $file) or warn("Unable to open '$file' [$!]\n"), return;
157  
158 my @lines;
159 my @showing = grep $options->{show}{$_}, $db->criteria;
160 my @headers = map { ($db->all_criteria_short)[$_] }
161 grep { $options->{show}{ ($db->criteria)[$_] } } (0 .. $db->criteria - 1);
162  
163 my $file_data = $db->cover->file($file);
164  
165 while (my $l = <F>) {
166 chomp $l;
167  
168 my %metric = get_metrics($db, $options, $file_data, $.);
169 my %line = (number => $., text => encode_entities($l), metrics => []);
170 $line{text} =~ s/\t/ /g;
171 $line{text} =~ s/\s/&nbsp;/g; # IE doesn't honor "white-space: pre" CSS
172  
173 foreach my $c ($db->criteria) {
174 next unless $options->{show}{$c};
175 push(@{ $line{metrics} }, []), next unless $metric{$c};
176  
177 if ($c eq 'branch') {
178 my @p;
179 foreach (@{ $file_data->branch->get($.) }) {
180 push @p, {
181 text => sprintf("%.0f", $_->percentage),
182 class => cvg_class($_->percentage),
183 link => "$Filenames{$file}--branch.html#line$.",
184 };
185 }
186 push @{ $line{metrics} }, \@p;
187 } elsif ($c eq 'condition') {
188 my @tt = $file_data->condition->truth_table($.);
189 my @p;
190 if (@tt) {
191 foreach (@tt) {
192 push @p, {
193 text => sprintf("%.0f", $_->[0]->percentage),
194 class => cvg_class($_->[0]->percentage),
195 link => "$Filenames{$file}--condition.html#line$.",
196 };
197 }
198 } else {
199 push @p, { text => "expression contains > 16 terms: ignored" };
200 }
201 push @{ $line{metrics} }, \@p;
202 } elsif ($c eq 'subroutine') {
203 my @p;
204 while (my $o = shift @{ $metric{$c} }) {
205 push @p, {
206 text => $o->covered,
207 class => $o->error ? 'uncovered' : 'covered',
208 link => "$Filenames{$file}--subroutine.html#line$.",
209 };
210 }
211 push @{ $line{metrics} }, \@p;
212 } else {
213 my @p;
214 while (my $o = shift @{ $metric{$c} }) {
215 push @p, {
216 text => ($c =~ /statement|pod|time/) ? $o->covered
217 : $o->percentage,
218 class => $c eq 'time' ? undef
219 : $o->error ? 'uncovered'
220 : 'covered',
221 link => undef,
222 };
223 }
224 push @{ $line{metrics} }, \@p;
225 }
226 }
227 push @lines, \%line;
228 last if $l =~ /^__(END|DATA)__/;
229 }
230 close F or die "Unable to close '$file' [$!]";
231  
232 my $vars = {
233 title => "File Coverage: $file",
234 file => $file,
235 percentage => sprintf("%.1f", $db->{summary}{$file}{total}{percentage}),
236 class => cvg_class($db->{summary}{$file}{total}{percentage}),
237 showing => \@showing,
238 headers => \@headers,
239 filenames => \%Filenames,
240 file_exists => \%File_exists,
241 lines => \@lines,
242 perlver => join('.', map { ord } split(//, $^V)), # should come from db
243 platform => $^O, # should come from db
244 };
245  
246 my $html = "$options->{outputdir}/$Filenames{$file}.html";
247 $Template->process("file", $vars, $html) or die $Template->error();
248 }
249  
250 #-------------------------------------------------------------------------------
251 # Subroutine : print_branches()
252 # Purpose : Print branch coverage report for a file.
253 # Notes :
254 #-------------------------------------------------------------------------------
255 sub print_branches {
256 my ($db, $file, $options) = @_;
257  
258 my $branches = $db->cover->file($file)->branch;
259  
260 return unless $branches;
261  
262 my @branches;
263 for my $location (sort { $a <=> $b } $branches->items) {
264 my $count = 0;
265 for my $b (@{ $branches->location($location) }) {
266 my @tf = $b->values;
267 push @branches, {
268 ref => "line$location",
269 number => $count++ ? undef : $location,
270 percentage => sprintf("%.0f", $b->percentage),
271 class => cvg_class($b->percentage),
272 parts => [
273 { text => 'T', class => $tf[0] ? 'covered' : 'uncovered' },
274 { text => 'F', class => $tf[1] ? 'covered' : 'uncovered' },
275 ],
276 text => encode_entities($b->text),
277 };
278 }
279 }
280  
281 my $vars = {
282 title => "Branch Coverage: $file",
283 file => $file,
284 percentage => sprintf("%.1f", $db->{summary}{$file}{branch}{percentage}),
285 class => cvg_class($db->{summary}{$file}{branch}{percentage}),
286 branches => \@branches,
287 perlver => join('.', map { ord } split(//, $^V)), # should come from db
288 platform => $^O, # should come from db
289 };
290  
291 my $html = "$options->{outputdir}/$Filenames{$file}--branch.html";
292 $Template->process("branches", $vars, $html) or die $Template->error();
293 }
294  
295 #-------------------------------------------------------------------------------
296 # Subroutine : print_conditions()
297 # Purpose : Print condition coverage report for a file.
298 # Notes :
299 #-------------------------------------------------------------------------------
300 sub print_conditions {
301 my ($db, $file, $options) = @_;
302 my $conditions = $db->cover->file($file)->condition;
303 return unless $conditions;
304  
305 my @data;
306 for my $location (sort { $a <=> $b } $conditions->items) {
307 my @x = $conditions->truth_table($location);
308  
309 for my $c (@x) {
310 push @data, {
311 line => $location,
312 ref => "line$location",
313 percentage => sprintf("%.0f", $c->[0]->percentage),
314 class => cvg_class($c->[0]->percentage),
315 condition => encode_entities($c->[1]),
316 coverage => $c->[0]->html,
317 };
318 }
319 }
320  
321 my $vars = {
322 title => "Condition Coverage: $file",
323 file => $file,
324 percentage =>
325 sprintf("%.1f", $db->{summary}{$file}{condition}{percentage}),
326 class => cvg_class($db->{summary}{$file}{condition}{percentage}),
327 headers => [ 'line', '%', 'coverage', 'condition' ],
328 conditions => \@data,
329 perlver => join('.', map { ord } split(//, $^V)), # should come from db
330 platform => $^O, # should come from db
331 };
332  
333 my $html = "$db->{db}/$Filenames{$file}--condition.html";
334 $Template->process("conditions", $vars, $html) or die $Template->error();
335 }
336  
337 sub print_subroutines {
338 my ($db, $file, $options) = @_;
339 my $subroutines = $db->cover->file($file)->subroutine;
340 return unless $subroutines;
341  
342 my @data;
343 for my $location ($subroutines->items) {
344 my $l = $subroutines->location($location);
345 for my $sub (@$l) {
346 push @data, {
347 ref => "line$location",
348 line => $location,
349 name => $sub->name,
350 class => cvg_class($sub->percentage),
351 }
352 }
353 }
354  
355 my $vars = {
356 title => "Subroutine Coverage: $file",
357 file => $file,
358 percentage =>
359 sprintf("%.1f", $db->{summary}{$file}{subroutine}{percentage}),
360 class => cvg_class($db->{summary}{$file}{subroutine}{percentage}),
361 subroutines => [ sort { $a->{name} cmp $b->{name} } @data ],
362 perlver => join('.', map { ord } split(//, $^V)), # should come from db
363 platform => $^O, # should come from db
364 };
365  
366 my $html = "$db->{db}/$Filenames{$file}--subroutine.html";
367 $Template->process("subroutines", $vars, $html) or die $Template->error();
368 }
369  
370 #-------------------------------------------------------------------------------
371 # Subroutine : report()
372 # Purpose : Entry point for printing HTML reports.
373 # Notes :
374 #-------------------------------------------------------------------------------
375 sub report {
376 my ($pkg, $db, $options) = @_;
377  
378 $Template = Template->new({
379 LOAD_TEMPLATES =>
380 [ Devel::Cover::Report::Html_subtle::Template::Provider->new({}) ]
381 });
382  
383 %Filenames
384 = map { $_ => do { (my $f = $_) =~ s/\W/-/g; $f } } @{ $options->{file} };
385 %File_exists = map { $_ => -e } @{ $options->{file} };
386  
387 print_stylesheet($db);
388  
389 for my $file (@{ $options->{file} }) {
390 print_file($db, $file, $options);
391 print_branches($db, $file, $options) if $options->{show}{branch};
392 print_conditions($db, $file, $options) if $options->{show}{condition};
393 print_subroutines($db, $file, $options) if $options->{show}{subroutine};
394 }
395 print_summary($db, $options);
396 }
397  
398 1;
399  
400 package Devel::Cover::Report::Html_subtle::Template::Provider;
401 use strict;
402 use warnings;
403  
404 our $VERSION = '1.52'; # VERSION
405  
406 use base "Template::Provider";
407  
408 my %Templates;
409  
410 sub fetch {
411 my $self = shift;
412 my ($name) = @_;
413  
414 # print "Looking for <$name>\n";
415 $self->SUPER::fetch(exists $Templates{$name} ? \$Templates{$name} : $name);
416 }
417  
418 #<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
419 $Templates{html} = <<'EOT';
420 <?xml version="1.0" encoding="utf-8"?>
421 <!--
422 This file was generated by Devel::Cover Version $VERSION
423 Devel::Cover is copyright 2001-2026, Paul Johnson (paul\@pjcj.net)
424 Devel::Cover is free. It is licensed under the same terms as Perl itself.
425 The latest version of Devel::Cover should be available from my homepage:
426 https://pjcj.net
427 -->
428 <!DOCTYPE html
429 PUBLIC "-//W3C//DTD XHTML Basic 1.0//EN"
430 "https://www.w3.org/TR/xhtml-basic/xhtml-basic10.dtd">
431 <html xmlns="https://www.w3.org/1999/xhtml">
432 <head>
433 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></meta>
434 <meta http-equiv="Content-Language" content="en-us"></meta>
435 <link rel="stylesheet" type="text/css" href="cover.css"></link>
436 <title> [% title %] </title>
437 </head>
438 <body>
439 [% content %]
440 </body>
441 </html>
442 EOT
443  
444 $Templates{summary} = <<'EOT';
445 [% WRAPPER html %]
446  
447 <h1>Coverage Summary</h1>
448 <table>
449 <tr>
450 <td class="header" align="right">Database:</td>
451 <td>[% dbname %]</td>
452 </tr>
453 </table>
454 <div><br></br></div>
455 <table>
456  
457 <tr>
458 <th align="left" class="header"> File </th>
459 [% FOREACH header = headers %]
460 <th class="header"> [% header %] </th>
461 [% END %]
462 </tr>
463  
464 [% FOREACH file = files %]
465 <tr align="center" valign="top">
466 <td align="left">
467 [% IF file_exists.$file %]
468 <a href="[%- filenames.$file -%].html"> [% file %] </a>
469 [% ELSE %]
470 [% file %]
471 [% END %]
472 </td>
473  
474 [% FOREACH criterion = showing %]
475 [% IF vals.$file.$criterion.class %]
476 <td class="[%- vals.$file.$criterion.class -%]"
477 title="[%- vals.$file.$criterion.details -%]">
478 [% ELSE %]
479 <td>
480 [% END %]
481 [% IF vals.$file.$criterion.link.defined%]
482 <a href="[% vals.$file.$criterion.link %]">
483 [% vals.$file.$criterion.pc %]
484 </a>
485 [% ELSE %]
486 [% vals.$file.$criterion.pc %]
487 [% END %]
488 </td>
489 [% END %]
490 </tr>
491 [% END %]
492  
493 </table>
494  
495 [% END %]
496 EOT
497  
498 $Templates{branches} = <<'EOT';
499 [% WRAPPER html %]
500  
501 <h1>Branch Coverage</h1>
502 <table>
503 <tr>
504 <td class="header" align="right">File:</td>
505 <td>[% file %]</td>
506 </tr>
507 <tr>
508 <td class="header" align="right">Coverage:</td>
509 <td class="[% class %]">[% percentage %]%</td>
510 </tr>
511 <tr>
512 <td class="header" align="right">Perl version:</td>
513 <td>[% perlver %]</td>
514 </tr>
515 <tr>
516 <td class="header" align="right">Platform:</td>
517 <td>[% platform %]</td>
518 </tr>
519 </table>
520 <div><br></br></div>
521 <table>
522 <tr valign="top">
523 <th class="header"> line </th>
524 <th class="header"> % </th>
525 <th colspan="2" class="header"> coverage </th>
526 <th class="header"> branch </th>
527 </tr>
528  
529 [% FOREACH branch = branches %]
530 <tr align="center" valign="top">
531 <td class="header">
532 [% IF branch.number.defined %]
533 <a id="[% branch.ref %]">[% branch.number %]</a>
534 [% ELSE %]
535 [% branch.number %]
536 [% END %]
537 </td>
538 <td class="[% branch.class %]"> [% branch.percentage %] </td>
539 [% FOREACH part = branch.parts %]
540 <td class="[% part.class %]"> [% part.text %] </td>
541 [% END %]
542 <td align="left">
543 <code>[% branch.text %]</code>
544 </td>
545 </tr>
546 [% END %]
547  
548 </table>
549  
550 [% END %]
551 EOT
552  
553 $Templates{conditions} = <<'EOT';
554 [% WRAPPER html %]
555  
556 <h1>Condition Coverage</h1>
557 <table>
558 <tr>
559 <td class="header" align="right">File:</td>
560 <td>[% file %]</td>
561 </tr>
562 <tr>
563 <td class="header" align="right">Coverage:</td>
564 <td class="[% class %]">[% percentage %]%</td>
565 </tr>
566 <tr>
567 <td class="header" align="right">Perl version:</td>
568 <td>[% perlver %]</td>
569 </tr>
570 <tr>
571 <td class="header" align="right">Platform:</td>
572 <td>[% platform %]</td>
573 </tr>
574 </table>
575 <div><br></br></div>
576 <table>
577 <tr>
578 [% FOREACH header = headers %]
579 <th class="header"> [% header %] </th>
580 [% END %]
581 </tr>
582  
583 [% FOREACH cond = conditions %]
584 <tr valign="top">
585 <td align="center" class="header"><a id="[% cond.ref %]">
586 [% cond.line %]
587 </a></td>
588 <td align="center" class="[% cond.class %]">
589 [% cond.percentage %]
590 </td>
591 <td><div>
592 [% cond.coverage %]
593 </div></td>
594 <td>
595 <code>[% cond.condition %]</code>
596 </td>
597 </tr>
598 [% END %]
599  
600 </table>
601  
602 [% END %]
603 EOT
604  
605 $Templates{subroutines} = <<'EOT';
606 [% WRAPPER html %]
607  
608 <h1>Subroutine Coverage</h1>
609 <table>
610 <tr>
611 <td class="header" align="right">File:</td>
612 <td>[% file %]</td>
613 </tr>
614 <tr>
615 <td class="header" align="right">Coverage:</td>
616 <td class="[% class %]">[% percentage %]%</td>
617 </tr>
618 <tr>
619 <td class="header" align="right">Perl version:</td>
620 <td>[% perlver %]</td>
621 </tr>
622 <tr>
623 <td class="header" align="right">Platform:</td>
624 <td>[% platform %]</td>
625 </tr>
626 </table>
627 <div><br></br></div>
628 <table>
629 <tr valign="top">
630 <th class="header"> subroutine </th>
631 <th class="header"> line </th>
632 </tr>
633  
634 [% FOREACH sub = subroutines %]
635 <tr align="center" valign="top">
636 <td class="[% sub.class %]"> <a id="[% sub.ref %]"> [% sub.name %] </td>
637 <td> [% sub.line %] </td>
638 </tr>
639 [% END %]
640  
641 </table>
642  
643 [% END %]
644 EOT
645  
646 $Templates{file} = <<'EOT';
647 [% WRAPPER html %]
648  
649 <h1>File Coverage</h1>
650 <table>
651 <tr>
652 <td class="header" align="right">File:</td>
653 <td>[% file %]</td>
654 </tr>
655 <tr>
656 <td class="header" align="right">Coverage:</td>
657 <td class="[% class %]">[% percentage %]%</td>
658 </tr>
659 <tr>
660 <td class="header" align="right">Perl version:</td>
661 <td>[% perlver %]</td>
662 </tr>
663 <tr>
664 <td class="header" align="right">Platform:</td>
665 <td>[% platform %]</td>
666 </tr>
667 </table>
668 <div><br></br></div>
669 <table>
670  
671 <tr>
672 <th class="header">line</th>
673 [% FOREACH header = headers %]
674 <th class="header">[% header %]</th>
675 [% END %]
676 <th class="header">code</th>
677 </tr>
678  
679 [% FOREACH line = lines %]
680 <tr align="center" valign="top">
681 <td class="header">[% line.number %]</td>
682 [% FOREACH metric = line.metrics %]
683 <td>
684 [% FOREACH cr = metric %]
685 [% IF cr.class.defined %]
686 <div class="[% cr.class %]">
687 [% ELSE %]
688 <div>
689 [% END %]
690 [% IF cr.link.defined %]
691 <a href="[% cr.link %]">[% cr.text %]</a>
692 [% ELSE %]
693 [% cr.text %]
694 [% END %]
695 </div>
696 [% END %]
697 </td>
698 [% END %]
699 <td align="left">
700 <code>[% line.text %]</code>
701 </td>
702 </tr>
703 [% END %]
704  
705 </table>
706  
707 [% END %]
708 EOT
709  
710 # remove some whitespace from templates
711 s/^\s+//gm for values %Templates;
712  
713 1;
714  
715 =pod
716  
717 =head1 NAME
718  
719 Devel::Cover::Report::Html_subtle - HTML backend for Devel::Cover
720  
721 =head1 VERSION
722  
723 version 1.52
724  
725 =head1 SYNOPSIS
726  
727 cover -report html_subtle
728  
729 =head1 DESCRIPTION
730  
731 This module provides a HTML reporting mechanism for coverage data. It
732 is designed to be called from the C<cover> program.
733  
734 Based on an original by Paul Johnson, the output was greatly improved by
735 Michael Carman (mjcarman@mchsi.com).
736  
737 =head1 SEE ALSO
738  
739 Devel::Cover
740  
741 =head1 LICENCE
742  
743 Copyright 2001-2026, Paul Johnson (paul@pjcj.net)
744  
745 This software is free. It is licensed under the same terms as Perl itself.
746  
747 The latest version of this software should be available from my homepage:
748 https://pjcj.net
749  
750 =cut
751  
752 package Devel::Cover::Report::Html_subtle;
753  
754 __DATA__