| 1 | package Test::MockObject; | ||||||||||||||||||||||||||||
| 2 | 12 | $Test::MockObject::VERSION = '1.20200122'; | |||||||||||||||||||||||||||
| 3 | 12 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 12 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 12 | ▶ | use Scalar::Util qw( blessed refaddr reftype weaken ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | sub import | ||||||||||||||||||||||||||||
| 9 | 21 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 10 | 21 | my $self = shift; | |||||||||||||||||||||||||||
| 11 | 21 | ▶ | return unless grep /^-debug/, @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 12 | 2 | eval { | |||||||||||||||||||||||||||
| 13 | 2 | require UNIVERSAL::isa; | |||||||||||||||||||||||||||
| 14 | 2 | UNIVERSAL::isa::->import('verbose'); | |||||||||||||||||||||||||||
| 15 | }; | ||||||||||||||||||||||||||||
| 16 | 2 | eval { | |||||||||||||||||||||||||||
| 17 | 2 | require UNIVERSAL::can; | |||||||||||||||||||||||||||
| 18 | 2 | UNIVERSAL::can::->import('-always_warn'); | |||||||||||||||||||||||||||
| 19 | }; | ||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 12 | ▶ | use Test::Builder; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 12 | my $Test = Test::Builder->new(); | |||||||||||||||||||||||||||
| 25 | 12 | my (%calls, %subs); | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | sub new | ||||||||||||||||||||||||||||
| 28 | 20 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 29 | 20 | my ($class, $type) = @_; | |||||||||||||||||||||||||||
| 30 | 20 | ▶ | $type ||= {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$type ||= {}
MC/DC0%0/1
$type ||= {}
$type
Truth table50%1/2
A
A= $type
| |||||||||||||||||||||||||||||
| 31 | 20 | bless $type, $class; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | sub mock | ||||||||||||||||||||||||||||
| 35 | 24 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
mock: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 36 | 24 | my ($self, @names_and_subs) = @_; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 24 | ▶ | while ( my ($name, $sub) = splice @names_and_subs, 0, 2 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 25 | ▶ | $sub ||= sub {}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.2
Condition66%2/3
$sub ||= sub {
}
MC/DC0%0/2
$sub ||= sub {
}
$sub
sub {
}
Truth table66%2/3
A ||= B
A= $subB= sub {
}
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | # leading dash means unlog, otherwise do log | ||||||||||||||||||||||||||||
| 42 | 25 | ▶ | _set_log( $self, $name, ( $name =~ s/^-// ? 0 : 1 ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 25 | _subs( $self )->{$name} = $sub; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 24 | $self; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | sub set_isa | ||||||||||||||||||||||||||||
| 50 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
set_isa: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 51 | 2 | my ($self, @supers) = @_; | |||||||||||||||||||||||||||
| 52 | 2 | my $supers = _isas( $self ); | |||||||||||||||||||||||||||
| 53 | 2 | $supers->{$_} = 1 for @supers; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | sub set_always | ||||||||||||||||||||||||||||
| 57 | 7 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
set_always: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 58 | 7 | my ($self, $name, $value) = @_; | |||||||||||||||||||||||||||
| 59 | 7 | ▶ | $self->mock( $name, sub { $value } ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | sub set_true | ||||||||||||||||||||||||||||
| 63 | 7 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
set_true: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 64 | 7 | my $self = shift; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 7 | for my $name ( @_ ) | |||||||||||||||||||||||||||
| 67 | { | ||||||||||||||||||||||||||||
| 68 | 10 | ▶ | $self->mock( $name, sub { 1 } ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 7 | return $self; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | sub set_false | ||||||||||||||||||||||||||||
| 75 | 3 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
set_false: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 76 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 3 | for my $name ( @_ ) | |||||||||||||||||||||||||||
| 79 | { | ||||||||||||||||||||||||||||
| 80 | 5 | ▶ | $self->mock( $name, sub {} ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 3 | return $self; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | sub set_list | ||||||||||||||||||||||||||||
| 87 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
set_list: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 88 | 1 | my ($self, $name, @list) = @_; | |||||||||||||||||||||||||||
| 89 | 1 | ▶ | $self->mock( $name, sub { @{[ @list ]} } ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | sub set_series | ||||||||||||||||||||||||||||
| 93 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
set_series: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 94 | 2 | my ($self, $name, @list) = @_; | |||||||||||||||||||||||||||
| 95 | 2 | ▶ | $self->mock( $name, sub { return unless @list; shift @list } ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | sub set_bound | ||||||||||||||||||||||||||||
| 99 | 3 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
set_bound: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 100 | 3 | my ($self, $name, $ref) = @_; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | my %bindings = | ||||||||||||||||||||||||||||
| 103 | ( | ||||||||||||||||||||||||||||
| 104 | 2 | ▶ | SCALAR => sub { $$ref }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 105 | 1 | ▶ | ARRAY => sub { @$ref }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 106 | 1 | ▶ | HASH => sub { %$ref }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 107 | 3 | ); | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 3 | ▶ | return unless exists $bindings{reftype( $ref )}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 3 | $self->mock( $name, $bindings{reftype( $ref )} ); | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | # hack around debugging mode being too smart for my sub names | ||||||||||||||||||||||||||||
| 114 | 12 | my $old_p; | |||||||||||||||||||||||||||
| 115 | BEGIN | ||||||||||||||||||||||||||||
| 116 | 12 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 117 | 12 | $old_p = $^P; | |||||||||||||||||||||||||||
| 118 | 12 | $^P &= ~0x200; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | BEGIN | ||||||||||||||||||||||||||||
| 122 | 12 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 123 | 12 | for my $universal | |||||||||||||||||||||||||||
| 124 | ( { sub => \&_subs, name => 'can' }, { sub => \&_isas, name => 'isa' } ) | ||||||||||||||||||||||||||||
| 125 | { | ||||||||||||||||||||||||||||
| 126 | my $sub = sub | ||||||||||||||||||||||||||||
| 127 | 123 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 128 | 123 | my ($self, $sub) = @_; | |||||||||||||||||||||||||||
| 129 | 123 | local *__ANON__ = $universal->{name}; | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | # mockmethods are special cases, class methods are handled directly | ||||||||||||||||||||||||||||
| 132 | 123 | my $lookup = $universal->{sub}->( $self ); | |||||||||||||||||||||||||||
| 133 | 123 | ▶ | return $lookup->{$sub} if blessed $self and exists $lookup->{$sub}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
builtin::blessed($self) and exists $lookup->{$sub}
MC/DC100%2/2
builtin::blessed($self) and exists $lookup->{$sub}
builtin::blessed($self)
exists $lookup->{$sub}
Truth table100%3/3
A and B
A= builtin::blessed($self)B= exists $lookup->{$sub}
| |||||||||||||||||||||||||||||
| 134 | 117 | my $parent = 'SUPER::' . $universal->{name}; | |||||||||||||||||||||||||||
| 135 | 117 | return $self->$parent( $sub ); | |||||||||||||||||||||||||||
| 136 | 24 | }; | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 12 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 139 | 24 | *{ $universal->{name} } = $sub; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 12 | $^P = $old_p; | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | sub remove | ||||||||||||||||||||||||||||
| 146 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
remove: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 147 | 1 | my ($self, $sub) = @_; | |||||||||||||||||||||||||||
| 148 | 1 | delete _subs( $self )->{$sub}; | |||||||||||||||||||||||||||
| 149 | 1 | $self; | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | sub called | ||||||||||||||||||||||||||||
| 153 | 9 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
called: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 154 | 9 | my ($self, $sub) = @_; | |||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 9 | for my $called (reverse @{ _calls( $self ) }) | |||||||||||||||||||||||||||
| 157 | { | ||||||||||||||||||||||||||||
| 158 | 28 | ▶ | return 1 if $called->[0] eq $sub; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 6 | return 0; | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | sub clear | ||||||||||||||||||||||||||||
| 165 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
clear: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 166 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 167 | 2 | @{ _calls( $self ) } = (); | |||||||||||||||||||||||||||
| 168 | 2 | $self; | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | sub call_pos | ||||||||||||||||||||||||||||
| 172 | 7 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
call_pos: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 173 | 7 | $_[0]->_call($_[1], 0); | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | sub call_args | ||||||||||||||||||||||||||||
| 177 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
call_args: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 178 | 2 | return @{ $_[0]->_call($_[1], 1) }; | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | sub _call | ||||||||||||||||||||||||||||
| 182 | 17 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_call: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 183 | 17 | my ($self, $pos, $type) = @_; | |||||||||||||||||||||||||||
| 184 | 17 | my $calls = _calls( $self ); | |||||||||||||||||||||||||||
| 185 | 17 | ▶ | return if abs($pos) > @$calls; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 186 | 12 | ▶ | $pos-- if $pos > 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 187 | 12 | return $calls->[$pos][$type]; | |||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | sub call_args_string | ||||||||||||||||||||||||||||
| 191 | 3 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
call_args_string: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||
| 192 | 3 | ▶ | my $args = $_[0]->_call( $_[1], 1 ) or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 193 | 3 | ▶ | return join($_[2] || '', @$args); | ||||||||||||||||||||||||||
|
Condition100%2/2
$_[2] || ''
MC/DC100%1/1
$_[2] || ''
$_[2]
Truth table100%2/2
A
A= $_[2]
| |||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | sub call_args_pos | ||||||||||||||||||||||||||||
| 197 | 4 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
call_args_pos: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 198 | 4 | my ($self, $subpos, $argpos) = @_; | |||||||||||||||||||||||||||
| 199 | 4 | ▶ | my $args = $self->_call( $subpos, 1 ) or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 200 | 4 | ▶ | $argpos-- if $argpos > 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 201 | 4 | return $args->[$argpos]; | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | sub next_call | ||||||||||||||||||||||||||||
| 205 | 6 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
next_call: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 206 | 6 | my ($self, $num) = @_; | |||||||||||||||||||||||||||
| 207 | 6 | ▶ | $num ||= 1; | ||||||||||||||||||||||||||
|
Condition100%2/2
$num ||= 1
MC/DC100%1/1
$num ||= 1
$num
Truth table100%2/2
A
A= $num
| |||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 6 | my $calls = _calls( $self ); | |||||||||||||||||||||||||||
| 210 | 6 | ▶ | return unless @$calls >= $num; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | 5 | my ($call) = (splice(@$calls, 0, $num))[-1]; | |||||||||||||||||||||||||||
| 213 | 5 | ▶ | return wantarray() ? @$call : $call->[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | sub AUTOLOAD | ||||||||||||||||||||||||||||
| 217 | 42 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
AUTOLOAD: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 218 | 42 | our $AUTOLOAD; | |||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | 42 | my $self = shift; | |||||||||||||||||||||||||||
| 221 | 42 | my $sub; | |||||||||||||||||||||||||||
| 222 | { | ||||||||||||||||||||||||||||
| 223 | 42 | local $1; | |||||||||||||||||||||||||||
| 224 | 42 | ($sub) = $AUTOLOAD =~ /::(\w+)\z/; | |||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||
| 226 | 42 | ▶ | return if $sub eq 'DESTROY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | 42 | $self->dispatch_mocked_method( $sub, @_ ); | |||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | sub dispatch_mocked_method | ||||||||||||||||||||||||||||
| 232 | 42 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
dispatch_mocked_method: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 233 | 42 | my $self = $_[0]; | |||||||||||||||||||||||||||
| 234 | 42 | my $sub = splice( @_, 1, 1 ); | |||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | 42 | my $subs = _subs( $self ); | |||||||||||||||||||||||||||
| 237 | 42 | ▶ | if (exists $subs->{$sub}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 238 | { | ||||||||||||||||||||||||||||
| 239 | 41 | $self->log_call( $sub, @_ ); | |||||||||||||||||||||||||||
| 240 | 41 | goto &{ $subs->{$sub} }; | |||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||
| 242 | else | ||||||||||||||||||||||||||||
| 243 | { | ||||||||||||||||||||||||||||
| 244 | 1 | require Carp; | |||||||||||||||||||||||||||
| 245 | 1 | Carp::carp("Un-mocked method '$sub()' called"); | |||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | 1 | return; | |||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | sub log_call | ||||||||||||||||||||||||||||
| 252 | 49 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
log_call: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 253 | 49 | my ($self, $sub, @call_args) = @_; | |||||||||||||||||||||||||||
| 254 | 49 | ▶ | return unless _logs( $self, $sub ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | # prevent circular references with weaken | ||||||||||||||||||||||||||||
| 257 | 46 | for my $arg ( @call_args ) | |||||||||||||||||||||||||||
| 258 | { | ||||||||||||||||||||||||||||
| 259 | 57 | ▶ | next unless ref $arg; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 260 | 49 | ▶ | weaken( $arg ) if refaddr( $arg ) eq refaddr( $self ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | 46 | push @{ _calls( $self ) }, [ $sub, \@call_args ]; | |||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | sub called_ok | ||||||||||||||||||||||||||||
| 267 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
called_ok: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 268 | 2 | my ($self, $sub, $name) = @_; | |||||||||||||||||||||||||||
| 269 | 2 | ▶ | $name ||= "object called '$sub'"; | ||||||||||||||||||||||||||
|
Condition50%1/2
$name ||= "object called '${sub}'"
MC/DC0%0/1
$name ||= "object called '${sub}'"
$name
Truth table50%1/2
A
A= $name
| |||||||||||||||||||||||||||||
| 270 | 2 | $Test->ok( $self->called($sub), $name ); | |||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | sub called_pos_ok | ||||||||||||||||||||||||||||
| 274 | 3 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
called_pos_ok: called CC=5 SCAR=16.5
Pod
documented
| |||||||||||||||||||||||||||||
| 275 | 3 | my ($self, $pos, $sub, $name) = @_; | |||||||||||||||||||||||||||
| 276 | 3 | ▶ | $name ||= "object called '$sub' at position $pos"; | ||||||||||||||||||||||||||
|
Condition50%1/2
$name ||= "object called '${sub}' at position $pos"
MC/DC0%0/1
$name ||= "object called '${sub}' at position $pos"
$name
Truth table50%1/2
A
A= $name
| |||||||||||||||||||||||||||||
| 277 | 3 | my $called = $self->call_pos($pos, $sub); | |||||||||||||||||||||||||||
| 278 | 3 | ▶ | unless ($Test->ok( (defined $called and $called eq $sub), $name )) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $called && $called eq $sub
MC/DC50%1/2
defined $called && $called eq $sub
defined $called
$called eq $sub
Truth table66%2/3
A && B
A= defined $calledB= $called eq $sub
| |||||||||||||||||||||||||||||
| 279 | { | ||||||||||||||||||||||||||||
| 280 | 2 | ▶ | $called = 'undef' unless defined $called; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 281 | 2 | $Test->diag("Got:\n\t'$called'\nExpected:\n\t'$sub'\n"); | |||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | sub called_args_string_is | ||||||||||||||||||||||||||||
| 286 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
called_args_string_is: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 287 | 1 | my ($self, $pos, $sep, $expected, $name) = @_; | |||||||||||||||||||||||||||
| 288 | 1 | ▶ | $name ||= "object sent expected args to sub at position $pos"; | ||||||||||||||||||||||||||
|
Condition50%1/2
$name ||= "object sent expected args to sub at position $pos"
MC/DC0%0/1
$name ||= "object sent expected args to sub at position $pos"
$name
Truth table50%1/2
A
A= $name
| |||||||||||||||||||||||||||||
| 289 | 1 | $Test->is_eq( $self->call_args_string( $pos, $sep ), $expected, $name ); | |||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | sub called_args_pos_is | ||||||||||||||||||||||||||||
| 293 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
called_args_pos_is: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 294 | 1 | my ($self, $pos, $argpos, $arg, $name) = @_; | |||||||||||||||||||||||||||
| 295 | 1 | ▶ | $name ||= "object sent expected arg '$arg' to sub at position $pos"; | ||||||||||||||||||||||||||
|
Condition50%1/2
$name ||= "object sent expected arg '${arg}' to sub at position $pos"
MC/DC0%0/1
$name ||= "object sent expected arg '${arg}' to sub at position $pos"
$name
Truth table50%1/2
A
A= $name
| |||||||||||||||||||||||||||||
| 296 | 1 | $Test->is_eq( $self->call_args_pos( $pos, $argpos ), $arg, $name ); | |||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | sub fake_module | ||||||||||||||||||||||||||||
| 300 | 8 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
fake_module: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 301 | 8 | my ($class, $modname, %subs) = @_; | |||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | 8 | ▶ | if ($class->check_class_loaded( $modname ) and ! keys %subs) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$class->check_class_loaded($modname) and not keys %subs
MC/DC100%2/2
$class->check_class_loaded($modname) and not keys %subs
$class->check_class_loaded($modname)
not keys %subs
Truth table100%3/3
A and not(B)
A= $class->check_class_loaded($modname)B= not keys %subs
| |||||||||||||||||||||||||||||
| 304 | { | ||||||||||||||||||||||||||||
| 305 | 1 | require Carp; | |||||||||||||||||||||||||||
| 306 | 1 | Carp::croak( "No mocked subs for loaded module '$modname'" ); | |||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | 7 | $modname =~ s!::!/!g; | |||||||||||||||||||||||||||
| 310 | 7 | $INC{ $modname . '.pm' } = 1; | |||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | 12 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 313 | { | ||||||||||||||||||||||||||||
| 314 | 12 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 315 | 7 | ▶ | ${ $modname . '::' }{VERSION} ||= -1; | ||||||||||||||||||||||||||
|
Condition100%2/2
do {
$modname . '::'
}->{"VERSION"} ||= -1
MC/DC100%1/1
do {
$modname . '::'
}->{"VERSION"} ||= -1
do {
$modname . '::'
}->{"VERSION"}
Truth table100%2/2
A
A= do {
$modname . '::'
}->{"VERSION"}
| |||||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | 7 | for my $sub (keys %subs) | |||||||||||||||||||||||||||
| 319 | { | ||||||||||||||||||||||||||||
| 320 | 5 | ▶ | my $type = reftype( $subs{ $sub } ) || ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
builtin::reftype($subs{$sub}) || ''
MC/DC100%1/1
builtin::reftype($subs{$sub}) || ''
builtin::reftype($subs{$sub})
Truth table100%2/2
A
A= builtin::reftype($subs{$sub})
| |||||||||||||||||||||||||||||
| 321 | 5 | ▶ | unless ( $type eq 'CODE' ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 322 | { | ||||||||||||||||||||||||||||
| 323 | 1 | require Carp; | |||||||||||||||||||||||||||
| 324 | 1 | Carp::carp("'$sub' is not a code reference" ); | |||||||||||||||||||||||||||
| 325 | 1 | next; | |||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||
| 327 | 12 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 328 | 4 | *{ $_[1] . '::' . $sub } = $subs{ $sub }; | |||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | sub check_class_loaded | ||||||||||||||||||||||||||||
| 333 | 32 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
check_class_loaded: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 334 | 32 | my ($self, $class, $load_flag) = @_; | |||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | 32 | (my $path = $class) =~ s{::}{/}g; | |||||||||||||||||||||||||||
| 337 | 32 | ▶ | return 1 if exists $INC{ $path . '.pm' }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | 19 | my $symtable = \%main::; | |||||||||||||||||||||||||||
| 340 | 19 | my $found = 1; | |||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | 19 | for my $symbol ( split( '::', $class )) | |||||||||||||||||||||||||||
| 343 | { | ||||||||||||||||||||||||||||
| 344 | 23 | ▶ | unless (exists $symtable->{ $symbol . '::' }) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 345 | { | ||||||||||||||||||||||||||||
| 346 | 6 | $found = 0; | |||||||||||||||||||||||||||
| 347 | 6 | last; | |||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | 17 | $symtable = $symtable->{ $symbol . '::' }; | |||||||||||||||||||||||||||
| 351 | } | ||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | 19 | return $found; | |||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | sub fake_new | ||||||||||||||||||||||||||||
| 357 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
fake_new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 358 | 1 | my ($self, $class) = @_; | |||||||||||||||||||||||||||
| 359 | 1 | ▶ | $self->fake_module( $class, new => sub { $self } ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | sub DESTROY | ||||||||||||||||||||||||||||
| 363 | 33 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 364 | 33 | my $self = shift; | |||||||||||||||||||||||||||
| 365 | 33 | $self->_clear_calls(); | |||||||||||||||||||||||||||
| 366 | 33 | $self->_clear_subs(); | |||||||||||||||||||||||||||
| 367 | 33 | $self->_clear_logs(); | |||||||||||||||||||||||||||
| 368 | 33 | $self->_clear_isas(); | |||||||||||||||||||||||||||
| 369 | } | ||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | sub _get_key | ||||||||||||||||||||||||||||
| 372 | 493 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_get_key: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 373 | 493 | my $invocant = shift; | |||||||||||||||||||||||||||
| 374 | 493 | ▶ | return blessed( $invocant ) ? refaddr( $invocant ) : $invocant; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 375 | } | ||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | { | ||||||||||||||||||||||||||||
| 378 | 12 | my %calls; | |||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | sub _calls | ||||||||||||||||||||||||||||
| 381 | 82 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_calls: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 382 | 82 | ▶ | $calls{ _get_key( shift ) } ||= []; | ||||||||||||||||||||||||||
|
Condition100%2/2
$calls{_get_key(shift())} ||= []
MC/DC100%1/1
$calls{_get_key(shift())} ||= []
$calls{_get_key(shift())}
Truth table100%2/2
A
A= $calls{_get_key(shift())}
| |||||||||||||||||||||||||||||
| 383 | } | ||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||
| 385 | sub _clear_calls | ||||||||||||||||||||||||||||
| 386 | 33 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_clear_calls: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 387 | 33 | delete $calls{ _get_key( shift ) }; | |||||||||||||||||||||||||||
| 388 | } | ||||||||||||||||||||||||||||
| 389 | } | ||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | { | ||||||||||||||||||||||||||||
| 392 | 12 | my %subs; | |||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||
| 394 | sub _subs | ||||||||||||||||||||||||||||
| 395 | 185 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_subs: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 396 | 185 | ▶ | $subs{ _get_key( shift ) } ||= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$subs{_get_key(shift())} ||= {}
MC/DC100%1/1
$subs{_get_key(shift())} ||= {}
$subs{_get_key(shift())}
Truth table100%2/2
A
A= $subs{_get_key(shift())}
| |||||||||||||||||||||||||||||
| 397 | } | ||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | sub _clear_subs | ||||||||||||||||||||||||||||
| 400 | 33 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_clear_subs: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 401 | 33 | delete $subs{ _get_key( shift ) }; | |||||||||||||||||||||||||||
| 402 | } | ||||||||||||||||||||||||||||
| 403 | } | ||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | { | ||||||||||||||||||||||||||||
| 406 | 12 | my %logs; | |||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||
| 408 | sub _set_log | ||||||||||||||||||||||||||||
| 409 | 35 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_set_log: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 410 | 35 | my $key = _get_key( shift ); | |||||||||||||||||||||||||||
| 411 | 35 | my ($name, $log) = @_; | |||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||
| 413 | 35 | ▶ | $logs{$key} ||= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$logs{$key} ||= {}
MC/DC100%1/1
$logs{$key} ||= {}
$logs{$key}
Truth table100%2/2
A
A= $logs{$key}
| |||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||
| 415 | 35 | ▶ | if ($log) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 416 | { | ||||||||||||||||||||||||||||
| 417 | 31 | $logs{$key}{$name} = 1; | |||||||||||||||||||||||||||
| 418 | } | ||||||||||||||||||||||||||||
| 419 | else | ||||||||||||||||||||||||||||
| 420 | { | ||||||||||||||||||||||||||||
| 421 | 4 | delete $logs{$key}{$name}; | |||||||||||||||||||||||||||
| 422 | } | ||||||||||||||||||||||||||||
| 423 | } | ||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||
| 425 | sub _logs | ||||||||||||||||||||||||||||
| 426 | 49 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_logs: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 427 | 49 | my $key = _get_key( shift ); | |||||||||||||||||||||||||||
| 428 | 49 | my ($name) = @_; | |||||||||||||||||||||||||||
| 429 | 49 | return exists $logs{$key}{$name}; | |||||||||||||||||||||||||||
| 430 | } | ||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | sub _clear_logs | ||||||||||||||||||||||||||||
| 433 | 33 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_clear_logs: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 434 | 33 | delete $logs{ _get_key( shift ) }; | |||||||||||||||||||||||||||
| 435 | } | ||||||||||||||||||||||||||||
| 436 | } | ||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||
| 438 | { | ||||||||||||||||||||||||||||
| 439 | 12 | my %isas; | |||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||
| 441 | sub _isas | ||||||||||||||||||||||||||||
| 442 | 10 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_isas: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 443 | 10 | ▶ | $isas{ _get_key( shift ) } ||= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$isas{_get_key(shift())} ||= {}
MC/DC100%1/1
$isas{_get_key(shift())} ||= {}
$isas{_get_key(shift())}
Truth table100%2/2
A
A= $isas{_get_key(shift())}
| |||||||||||||||||||||||||||||
| 444 | } | ||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||
| 446 | sub _clear_isas | ||||||||||||||||||||||||||||
| 447 | 33 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_clear_isas: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 448 | 33 | delete $isas{ _get_key( shift ) }; | |||||||||||||||||||||||||||
| 449 | } | ||||||||||||||||||||||||||||
| 450 | } | ||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||
| 452 | 12 | 1; | |||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||
| 454 | __END__ | ||||||||||||||||||||||||||||