File: | blib/lib/Test/Mocha/CalledOk.pm |
Coverage: | 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 | 24 24 24 | 8437 21 472 | use strict; | ||||
6 | 24 24 24 | 46 16 312 | use warnings; | ||||
7 | |||||||
8 | 24 24 24 | 51 18 3330 | use Test::Builder; | ||||
9 | |||||||
10 | my $TB = Test::Builder->new; | ||||||
11 | |||||||
12 | sub test { | ||||||
13 | # uncoverable pod | ||||||
14 | 180 | 0 | 132 | my ( $class, $method_call, $exp, $test_name ) = @_; | |||
15 | |||||||
16 | 180 | 226 | my $calls = $method_call->invocant->__calls; | ||||
17 | 180 742 180 | 133 16020 147 | my $got = grep { $method_call->__satisfied_by($_) } @{$calls}; | ||||
18 | 180 | 1891 | my $test_ok = $class->is( $got, $exp ); | ||||
19 | |||||||
20 | 180 | 250 | my $exp_times = $class->stringify($exp); | ||||
21 | 180 | 277 | $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 | 180 | 156 | local $Test::Builder::Level = $Test::Builder::Level + 1; | ||||
25 | 180 | 232 | $TB->ok( $test_ok, $test_name ); | ||||
26 | |||||||
27 | # output diagnostics to aid with debugging | ||||||
28 | 180 | 23788 | unless ( $test_ok || $TB->in_todo ) { | ||||
29 | 16 | 224 | my $call_history; | ||||
30 | 16 16 | 12 18 | if ( @{$calls} ) { | ||||
31 | 14 14 | 10 34 | $call_history .= "\n " . $_->stringify_long foreach @{$calls}; | ||||
32 | } | ||||||
33 | else { | ||||||
34 | 2 | 2 | $call_history = "\n (No methods were called)"; | ||||
35 | } | ||||||
36 | |||||||
37 | 16 | 33 | $TB->diag(<<"END"); | ||||
38 | Error: unexpected number of calls to '$method_call' | ||||||
39 | got: $got time(s) | ||||||
40 | expected: $exp_times | ||||||
41 | Complete method call history (most recent call last):$call_history | ||||||
42 | END | ||||||
43 | } | ||||||
44 | 180 | 983 | return; | ||||
45 | } | ||||||
46 | |||||||
47 | 1; |