File Coverage

blib/lib/Test/Mock/Wrapper/Verify.pm
Criterion Covered Total %
statement 54 56 96.4
branch 1 2 50.0
condition 3 5 60.0
subroutine 12 13 92.3
pod 7 8 87.5
total 77 84 91.6


line stmt bran cond sub pod time code
1             package Test::Mock::Wrapper::Verify;
2             $Test::Mock::Wrapper::Verify::VERSION = '0.12';
3 3     3   9 use strict;
  3         3  
  3         74  
4 3     3   9 use warnings;
  3         3  
  3         44  
5 3     3   8 use Test::Deep;
  3         2  
  3         16  
6 3     3   502 use Test::More;
  3         4  
  3         15  
7 3     3   546 use Clone qw(clone);
  3         4  
  3         1204  
8              
9             =head1 NAME
10              
11             Test::Mock::Wrapped::Verify - Part of the Test::Mock::Wrapper module
12              
13             =head1 VERSION
14              
15             version 0.12
16              
17             =head1 SYNOPIS
18              
19             my $verifier = $wrapper->verify('bar');
20            
21             $verifier->at_least(2)->at_most(5);
22            
23             $verifier->with(['zomg'])->never;
24              
25             =head1 DESCRIPTION
26              
27             Instances of this class are returned by Test::Mock::Wrapper::verify to allow for
28             flexible, readible call verification with objects mocked by Test::Mock:Wrapper
29              
30             =head1 METHODS
31              
32             =cut
33              
34             sub new {
35 19     19 0 24 my($proto, $method, $calls) = @_;
36 19   100     40 $calls ||= [];
37 19   33     57 my $class = ref($proto) || $proto;
38 19         97 return bless({__calls=>$calls, method=>$method}, $class);
39             }
40              
41             =head2 getCalls
42              
43             Returns an array of arrays representing all the calls to the mocked method which
44             match any criteria added via a "with" call.
45              
46             =cut
47              
48             sub getCalls {
49 0     0 1 0 my $self = shift;
50 0         0 return clone($self->{__calls});
51             }
52              
53             =head2 with(['some', 'args', ignore()])
54              
55             This returns a new verifier object with a call list which has been filtered using the
56             supplied matcher. See L<Test::Deep> for information about matcher syntax.
57              
58             =cut
59              
60             sub with {
61 5     5 1 55 my $self = shift;
62 5         5 my $matcher = shift;
63 5         6 my (@__calls) = grep({eq_deeply($_, $matcher)} @{ $self->{__calls} });
  22         19341  
  5         8  
64 5         5494 return bless({__calls=>\@__calls, method=>$self->{method}}, ref($self));
65             }
66              
67             =head2 exactly(N)
68              
69             Assert this method was called exactly N times. This is equivelent to
70            
71             =cut
72              
73             sub exactly {
74 4     4 1 7 my $self = shift;
75 4         3 my $times = shift;
76 4         4 ok(scalar(@{ $self->{__calls} }) == $times, "$self->{method} called ".scalar(@{ $self->{__calls} })." times, wanted exactly $times times");
  4         14  
  4         29  
77 4         1023 return $self;
78             }
79              
80             =head2 never
81              
82             Assert this method was never called. This is syntatic sugar, equivilent to
83            
84             $verify->exactly(0)
85              
86             =cut
87              
88             sub never {
89 4     4 1 4 my $self = shift;
90 4         20 ok(scalar(@{ $self->{__calls} }) == 0,
  4         8  
91 4 50       5 "$self->{method} should never be called but was called ".scalar(@{ $self->{__calls} })." time".(scalar(@{ $self->{__calls} }) > 1 ? "s":'').".");
  4         16  
92 4         819 return $self;
93             }
94              
95             =head2 once
96              
97             Assert this method was called one time. This is syntatic sugar, equivilent to
98            
99             $verify->exactly(1)
100              
101             =cut
102              
103             sub once {
104 4     4 1 8 my $self = shift;
105 4         4 ok(scalar(@{ $self->{__calls} }) == 1, "$self->{method} should have been called once, but was called ".scalar(@{ $self->{__calls} })." times.");
  4         11  
  4         21  
106 4         1083 return $self;
107             }
108              
109             =head2 at_least(N)
110              
111             Assert this method was called at least N times.
112              
113             =cut
114              
115             sub at_least {
116 9     9 1 11 my $self = shift;
117 9         10 my $times = shift;
118 9         10 ok(scalar(@{ $self->{__calls} }) >= $times, "$self->{method} only called ".scalar(@{ $self->{__calls} })." times, wanted at least $times\n");
  9         21  
  9         43  
119 9         2100 return $self;
120             }
121              
122             =head2 at_most(N)
123              
124             Assert this method was called at most N times.
125              
126             =cut
127              
128             sub at_most {
129 2     2 1 8 my $self = shift;
130 2         2 my $times = shift;
131 2         3 ok(scalar(@{ $self->{__calls} }) <= $times, "$self->{method} called ".scalar(@{ $self->{__calls} })." times, wanted at most $times\n");
  2         5  
  2         8  
132 2         375 return $self;
133             }
134              
135              
136             return 42;
137              
138             =head1 AUTHOR
139              
140             Dave Mueller <dave@perljedi.com>
141              
142             =head1 COPYRIGHT AND LICENSE
143              
144             This software is copyright (c) 2015 by Dave Mueller.
145              
146             This is free software; you can redistribute it and/or modify it under the
147             same terms as the Perl 5 programming language system itself.