File Coverage

blib/lib/Test/Mocha/CalledOk.pm
Criterion Covered Total %
statement 32 32 100.0
branch 6 6 100.0
condition 3 3 100.0
subroutine 5 5 100.0
pod 0 1 100.0
total 46 47 100.0


line stmt bran cond sub pod time code
1             package Test::Mocha::CalledOk;
2             # ABSTRACT: Abstract base class for verifying method calls
3             # Abstract class methods required of sub-classes: 'is' and 'stringify'
4             $Test::Mocha::CalledOk::VERSION = '0.61';
5 21     21   6260 use strict;
  21         29  
  21         680  
6 21     21   80 use warnings;
  21         21  
  21         399  
7              
8 21     21   72 use Test::Builder;
  21         27  
  21         413  
9 21     21   6667 use Test::Mocha::Util qw( getattr );
  21         40  
  21         4364  
10              
11             my $TB = Test::Builder->new;
12              
13             sub test {
14             # uncoverable pod
15 210     210 0 287 my ( $class, $method_call, $exp, $test_name ) = @_;
16              
17 210         371 my $mock = $method_call->invocant;
18 210         384 my $calls = getattr( $mock, 'calls' );
19 210         213 my $got = grep { $method_call->satisfied_by($_) } @{$calls};
  889         2011  
  210         294  
20 207         618 my $test_ok = $class->is( $got, $exp );
21              
22 207         472 my $exp_times = $class->stringify($exp);
23 207 100       531 $test_name = "$method_call was called $exp_times" if !defined $test_name;
24              
25             # Test failure report should not trace back to Mocha modules
26 207         303 local $Test::Builder::Level = $Test::Builder::Level + 1;
27 207         496 $TB->ok( $test_ok, $test_name );
28              
29             # output diagnostics to aid with debugging
30 207 100 100     55980 unless ( $test_ok || $TB->in_todo ) {
31              
32 22         568 my $call_history;
33 22 100       23 if ( @{$calls} ) {
  22         47  
34 20         19 $call_history .= "\n " . $_->stringify_long foreach @{$calls};
  20         81  
35             }
36             else {
37 2         6 $call_history = "\n (No methods were called)";
38             }
39              
40 22         75 $TB->diag(<<"END");
41             Error: unexpected number of calls to '$method_call'
42             got: $got time(s)
43             expected: $exp_times
44             Complete method call history (most recent call last):$call_history
45             END
46             }
47 207         2838 return;
48             }
49              
50             1;