I am a bit new to the Devel::Cover module, but have found it very useful in making sure I am not missing tests.
A problem I am running into is understanding the report from Devel::Cover. I've looked at the documentation, but can't figure out what I need to test to get 100% coverage.
Edit - I should make it clear that I am not saying I need 100% coverage, because as multiple people point out, 100% coverage is a loose term, does not mean that my code is bug free, and may not always be completely necessary. Since I am new at Devel::Cover, I am interested to know why my code is not 100% coverage, in case I am missing some important tests.
Here is the output from the cover report:
line err stmt bran cond sub pod time code
...
36 sub connect_database {
37 3 3 1 1126 my $self = shift;
38 3 100 24 if ( !$self->{dsn} ) {
39 1 7 croak 'dsn not supplied - cannot connect';
40 }
41 *** 2 33 21 $self->{dbh} = DBI->connect( $self->{dsn}, q{}, q{} )
42 || croak "$DBI::errstr";
43 1 11 return $self;
44 }
...
line err % l !l&&r !l&&!r expr
----- --- ------ ------ ------ ------ ----
41 *** 33 1 0 0 'DBI'->connect($$self{'dsn'}, '', '') || croak("$DBI::errstr")
And here is and example of my code that tests this specific line:
my $database = MyModule::Database->new( { dsn => 'Invalid DSN' });
throws_ok( sub { $database->connect_database() },
qr/Can't connect to data source/,
'Test connection exception (invalid dsn)' );
This test passes - the connect does throw an error and fulfills my "throws_ok" test.
I do have some tests that test for a successful connection, which is why I think I have 33% coverage, but if I'm reading it correctly, cover thinks I am not testing the "|| croak" part of the statement. I thought I was, with the "throws_ok" test, but obviously I am missing something.
Does anyone have advice on how I can test my DBI->connect line successfully?
Thanks!
Edit:
brian tipped me off to the HTML report and the truth table that explains why line #41 is not passing. The only problem is that I can't figure out what it is telling me. I guess the real core of my question is why is this specific line not passing coverage.
Here is the truth table:
LINE # % # coverage # condition
41 # 33 # A | B | dec # 'DBI'->connect($$self{'dsn'}, '', '') || croak("$DBI::errstr")
# # 0 | 0 | 0 #
# # 0 | 1 | 1 #
# # 1 | X | 1 # (THIS LINE IS Green - the rest are red)
If anyone could help explain this truth table, I'd appreciate it. It has also been mentioned that to pass the coverage I need to have a mock database object, but I don't quite see how anything in the coverage results that would clue me in to this.
Thanks again!