| 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::Branch; | ||
| 9 | |||
| 10 | use strict; | ||
| 11 | use warnings; | ||
| 12 | |||
| 13 | our $VERSION = '1.52'; # VERSION | ||
| 14 | |||
| 15 | use base "Devel::Cover::Criterion"; | ||
| 16 | |||
| 17 | sub pad { | ||
| 18 | my $self = shift; | ||
| 19 | $self->[0] = [ 0, 0 ] unless $self->[0] && @{ $self->[0] }; | ||
| 20 | } | ||
| 21 | sub uncoverable { @_ > 1 ? $_[0][2][ $_[1] ] : scalar grep $_, @{ $_[0][2] } } | ||
| 22 | sub covered { @_ > 1 ? $_[0][0][ $_[1] ] : scalar grep $_, @{ $_[0][0] } } | ||
| 23 | sub total { scalar @{ $_[0][0] } } | ||
| 24 | sub value { $_[0][0][ $_[1] ] } | ||
| 25 | sub values { @{ $_[0][0] } } | ||
| 26 | sub text { $_[0][1]{text} } | ||
| 27 | sub criterion { "branch" } | ||
| 28 | |||
| 29 | sub percentage { | ||
| 30 | my $t = $_[0]->total; | ||
| 31 | sprintf "%3d", $t ? $_[0]->covered / $t * 100 : 0 | ||
| 32 | } | ||
| 33 | |||
| 34 | sub error { | ||
| 35 | my $self = shift; | ||
| 36 | if (@_) { | ||
| 37 | my $c = shift; | ||
| 38 | return $self->err_chk($self->covered($c), $self->uncoverable($c)); | ||
| 39 | } | ||
| 40 | my $e = 0; | ||
| 41 | for my $c (0 .. $#{ $self->[0] }) { | ||
| 42 | $e++ if $self->err_chk($self->covered($c), $self->uncoverable($c)); | ||
| 43 | } | ||
| 44 | $e | ||
| 45 | } | ||
| 46 | |||
| 47 | sub calculate_summary { | ||
| 48 | my $self = shift; | ||
| 49 | my ($db, $file) = @_; | ||
| 50 | |||
| 51 | my $s = $db->{summary}; | ||
| 52 | $self->pad; | ||
| 53 | |||
| 54 | $self->aggregate($s, $file, "total", $self->total); | ||
| 55 | $self->aggregate($s, $file, "uncoverable", $self->uncoverable); | ||
| 56 | $self->aggregate($s, $file, "covered", $self->covered); | ||
| 57 | $self->aggregate($s, $file, "error", $self->error); | ||
| 58 | } | ||
| 59 | |||
| 60 | 1 | ||
| 61 | |||
| 62 | __END__ |