Subject: | 0% branch coverage for methods defined in non-reported files |
A simple condition like
print 1 unless defined $object->attr;
reports 0% branch coverage if the "attr" sub is defined by an external
file (such as Class::Accessor, etc).
changing it to
my $attr = $object->attr;
print 1 unless defined $attr;
reports 100%:
line % coverage branch
16 0 T F unless defined $self->foo
20 100 T F unless defined $foo
Example files and output can be seen here (also attached):
https://gist.github.com/1405707
Overall branch coverage for the file is 50% (average of 0 and 100).
Switching the commented lines to define the sub directly instead of
using the accessor maker boosts coverage to 100%.
Subject: | accessor_maker.pm |
package accessor_maker;
sub import {
no strict 'refs';
*{ caller() . '::' . 'foo' } = sub { $_[0]->{ 'foo' } };
}
1;
Subject: | whole.pl |
package cm3;
sub new { bless $_[1], $_[0] }
# comment/uncomment one or the other:
use accessor_maker;
#sub foo { $_[0]->{ 'foo' } }
package main;
sub test {
my $self = shift;
print 'un' unless defined $self->foo;
print "defined\n";
my $foo = $self->foo;
print 'un' unless defined $foo;
print "defined\n";
}
test( cm3->new({}) );
test( cm3->new({foo => 1}) );
1;