| 1 | #!/usr/bin/perl -c | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | package Test::Mock::Class::Role::Object; | ||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||
| 7 | Test::Mock::Class::Role::Object - Role for base object of mock class | ||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||
| 9 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | This role provides an API for defining and changing behavior of mock class. | ||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||
| 13 | =cut | ||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use 5.006; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||
| 17 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 18 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||
| 20 | 1 | our $VERSION = '0.0303'; | |||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||
| 22 | 1 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||
| 25 | 1 | ▶ | use Symbol (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||
| 27 | 1 | ▶ | use Test::Assert ':all'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||
| 30 | ## no critic qw(ProhibitConstantPragma) | ||||||||||||||||||||||||||||||||
| 31 | 1 | ▶ | use constant Exception => 'Test::Mock::Class::Exception'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 32 | 1 | ▶ | use English '-no_match_vars'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||
| 34 | 1 | ▶ | use Exception::Base ( | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 35 | 1 | Exception, | |||||||||||||||||||||||||||||||
| 36 | 'Exception::Fatal', | ||||||||||||||||||||||||||||||||
| 37 | '+ignore_package' => [__PACKAGE__], | ||||||||||||||||||||||||||||||||
| 38 | 1 | ); | |||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||
| 41 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||
| 43 | =over | ||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||
| 45 | =item B<_mock_call> : HashRef | ||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||
| 47 | Count of method calls stored as HashRef. | ||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||
| 49 | =cut | ||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||
| 51 | has '_mock_call' => ( | ||||||||||||||||||||||||||||||||
| 52 | is => 'ro', | ||||||||||||||||||||||||||||||||
| 53 | isa => 'HashRef', | ||||||||||||||||||||||||||||||||
| 54 | 52 | ▶ | default => sub { {} }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 55 | 1 | ); | |||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||
| 58 | =item B<_mock_expectation> : HashRef | ||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||
| 60 | Expectations for mock methods stored as HashRef. | ||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||
| 62 | =cut | ||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||
| 64 | has '_mock_expectation' => ( | ||||||||||||||||||||||||||||||||
| 65 | is => 'ro', | ||||||||||||||||||||||||||||||||
| 66 | isa => 'HashRef', | ||||||||||||||||||||||||||||||||
| 67 | 52 | ▶ | default => sub { {} }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 68 | 1 | ); | |||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||
| 71 | =item B<_mock_action> : HashRef | ||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||
| 73 | Return values or actions for mock methods stored as HashRef. | ||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||
| 75 | =back | ||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||
| 77 | =cut | ||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||
| 79 | has '_mock_action' => ( | ||||||||||||||||||||||||||||||||
| 80 | is => 'ro', | ||||||||||||||||||||||||||||||||
| 81 | isa => 'HashRef', | ||||||||||||||||||||||||||||||||
| 82 | 52 | ▶ | default => sub { {} }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 83 | 1 | ); | |||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||
| 86 | 1 | ▶ | use namespace::clean -except => 'meta'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||
| 89 | ## no critic qw(RequireCheckingReturnValueOfEval) | ||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||
| 91 | =head1 METHODS | ||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||
| 93 | =over | ||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||
| 95 | =item B<mock_tally>(I<>) : Self | ||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||
| 97 | Check the expectations at the end. It should be called expicitly if | ||||||||||||||||||||||||||||||||
| 98 | C<minimum> or C<count> parameter was used for expectation, or following | ||||||||||||||||||||||||||||||||
| 99 | methods was called: C<mock_expect_at_least_once>, | ||||||||||||||||||||||||||||||||
| 100 | C<mock_add_expectation_call_count>, C<mock_expect_minimum_call_count> | ||||||||||||||||||||||||||||||||
| 101 | or C<mock_expect_once>. | ||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||
| 103 | =cut | ||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||
| 105 | 40 | ▶ | sub mock_tally { | ||||||||||||||||||||||||||||||
|
Subroutine
mock_tally: called CC=15 SCAR=28.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 106 | 40 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||
| 108 | 40 | my $expectation = $self->_mock_expectation; | |||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||
| 110 | 40 | ▶ | return if not defined $expectation | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition40%2/5
not defined $expectation or (ref $expectation || "") ne "HASH"
ref $expectation || ''
MC/DC0%0/2
not defined $expectation or (ref $expectation || "") ne "HASH"
not defined $expectation
ref $expectation
Truth table33%1/3
not(A) or B
A= not defined $expectationB= ref $expectation
| |||||||||||||||||||||||||||||||||
| 111 | or (ref $expectation || '') ne 'HASH'; | ||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||
| 113 | 40 | foreach my $method (keys %{ $expectation }) { | |||||||||||||||||||||||||||||||
| 114 | next if not defined $expectation->{$method} | ||||||||||||||||||||||||||||||||
| 115 | 26 | ▶ | or (ref $expectation->{$method} || '') ne 'ARRAY'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition40%2/5
not defined $expectation->{$method} or (ref $expectation->{$method} || "") ne "ARRAY"
ref $expectation->{$method} || ''
MC/DC0%0/2
not defined $expectation->{$method} or (ref $expectation->{$method} || "") ne "ARRAY"
not defined $expectation->{$method}
ref $expectation->{$method}
Truth table33%1/3
not(A) or B
A= not defined $expectation->{$method}B= ref $expectation->{$method}
| |||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||
| 117 | 26 | foreach my $rule (@{ $expectation->{$method} }) { | |||||||||||||||||||||||||||||||
| 118 | 29 | ▶ | if (defined $rule->{count}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 119 | 7 | ▶ | my $count = $rule->{call} || 0; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$rule->{'call'} || 0
MC/DC100%1/1
$rule->{'call'} || 0
$rule->{'call'}
Truth table100%2/2
A
A= $rule->{'call'}
| |||||||||||||||||||||||||||||||||
| 120 | fail([ | ||||||||||||||||||||||||||||||||
| 121 | 'Expected call count (%d) for method (%s) with calls (%d)', | ||||||||||||||||||||||||||||||||
| 122 | $rule->{count}, $method, $count | ||||||||||||||||||||||||||||||||
| 123 | 7 | ▶ | ]) if ($count != $rule->{count}); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 124 | }; | ||||||||||||||||||||||||||||||||
| 125 | 28 | ▶ | if (defined $rule->{minimum}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 126 | 6 | ▶ | my $count = $rule->{call} || 0; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$rule->{'call'} || 0
MC/DC100%1/1
$rule->{'call'} || 0
$rule->{'call'}
Truth table100%2/2
A
A= $rule->{'call'}
| |||||||||||||||||||||||||||||||||
| 127 | fail([ | ||||||||||||||||||||||||||||||||
| 128 | 'Minimum call count (%d) for method (%s) with calls (%d)', | ||||||||||||||||||||||||||||||||
| 129 | $rule->{minimum}, $method, $count | ||||||||||||||||||||||||||||||||
| 130 | 6 | ▶ | ]) if ($count < $rule->{minimum}); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 131 | }; | ||||||||||||||||||||||||||||||||
| 132 | }; | ||||||||||||||||||||||||||||||||
| 133 | }; | ||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||
| 135 | 38 | return $self; | |||||||||||||||||||||||||||||||
| 136 | }; | ||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||
| 139 | =item B<mock_invoke>( I<method> : Str, I<args> : Array ) : Any | ||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||
| 141 | Increases the call counter and returns the expected value for the method name | ||||||||||||||||||||||||||||||||
| 142 | and checks expectations. Will generate any test assertions as a result of | ||||||||||||||||||||||||||||||||
| 143 | expectations if there is a test present. | ||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||
| 145 | If more that one expectation matches, all of them are checked. If one of them | ||||||||||||||||||||||||||||||||
| 146 | fails, the whole C<mock_invoke> method is failed. | ||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||
| 148 | This method is called in overridden methods of mock class, but you need to | ||||||||||||||||||||||||||||||||
| 149 | call it explicitly if you constructed own method. | ||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||
| 151 | =cut | ||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||
| 153 | 145 | ▶ | sub mock_invoke { | ||||||||||||||||||||||||||||||
|
Subroutine
mock_invoke: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 154 | 145 | my ($self, $method, @args) = @_; | |||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||
| 156 | 145 | assert_not_null($method) if ASSERT; | |||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||
| 158 | 145 | my $timing = $self->_mock_add_call($method, @args); | |||||||||||||||||||||||||||||||
| 159 | 145 | $self->_mock_check_expectations($method, $timing, @args); | |||||||||||||||||||||||||||||||
| 160 | 139 | return $self->_mock_emulate_call($method, $timing, @args); | |||||||||||||||||||||||||||||||
| 161 | }; | ||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||
| 164 | =item B<mock_return>( I<method> : Str, I<value> : Any, :I<at> : Int, :I<args> : ArrayRef[Any] ) : Self | ||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||
| 166 | Sets a return for a parameter list that will be passed on by call to this | ||||||||||||||||||||||||||||||||
| 167 | method that match. | ||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||
| 169 | The first value is returned if more than one parameter list matches method's | ||||||||||||||||||||||||||||||||
| 170 | arguments. The C<undef> value is returned if none of parameters matches. | ||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||
| 172 | =over | ||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||
| 174 | =item method | ||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||
| 176 | Method name. | ||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||
| 178 | =item value | ||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||
| 180 | Returned value. | ||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||
| 182 | $m->mock_return( 'open', 1 ); | ||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||
| 184 | If value is coderef, then it is called with method name, current timing | ||||||||||||||||||||||||||||||||
| 185 | and original arguments as arguments. It allows to return array rather than | ||||||||||||||||||||||||||||||||
| 186 | scalar. | ||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||
| 188 | $m->mock_return( 'sequence', sub { | ||||||||||||||||||||||||||||||||
| 189 | qw( one two three )[ $_[1] ] | ||||||||||||||||||||||||||||||||
| 190 | } ); | ||||||||||||||||||||||||||||||||
| 191 | $m->mock_return( 'get_array', sub { (1,2,3) } ); | ||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||
| 193 | =item at | ||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||
| 195 | Value is returned only for current timing, started from C<0>. | ||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||
| 197 | $m->mock_return( 'sequence', 'one', at => 0 ); | ||||||||||||||||||||||||||||||||
| 198 | $m->mock_return( 'sequence', 'two', at => 1 ); | ||||||||||||||||||||||||||||||||
| 199 | $m->mock_return( 'sequence', 'three', at => 2 ); | ||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||
| 201 | =item args | ||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||
| 203 | Value is returned only if method is called with proper argument. | ||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||
| 205 | $m->mock_return( 'get_value', 'admin', args => ['dbuser'] ); | ||||||||||||||||||||||||||||||||
| 206 | $m->mock_return( 'get_value', 'secret', args => ['dbpass'] ); | ||||||||||||||||||||||||||||||||
| 207 | $m->mock_return( 'get_value', sub { $_[2] }, args => [qr/.*/] ); | ||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||
| 209 | =back | ||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||
| 211 | =cut | ||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||
| 213 | 34 | ▶ | sub mock_return { | ||||||||||||||||||||||||||||||
|
Subroutine
mock_return: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 214 | 34 | my ($self, $method, $value, %params) = @_; | |||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||
| 216 | 34 | ▶ | $self->throw_error( | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 217 | 'Usage: $mock->mock_return( METHOD => VALUE, PARAMS )' | ||||||||||||||||||||||||||||||||
| 218 | ) unless defined $method; | ||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||
| 220 | 34 | assert_equals('HASH', ref $self->_mock_action) if ASSERT; | |||||||||||||||||||||||||||||||
| 221 | 34 | push @{ $self->_mock_action->{$method} } => { %params, value => $value }; | |||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||
| 223 | 34 | return $self; | |||||||||||||||||||||||||||||||
| 224 | }; | ||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||
| 227 | =item B<mock_return_at>( I<at> : Int, I<method> : Str, I<value> : Any, :I<args> : ArrayRef[Any] ) : Self | ||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||
| 229 | Convenience method for returning a value upon the method call. | ||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||
| 231 | =cut | ||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||
| 233 | 16 | ▶ | sub mock_return_at { | ||||||||||||||||||||||||||||||
|
Subroutine
mock_return_at: called CC=3 SCAR=12.5
Pod
documented
| |||||||||||||||||||||||||||||||||
| 234 | 16 | my ($self, $at, $method, $value, %params) = @_; | |||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||
| 236 | 16 | ▶ | $self->throw_error( | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $at and defined $method
MC/DC0%0/2
defined $at and defined $method
defined $at
defined $method
Truth table33%1/3
A and B
A= defined $atB= defined $method
| |||||||||||||||||||||||||||||||||
| 237 | message => 'Usage: $mock->mock_return_at( AT, METHOD => VALUE, PARAMS )' | ||||||||||||||||||||||||||||||||
| 238 | ) unless defined $at and defined $method; | ||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||
| 240 | 16 | return $self->mock_return( $method => $value, %params, at => $at ); | |||||||||||||||||||||||||||||||
| 241 | }; | ||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||
| 244 | =item B<mock_throw>( I<method> : Str, :I<at> : Int, I<exception> : Str|Object, :I<args> : ArrayRef[Any], I<params> : Hash ) : Self | ||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||
| 246 | Sets up a trigger to throw an exception upon the method call. The method | ||||||||||||||||||||||||||||||||
| 247 | takes the same arguments as C<mock_return>. | ||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||
| 249 | If an I<exception> parameter is a string, the L<Exception::Assertion> is | ||||||||||||||||||||||||||||||||
| 250 | thrown with this parameter as its message and rest of parameters as its | ||||||||||||||||||||||||||||||||
| 251 | arguments. If an I<exception> parameter is an object reference, the C<throw> | ||||||||||||||||||||||||||||||||
| 252 | method is called on this object with predefined message and rest of parameters | ||||||||||||||||||||||||||||||||
| 253 | as its arguments. | ||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||
| 255 | =cut | ||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||
| 257 | 4 | ▶ | sub mock_throw { | ||||||||||||||||||||||||||||||
|
Subroutine
mock_throw: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 258 | 4 | my ($self, $method, $exception, %params) = @_; | |||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||
| 260 | 4 | ▶ | Exception::Argument->throw( | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 261 | message => 'Usage: $mock->mock_throw( METHOD => EXCEPTION, PARAMS )' | ||||||||||||||||||||||||||||||||
| 262 | ) unless defined $method; | ||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||
| 264 | 4 | ▶ | $exception = Exception::Assertion->new( | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 265 | message => $exception, | ||||||||||||||||||||||||||||||||
| 266 | reason => ['Thrown on method (%s)', $method], | ||||||||||||||||||||||||||||||||
| 267 | %params | ||||||||||||||||||||||||||||||||
| 268 | ) unless blessed $exception; | ||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||
| 270 | 4 | assert_equals('HASH', ref $self->_mock_action) if ASSERT; | |||||||||||||||||||||||||||||||
| 271 | 4 | push @{ $self->_mock_action->{$method} } => { | |||||||||||||||||||||||||||||||
| 272 | %params, | ||||||||||||||||||||||||||||||||
| 273 | 5 | ▶ | value => sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 274 | 5 | $exception->throw; | |||||||||||||||||||||||||||||||
| 275 | }, | ||||||||||||||||||||||||||||||||
| 276 | 4 | }; | |||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||
| 278 | 4 | return $self; | |||||||||||||||||||||||||||||||
| 279 | }; | ||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||
| 282 | =item B<mock_throw_at>( I<at> : Int, I<method> : Str, I<exception> : Str|Object, :I<args> : ArrayRef[Any] ) : Self | ||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||
| 284 | Convenience method for throwing an error upon the method call. | ||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||
| 286 | =cut | ||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||
| 288 | 1 | ▶ | sub mock_throw_at { | ||||||||||||||||||||||||||||||
|
Subroutine
mock_throw_at: called CC=3 SCAR=12.5
Pod
documented
| |||||||||||||||||||||||||||||||||
| 289 | 1 | my ($self, $at, $method, $exception, %params) = @_; | |||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||
| 291 | 1 | ▶ | Exception::Argument->throw( | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $at and defined $method
MC/DC0%0/2
defined $at and defined $method
defined $at
defined $method
Truth table33%1/3
A and B
A= defined $atB= defined $method
| |||||||||||||||||||||||||||||||||
| 292 | message => 'Usage: $mock->mock_throw_at( AT, METHOD => EXCEPTION, PARAMS )' | ||||||||||||||||||||||||||||||||
| 293 | ) unless defined $at and defined $method; | ||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||
| 295 | 1 | return $self->mock_throw( $method => $exception, %params, at => $at ); | |||||||||||||||||||||||||||||||
| 296 | }; | ||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||
| 299 | =item B<mock_expect>( I<method> : Str, :I<at> : Int, :I<minimum> : Int, :I<maximum> : Int, :I<count> : Int, :I<args> : ArrayRef[Any] ) : Self | ||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||
| 301 | Sets up an expected call with a set of expected parameters in that call. Each | ||||||||||||||||||||||||||||||||
| 302 | call will be compared to these expectations regardless of when the call is | ||||||||||||||||||||||||||||||||
| 303 | made. The method takes the same arguments as C<mock_return>. | ||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||
| 305 | =cut | ||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||
| 307 | 36 | ▶ | sub mock_expect { | ||||||||||||||||||||||||||||||
|
Subroutine
mock_expect: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 308 | 36 | my ($self, $method, %params) = @_; | |||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||
| 310 | 36 | ▶ | Exception::Argument->throw( | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 311 | message => 'Usage: $mock->mock_expect( METHOD => PARAMS )' | ||||||||||||||||||||||||||||||||
| 312 | ) unless defined $method; | ||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||
| 314 | 36 | ▶ | Exception->throw( | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 315 | message => ['Cannot set expected arguments as no method (%s) in class (%s)', $method, $self->meta->name], | ||||||||||||||||||||||||||||||||
| 316 | ) unless $self->meta->has_method($method); | ||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||
| 318 | 35 | assert_equals('HASH', ref $self->_mock_expectation) if ASSERT; | |||||||||||||||||||||||||||||||
| 319 | 35 | push @{ $self->_mock_expectation->{$method} } => { | |||||||||||||||||||||||||||||||
| 320 | %params, | ||||||||||||||||||||||||||||||||
| 321 | }; | ||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||
| 323 | 35 | return $self; | |||||||||||||||||||||||||||||||
| 324 | }; | ||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||
| 327 | =item B<mock_expect_at>( I<at> : Int, I<method> : Str, :I<args> : ArrayRef[Any] ) : Self | ||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||
| 329 | Sets up an expected call with a set of expected parameters in that call. | ||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||
| 331 | =cut | ||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||
| 333 | 4 | ▶ | sub mock_expect_at { | ||||||||||||||||||||||||||||||
|
Subroutine
mock_expect_at: called CC=3 SCAR=12.5
Pod
documented
| |||||||||||||||||||||||||||||||||
| 334 | 4 | my ($self, $at, $method, %params) = @_; | |||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||
| 336 | 4 | ▶ | Exception::Argument->throw( | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $at and defined $method
MC/DC0%0/2
defined $at and defined $method
defined $at
defined $method
Truth table33%1/3
A and B
A= defined $atB= defined $method
| |||||||||||||||||||||||||||||||||
| 337 | message => 'Usage: $mock->mock_expect_at( AT, METHOD => PARAMS )' | ||||||||||||||||||||||||||||||||
| 338 | ) unless defined $at and defined $method; | ||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||
| 340 | 4 | return $self->mock_expect( $method => %params, at => $at ); | |||||||||||||||||||||||||||||||
| 341 | }; | ||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||
| 344 | =item B<mock_expect_call_count>( I<method> : Str, I<count> : Int, :I<args> : ArrayRef[Any] ) : Self | ||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||
| 346 | Sets an expectation for the number of times a method will be called. The | ||||||||||||||||||||||||||||||||
| 347 | C<mock_tally> method have to be used to check this. | ||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||
| 349 | =cut | ||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||
| 351 | 1 | ▶ | sub mock_expect_call_count { | ||||||||||||||||||||||||||||||
|
Subroutine
mock_expect_call_count: called CC=3 SCAR=12.5
Pod
documented
| |||||||||||||||||||||||||||||||||
| 352 | 1 | my ($self, $method, $count, %params) = @_; | |||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||
| 354 | 1 | ▶ | Exception::Argument->throw( | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $method and defined $count
MC/DC0%0/2
defined $method and defined $count
defined $method
defined $count
Truth table33%1/3
A and B
A= defined $methodB= defined $count
| |||||||||||||||||||||||||||||||||
| 355 | message => 'Usage: $mock->mock_expect_call_count( METHOD, COUNT => PARAMS )' | ||||||||||||||||||||||||||||||||
| 356 | ) unless defined $method and defined $count; | ||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||
| 358 | 1 | return $self->mock_expect( $method => %params, count => $count ); | |||||||||||||||||||||||||||||||
| 359 | }; | ||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||
| 362 | =item B<mock_expect_maximum_call_count>( I<method> : Str, I<count> : Int, :I<args> : ArrayRef[Any] ) : Self | ||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||
| 364 | Sets the number of times a method may be called before a test failure is | ||||||||||||||||||||||||||||||||
| 365 | triggered. | ||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||
| 367 | =cut | ||||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||||
| 369 | 5 | ▶ | sub mock_expect_maximum_call_count { | ||||||||||||||||||||||||||||||
|
Subroutine
mock_expect_maximum_call_count: called CC=3 SCAR=12.5
Pod
documented
| |||||||||||||||||||||||||||||||||
| 370 | 5 | my ($self, $method, $count, %params) = @_; | |||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||
| 372 | 5 | ▶ | Exception::Argument->throw( | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $method and defined $count
MC/DC0%0/2
defined $method and defined $count
defined $method
defined $count
Truth table33%1/3
A and B
A= defined $methodB= defined $count
| |||||||||||||||||||||||||||||||||
| 373 | message => 'Usage: $mock->mock_expect_maximum_call_count( METHOD, COUNT => PARAMS )' | ||||||||||||||||||||||||||||||||
| 374 | ) unless defined $method and defined $count; | ||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||
| 376 | 5 | return $self->mock_expect( $method => %params, maximum => $count ); | |||||||||||||||||||||||||||||||
| 377 | }; | ||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||
| 380 | =item B<mock_expect_minimum_call_count>( I<method> : Str, I<count> : Int, :I<args> : ArrayRef[Any] ) : Self | ||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||
| 382 | Sets the number of times to call a method to prevent a failure on the tally. | ||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||
| 384 | =cut | ||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||
| 386 | 3 | ▶ | sub mock_expect_minimum_call_count { | ||||||||||||||||||||||||||||||
|
Subroutine
mock_expect_minimum_call_count: called CC=3 SCAR=12.5
Pod
documented
| |||||||||||||||||||||||||||||||||
| 387 | 3 | my ($self, $method, $count, %params) = @_; | |||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||
| 389 | 3 | ▶ | Exception::Argument->throw( | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $method and defined $count
MC/DC0%0/2
defined $method and defined $count
defined $method
defined $count
Truth table33%1/3
A and B
A= defined $methodB= defined $count
| |||||||||||||||||||||||||||||||||
| 390 | message => 'Usage: $mock->mock_expect_minimum_call_count( METHOD, COUNT => PARAMS )' | ||||||||||||||||||||||||||||||||
| 391 | ) unless defined $method and defined $count; | ||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||
| 393 | 3 | return $self->mock_expect( $method => %params, minimum => $count ); | |||||||||||||||||||||||||||||||
| 394 | }; | ||||||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||
| 397 | =item B<mock_expect_never>( I<method> : Str, :I<args> : ArrayRef[Any] ) : Self | ||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||
| 399 | Convenience method for barring a method call. | ||||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||
| 401 | =cut | ||||||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||||||
| 403 | 5 | ▶ | sub mock_expect_never { | ||||||||||||||||||||||||||||||
|
Subroutine
mock_expect_never: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 404 | 5 | my ($self, $method, %params) = @_; | |||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||
| 406 | 5 | ▶ | Exception::Argument->throw( | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 407 | message => 'Usage: $mock->mock_expect_never( METHOD => PARAMS )' | ||||||||||||||||||||||||||||||||
| 408 | ) unless defined $method; | ||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||
| 410 | 5 | return $self->mock_expect( $method => %params, maximum => 0 ); | |||||||||||||||||||||||||||||||
| 411 | }; | ||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||||||
| 414 | =item B<mock_expect_once>( I<method> : Str, :I<args> : ArrayRef[Any] ) : Self | ||||||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||||||
| 416 | Convenience method for a single method call. | ||||||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||||||
| 418 | =cut | ||||||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||||||
| 420 | 7 | ▶ | sub mock_expect_once { | ||||||||||||||||||||||||||||||
|
Subroutine
mock_expect_once: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 421 | 7 | my ($self, $method, %params) = @_; | |||||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||||||
| 423 | 7 | ▶ | Exception::Argument->throw( | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 424 | message => 'Usage: $mock->mock_expect_once( METHOD => PARAMS )' | ||||||||||||||||||||||||||||||||
| 425 | ) unless defined $method; | ||||||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||||||
| 427 | 7 | return $self->mock_expect( $method => %params, count => 1 ); | |||||||||||||||||||||||||||||||
| 428 | }; | ||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||||||
| 431 | =item B<mock_expect_at_least_once>( I<method> : Str, :I<args> : ArrayRef[Any] ) : Self | ||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||
| 433 | Convenience method for requiring a method call. | ||||||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||||||
| 435 | =cut | ||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||
| 437 | 3 | ▶ | sub mock_expect_at_least_once { | ||||||||||||||||||||||||||||||
|
Subroutine
mock_expect_at_least_once: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 438 | 3 | my ($self, $method, %params) = @_; | |||||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||||||
| 440 | 3 | ▶ | Exception::Argument->throw( | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 441 | message => 'Usage: $mock->mock_expect_at_least_once( METHOD => PARAMS )' | ||||||||||||||||||||||||||||||||
| 442 | ) unless defined $method; | ||||||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||||||
| 444 | 3 | return $self->mock_expect( $method => %params, minimum => 1 ); | |||||||||||||||||||||||||||||||
| 445 | }; | ||||||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||||||
| 448 | =item B<_mock_emulate_call>( I<method> : Str, I<timing> : Int, I<args> : Array ) : Any | ||||||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||||||
| 450 | Finds the return value matching the incoming arguments. If there is no | ||||||||||||||||||||||||||||||||
| 451 | matching value found then an error is triggered. | ||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||
| 453 | =cut | ||||||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||||||
| 455 | 139 | ▶ | sub _mock_emulate_call { | ||||||||||||||||||||||||||||||
|
Subroutine
_mock_emulate_call: called CC=20 SCAR=30.8
| |||||||||||||||||||||||||||||||||
| 456 | 139 | my ($self, $method, $timing, @args) = @_; | |||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||
| 458 | 139 | assert_not_null($method) if ASSERT; | |||||||||||||||||||||||||||||||
| 459 | 139 | assert_not_null($timing) if ASSERT; | |||||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||||||
| 461 | 139 | my $rules_for_method = $self->_mock_action->{$method}; | |||||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||||||
| 463 | 139 | ▶ | return if not defined $rules_for_method | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition60%3/5
not defined $rules_for_method or (ref $rules_for_method || "") ne "ARRAY"
ref $rules_for_method || ''
MC/DC0%0/2
not defined $rules_for_method or (ref $rules_for_method || "") ne "ARRAY"
not defined $rules_for_method
ref $rules_for_method
Truth table66%2/3
not(A) or B
A= not defined $rules_for_methodB= ref $rules_for_method
| |||||||||||||||||||||||||||||||||
| 464 | or (ref $rules_for_method || '') ne 'ARRAY'; | ||||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||
| 466 | RULE: | ||||||||||||||||||||||||||||||||
| 467 | 48 | foreach my $rule (@$rules_for_method) { | |||||||||||||||||||||||||||||||
| 468 | 75 | ▶ | if (defined $rule->{at}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 469 | 42 | ▶ | next unless $timing == $rule->{at}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 470 | }; | ||||||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||||||
| 472 | 48 | ▶ | if (exists $rule->{args}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 473 | my @rule_args = (ref $rule->{args} || '') eq 'ARRAY' | ||||||||||||||||||||||||||||||||
| 474 | 27 | ? @{ $rule->{args} } | |||||||||||||||||||||||||||||||
| 475 | 27 | ▶ | : ( $rule->{args} ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
ref $rule->{'args'} || ''
MC/DC0%0/1
ref $rule->{'args'} || ''
ref $rule->{'args'}
Truth table50%1/2
A
A= ref $rule->{'args'}
| |||||||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||||||
| 477 | # number of args matches? | ||||||||||||||||||||||||||||||||
| 478 | 27 | ▶ | next unless @args == @rule_args; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||||||
| 480 | # iterate args | ||||||||||||||||||||||||||||||||
| 481 | 24 | foreach my $i (0 .. @rule_args - 1) { | |||||||||||||||||||||||||||||||
| 482 | 35 | my $rule_arg = $rule_args[$i]; | |||||||||||||||||||||||||||||||
| 483 | 35 | ▶ | if ((ref $rule_arg || '') eq 'Regexp') { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%2/2
ref $rule_arg || ''
MC/DC100%1/1
ref $rule_arg || ''
ref $rule_arg
Truth table100%2/2
A
A= ref $rule_arg
| |||||||||||||||||||||||||||||||||
| 484 | 9 | ▶ | next RULE unless $args[$i] =~ $rule_arg; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 485 | } | ||||||||||||||||||||||||||||||||
| 486 | elsif (ref $rule_arg) { | ||||||||||||||||||||||||||||||||
| 487 | # TODO: use Test::Deep::NoTest | ||||||||||||||||||||||||||||||||
| 488 | 0 | eval { | |||||||||||||||||||||||||||||||
| 489 | 0 | assert_deep_equals($rule_arg, $args[$i]); | |||||||||||||||||||||||||||||||
| 490 | }; | ||||||||||||||||||||||||||||||||
| 491 | 0 | ▶ | next RULE if $EVAL_ERROR; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 492 | } | ||||||||||||||||||||||||||||||||
| 493 | else { | ||||||||||||||||||||||||||||||||
| 494 | # TODO: do not use eval | ||||||||||||||||||||||||||||||||
| 495 | 26 | eval { | |||||||||||||||||||||||||||||||
| 496 | 26 | assert_equals($rule_arg, $args[$i]); | |||||||||||||||||||||||||||||||
| 497 | }; | ||||||||||||||||||||||||||||||||
| 498 | 26 | ▶ | next RULE if $EVAL_ERROR; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 499 | }; | ||||||||||||||||||||||||||||||||
| 500 | }; | ||||||||||||||||||||||||||||||||
| 501 | }; | ||||||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||||||
| 503 | 37 | ▶ | if (ref $rule->{value} eq 'CODE') { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 504 | 6 | return $rule->{value}->( | |||||||||||||||||||||||||||||||
| 505 | $method, $timing, @args | ||||||||||||||||||||||||||||||||
| 506 | ); | ||||||||||||||||||||||||||||||||
| 507 | } | ||||||||||||||||||||||||||||||||
| 508 | elsif (defined $rule->{value}) { | ||||||||||||||||||||||||||||||||
| 509 | 29 | return $rule->{value}; | |||||||||||||||||||||||||||||||
| 510 | }; | ||||||||||||||||||||||||||||||||
| 511 | }; | ||||||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||||||
| 513 | 13 | return; | |||||||||||||||||||||||||||||||
| 514 | }; | ||||||||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||||||
| 517 | =item B<_mock_add_call>( I<method> : Str, I<args> : Array ) : Int | ||||||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||||||
| 519 | Adds one to the call count of a method and returns previous value. | ||||||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||||||
| 521 | =cut | ||||||||||||||||||||||||||||||||
| 522 | |||||||||||||||||||||||||||||||||
| 523 | 145 | ▶ | sub _mock_add_call { | ||||||||||||||||||||||||||||||
|
Subroutine
_mock_add_call: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 524 | 145 | my ($self, $method, @args) = @_; | |||||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||||||
| 526 | 145 | assert_not_null($method) if ASSERT; | |||||||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||||||
| 528 | 145 | assert_equals('HASH', ref $self->_mock_call) if ASSERT; | |||||||||||||||||||||||||||||||
| 529 | 145 | return $self->_mock_call->{$method}++; | |||||||||||||||||||||||||||||||
| 530 | }; | ||||||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||||||
| 532 | =item B<_mock_check_expectations>( I<method> : Str, I<timing> : Num, I<args> : Array ) : Self | ||||||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||||||
| 534 | Tests the arguments against expectations. | ||||||||||||||||||||||||||||||||
| 535 | |||||||||||||||||||||||||||||||||
| 536 | =cut | ||||||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||||||
| 538 | 145 | ▶ | sub _mock_check_expectations { | ||||||||||||||||||||||||||||||
|
Subroutine
_mock_check_expectations: called CC=24 SCAR=33.1
| |||||||||||||||||||||||||||||||||
| 539 | 145 | my ($self, $method, $timing, @args) = @_; | |||||||||||||||||||||||||||||||
| 540 | |||||||||||||||||||||||||||||||||
| 541 | 145 | assert_not_null($method) if ASSERT; | |||||||||||||||||||||||||||||||
| 542 | 145 | assert_not_null($timing) if ASSERT; | |||||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||||||
| 544 | 145 | my $rules_for_method = $self->_mock_expectation->{$method}; | |||||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||||||
| 546 | 145 | ▶ | return if not defined $rules_for_method | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition60%3/5
not defined $rules_for_method or (ref $rules_for_method || "") ne "ARRAY"
ref $rules_for_method || ''
MC/DC0%0/2
not defined $rules_for_method or (ref $rules_for_method || "") ne "ARRAY"
not defined $rules_for_method
ref $rules_for_method
Truth table66%2/3
not(A) or B
A= not defined $rules_for_methodB= ref $rules_for_method
| |||||||||||||||||||||||||||||||||
| 547 | or (ref $rules_for_method || '') ne 'ARRAY'; | ||||||||||||||||||||||||||||||||
| 548 | |||||||||||||||||||||||||||||||||
| 549 | 42 | my $e; | |||||||||||||||||||||||||||||||
| 550 | |||||||||||||||||||||||||||||||||
| 551 | RULE: | ||||||||||||||||||||||||||||||||
| 552 | 42 | foreach my $rule (@$rules_for_method) { | |||||||||||||||||||||||||||||||
| 553 | 50 | ▶ | if (defined $rule->{at}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 554 | 12 | ▶ | next RULE unless $timing == $rule->{at}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 555 | }; | ||||||||||||||||||||||||||||||||
| 556 | |||||||||||||||||||||||||||||||||
| 557 | 42 | eval { | |||||||||||||||||||||||||||||||
| 558 | TRY: { | ||||||||||||||||||||||||||||||||
| 559 | 42 | ▶ | if (exists $rule->{args}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 560 | my @rule_args = (ref $rule->{args} || '') eq 'ARRAY' | ||||||||||||||||||||||||||||||||
| 561 | 17 | ? @{ $rule->{args} } | |||||||||||||||||||||||||||||||
| 562 | 17 | ▶ | : ( $rule->{args} ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
ref $rule->{'args'} || ''
MC/DC0%0/1
ref $rule->{'args'} || ''
ref $rule->{'args'}
Truth table50%1/2
A
A= ref $rule->{'args'}
| |||||||||||||||||||||||||||||||||
| 563 | |||||||||||||||||||||||||||||||||
| 564 | # number of args matches? | ||||||||||||||||||||||||||||||||
| 565 | 17 | ▶ | next TRY unless @args == @rule_args; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 566 | |||||||||||||||||||||||||||||||||
| 567 | # iterate args | ||||||||||||||||||||||||||||||||
| 568 | 16 | foreach my $i (0 .. @rule_args - 1) { | |||||||||||||||||||||||||||||||
| 569 | 28 | my $rule_arg = $rule_args[$i]; | |||||||||||||||||||||||||||||||
| 570 | 28 | ▶ | if ((ref $rule_arg || '') eq 'Regexp') { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%2/2
ref $rule_arg || ''
MC/DC100%1/1
ref $rule_arg || ''
ref $rule_arg
Truth table100%2/2
A
A= ref $rule_arg
| |||||||||||||||||||||||||||||||||
| 571 | 6 | assert_matches($rule_arg, $args[$i]); | |||||||||||||||||||||||||||||||
| 572 | } | ||||||||||||||||||||||||||||||||
| 573 | elsif (ref $rule_arg) { | ||||||||||||||||||||||||||||||||
| 574 | 0 | assert_deep_equals($rule_arg, $args[$i]); | |||||||||||||||||||||||||||||||
| 575 | } | ||||||||||||||||||||||||||||||||
| 576 | else { | ||||||||||||||||||||||||||||||||
| 577 | 22 | assert_equals($rule_arg, $args[$i]); | |||||||||||||||||||||||||||||||
| 578 | }; | ||||||||||||||||||||||||||||||||
| 579 | }; | ||||||||||||||||||||||||||||||||
| 580 | }; | ||||||||||||||||||||||||||||||||
| 581 | |||||||||||||||||||||||||||||||||
| 582 | 39 | $rule->{call} ++; | |||||||||||||||||||||||||||||||
| 583 | |||||||||||||||||||||||||||||||||
| 584 | fail( [ | ||||||||||||||||||||||||||||||||
| 585 | 'Maximum call count (%d) for method (%s) at call (%d)', | ||||||||||||||||||||||||||||||||
| 586 | $rule->{maximum}, $method, $timing | ||||||||||||||||||||||||||||||||
| 587 | 39 | ▶ | ] ) if (defined $rule->{maximum} and $rule->{call} > $rule->{maximum}); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $rule->{"maximum"} and $rule->{"call"} > $rule->{"maximum"}
MC/DC100%2/2
defined $rule->{"maximum"} and $rule->{"call"} > $rule->{"maximum"}
defined $rule->{"maximum"}
$rule->{"call"} > $rule->{"maximum"}
Truth table100%3/3
A and B
A= defined $rule->{"maximum"}B= $rule->{"call"} > $rule->{"maximum"}
| |||||||||||||||||||||||||||||||||
| 588 | |||||||||||||||||||||||||||||||||
| 589 | fail( [ | ||||||||||||||||||||||||||||||||
| 590 | 'Expected call count (%d) for method (%s) at call (%d)', | ||||||||||||||||||||||||||||||||
| 591 | $rule->{count}, $method, $timing | ||||||||||||||||||||||||||||||||
| 592 | 36 | ▶ | ] ) if (defined $rule->{count} and $rule->{call} > $rule->{count}); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $rule->{"count"} and $rule->{"call"} > $rule->{"count"}
MC/DC100%2/2
defined $rule->{"count"} and $rule->{"call"} > $rule->{"count"}
defined $rule->{"count"}
$rule->{"call"} > $rule->{"count"}
Truth table100%3/3
A and B
A= defined $rule->{"count"}B= $rule->{"call"} > $rule->{"count"}
| |||||||||||||||||||||||||||||||||
| 593 | |||||||||||||||||||||||||||||||||
| 594 | 35 | ▶ | if (defined $rule->{assertion}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 595 | 0 | ▶ | if (ref $rule->{assertion} eq 'CODE') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 596 | 0 | fail( $rule->{assertion}->($method, $timing, @args) ); | |||||||||||||||||||||||||||||||
| 597 | } | ||||||||||||||||||||||||||||||||
| 598 | else { | ||||||||||||||||||||||||||||||||
| 599 | 0 | fail( $rule->{assertion} ); | |||||||||||||||||||||||||||||||
| 600 | }; | ||||||||||||||||||||||||||||||||
| 601 | }; | ||||||||||||||||||||||||||||||||
| 602 | }; | ||||||||||||||||||||||||||||||||
| 603 | }; | ||||||||||||||||||||||||||||||||
| 604 | 42 | ▶ | $e ||= Exception::Fatal->catch if $EVAL_ERROR; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$e ||= "Exception::Fatal"->catch
MC/DC0%0/2
$e ||= "Exception::Fatal"->catch
$e
"Exception::Fatal"->catch
Truth table33%1/3
A ||= B
A= $eB= "Exception::Fatal"->catch
| |||||||||||||||||||||||||||||||||
| 605 | }; | ||||||||||||||||||||||||||||||||
| 606 | |||||||||||||||||||||||||||||||||
| 607 | 42 | ▶ | $e->throw if $e; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 608 | |||||||||||||||||||||||||||||||||
| 609 | 36 | return; | |||||||||||||||||||||||||||||||
| 610 | }; | ||||||||||||||||||||||||||||||||
| 611 | |||||||||||||||||||||||||||||||||
| 612 | |||||||||||||||||||||||||||||||||
| 613 | 1 | 1; | |||||||||||||||||||||||||||||||
| 614 | |||||||||||||||||||||||||||||||||
| 615 | |||||||||||||||||||||||||||||||||
| 616 | =back | ||||||||||||||||||||||||||||||||
| 617 | |||||||||||||||||||||||||||||||||
| 618 | =begin umlwiki | ||||||||||||||||||||||||||||||||
| 619 | |||||||||||||||||||||||||||||||||
| 620 | = Class Diagram = | ||||||||||||||||||||||||||||||||
| 621 | |||||||||||||||||||||||||||||||||
| 622 | [ <<role>> | ||||||||||||||||||||||||||||||||
| 623 | Test::Mock::Class::Role::Object | ||||||||||||||||||||||||||||||||
| 624 | ----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||
| 625 | #_mock_call : HashRef | ||||||||||||||||||||||||||||||||
| 626 | #_mock_expectation : HashRef | ||||||||||||||||||||||||||||||||
| 627 | #_mock_action : HashRef | ||||||||||||||||||||||||||||||||
| 628 | ----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||
| 629 | +mock_return( method : Str, :value : Any, :at : Int, :args : ArrayRef[Any] ) : Self | ||||||||||||||||||||||||||||||||
| 630 | +mock_return_at( at : Int, method : Str, :args : ArrayRef[Any] ) : Self | ||||||||||||||||||||||||||||||||
| 631 | +mock_throw( method : Str, :at : Int, :exception : Str, :args : ArrayRef[Any] ) : Self | ||||||||||||||||||||||||||||||||
| 632 | +mock_throw_at( at : Int, method : Str, :args : ArrayRef[Any] ) : Self | ||||||||||||||||||||||||||||||||
| 633 | +mock_expect( method : Str, :at : Int, :minimum : Int, :maximum : Int, :count : Int, :args : ArrayRef[Any] ) : Self | ||||||||||||||||||||||||||||||||
| 634 | +mock_expect_at( at : Int, method : Str, :args : ArrayRef[Any] ) : Self | ||||||||||||||||||||||||||||||||
| 635 | +mock_expect_call_count( method : Str, count : Int, :args : ArrayRef[Any] ) : Self | ||||||||||||||||||||||||||||||||
| 636 | +mock_expect_maximum_call_count( method : Str, count : Int, :args : ArrayRef[Any] ) : Self | ||||||||||||||||||||||||||||||||
| 637 | +mock_expect_minimum_call_count( method : Str, count : Int, :args : ArrayRef[Any] ) : Self | ||||||||||||||||||||||||||||||||
| 638 | +mock_expect_never( method : Str, :args : ArrayRef[Any] ) : Self | ||||||||||||||||||||||||||||||||
| 639 | +mock_expect_once( method : Str, :args : ArrayRef[Any] ) : Self | ||||||||||||||||||||||||||||||||
| 640 | +mock_expect_at_least_once( method : Str, :args : ArrayRef[Any] ) : Self | ||||||||||||||||||||||||||||||||
| 641 | +mock_invoke( method : Str, args : Array ) : Any | ||||||||||||||||||||||||||||||||
| 642 | +mock_tally() : Self | ||||||||||||||||||||||||||||||||
| 643 | ] | ||||||||||||||||||||||||||||||||
| 644 | |||||||||||||||||||||||||||||||||
| 645 | =end umlwiki | ||||||||||||||||||||||||||||||||
| 646 | |||||||||||||||||||||||||||||||||
| 647 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||
| 648 | |||||||||||||||||||||||||||||||||
| 649 | L<Test::Mock::Class>. | ||||||||||||||||||||||||||||||||
| 650 | |||||||||||||||||||||||||||||||||
| 651 | =head1 BUGS | ||||||||||||||||||||||||||||||||
| 652 | |||||||||||||||||||||||||||||||||
| 653 | The expectations and return values should be refactored as objects rather than | ||||||||||||||||||||||||||||||||
| 654 | complex structure. | ||||||||||||||||||||||||||||||||
| 655 | |||||||||||||||||||||||||||||||||
| 656 | The API is not stable yet and can be changed in future. | ||||||||||||||||||||||||||||||||
| 657 | |||||||||||||||||||||||||||||||||
| 658 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||
| 659 | |||||||||||||||||||||||||||||||||
| 660 | Piotr Roszatycki <dexter@cpan.org> | ||||||||||||||||||||||||||||||||
| 661 | |||||||||||||||||||||||||||||||||
| 662 | =head1 LICENSE | ||||||||||||||||||||||||||||||||
| 663 | |||||||||||||||||||||||||||||||||
| 664 | Based on SimpleTest, an open source unit test framework for the PHP | ||||||||||||||||||||||||||||||||
| 665 | programming language, created by Marcus Baker, Jason Sweat, Travis Swicegood, | ||||||||||||||||||||||||||||||||
| 666 | Perrick Penet and Edward Z. Yang. | ||||||||||||||||||||||||||||||||
| 667 | |||||||||||||||||||||||||||||||||
| 668 | Copyright (c) 2009, 2010 Piotr Roszatycki <dexter@cpan.org>. | ||||||||||||||||||||||||||||||||
| 669 | |||||||||||||||||||||||||||||||||
| 670 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||||||
| 671 | under GNU Lesser General Public License. | ||||||||||||||||||||||||||||||||