File Coverage

blib/lib/Test/Mocha/CalledOk.pm
Criterion Covered Total %
statement 29 29 100.0
branch 6 6 100.0
condition 3 3 100.0
subroutine 4 4 100.0
pod 0 1 100.0
total 42 43 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.67';
5 13     13   5614 use strict;
  13         33  
  13         372  
6 13     13   83 use warnings;
  13         25  
  13         351  
7              
8 13     13   71 use Test::Builder;
  13         24  
  13         3855  
9              
10             my $TB = Test::Builder->new;
11              
12             sub test {
13             # uncoverable pod
14 118     118 0 270 my ( $class, $method_call, $exp, $test_name ) = @_;
15              
16 118         294 my $calls = $method_call->invocant->__calls;
17 118         187 my $got = grep { $method_call->__satisfied_by($_) } @{$calls};
  515         1973  
  118         251  
18 118         429 my $test_ok = $class->is( $got, $exp );
19              
20 118         292 my $exp_times = $class->stringify($exp);
21 118 100       377 $test_name = "$method_call was called $exp_times" if !defined $test_name;
22              
23             # Test failure report should not trace back to Mocha modules
24             {
25 118         10386 local $Test::Builder::Level = $Test::Builder::Level + 1;
  118         203  
26 118         367 $TB->ok( $test_ok, $test_name );
27             }
28              
29             # output diagnostics to aid with debugging
30 118 100 100     53569 unless ( $test_ok || $TB->in_todo ) {
31 14         1961 my $call_history;
32 14 100       24 if ( @{$calls} ) {
  14         45  
33 12         22 $call_history .= "\n " . $_->stringify_long foreach @{$calls};
  12         68  
34             }
35             else {
36 2         6 $call_history = "\n (No methods were called)";
37             }
38              
39 14         81 $TB->diag(<<"END");
40             Error: unexpected number of calls to '$method_call'
41             got: $got time(s)
42             expected: $exp_times
43             Complete method call history (most recent call last):$call_history
44             END
45             }
46 118         7247 return;
47             }
48              
49             1;