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.66';
5 13     13   5495 use strict;
  13         26  
  13         357  
6 13     13   69 use warnings;
  13         26  
  13         323  
7              
8 13     13   79 use Test::Builder;
  13         31  
  13         3770  
9              
10             my $TB = Test::Builder->new;
11              
12             sub test {
13             # uncoverable pod
14 118     118 0 286 my ( $class, $method_call, $exp, $test_name ) = @_;
15              
16 118         289 my $calls = $method_call->invocant->__calls;
17 118         178 my $got = grep { $method_call->__satisfied_by($_) } @{$calls};
  515         2060  
  118         244  
18 118         436 my $test_ok = $class->is( $got, $exp );
19              
20 118         292 my $exp_times = $class->stringify($exp);
21 118 100       359 $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         9854 local $Test::Builder::Level = $Test::Builder::Level + 1;
  118         205  
26 118         380 $TB->ok( $test_ok, $test_name );
27             }
28              
29             # output diagnostics to aid with debugging
30 118 100 100     56710 unless ( $test_ok || $TB->in_todo ) {
31 14         1996 my $call_history;
32 14 100       19 if ( @{$calls} ) {
  14         37  
33 12         18 $call_history .= "\n " . $_->stringify_long foreach @{$calls};
  12         50  
34             }
35             else {
36 2         4 $call_history = "\n (No methods were called)";
37             }
38              
39 14         61 $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         6994 return;
47             }
48              
49             1;