| 1 | package Test::MockObject::Extends; | ||||||||||||||||||||||||||||
| 2 | 5 | $Test::MockObject::Extends::VERSION = '1.20200122'; | |||||||||||||||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use Test::MockObject; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | # Alias our 'import' to T:MO::import to handle this: | ||||||||||||||||||||||||||||
| 9 | # use Test::MockObject::Extends '-debug'; | ||||||||||||||||||||||||||||
| 10 | 5 | *import = \&Test::MockObject::import; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 5 | ▶ | use Devel::Peek 'CvGV'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 5 | ▶ | use Scalar::Util 'blessed'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 5 | ▶ | use constant PERL_5_9 => $^V gt v5.9.0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | sub new | ||||||||||||||||||||||||||||
| 18 | 16 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 19 | 16 | my ($class, $fake_class) = @_; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 16 | ▶ | return Test::MockObject->new() unless defined $fake_class; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 15 | my $parent_class = $class->get_class( $fake_class ); | |||||||||||||||||||||||||||
| 24 | 15 | $class->check_class_loaded( $parent_class ); | |||||||||||||||||||||||||||
| 25 | 15 | ▶ | my $self = blessed( $fake_class ) ? $fake_class : {}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | # Fields now locks the hash as of 5.9.0 - #84535 | ||||||||||||||||||||||||||||
| 28 | 15 | ▶ | if (PERL_5_9 && blessed( $fake_class ) && do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
builtin::blessed($fake_class) and do {
no strict 'refs';
exists do {
$parent_class . '::'
}->{"FIELDS"}
}
MC/DC100%2/2
builtin::blessed($fake_class) and do {
no strict 'refs';
exists do {
$parent_class . '::'
}->{"FIELDS"}
}
builtin::blessed($fake_class)
do {
no strict 'refs';
exists do {
$parent_class . '::'
}->{"FIELDS"}
}
Truth table100%3/3
A and B
A= builtin::blessed($fake_class)B= do {
no strict 'refs';
exists do {
$parent_class . '::'
}->{"FIELDS"}
}
| |||||||||||||||||||||||||||||
| 29 | 5 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 10 | exists ${$parent_class . '::'}{FIELDS} # uses fields | |||||||||||||||||||||||||||
| 31 | 10 | }) { | |||||||||||||||||||||||||||
| 32 | # bypass prototypes | ||||||||||||||||||||||||||||
| 33 | 1 | &Hash::Util::unlock_hash(\%$fake_class); | |||||||||||||||||||||||||||
| 34 | 1 | bless $self, $class->gen_package( $parent_class ); | |||||||||||||||||||||||||||
| 35 | 1 | &Hash::Util::lock_keys(\%$fake_class, | |||||||||||||||||||||||||||
| 36 | fields::_accessible_keys($parent_class)); | ||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | else | ||||||||||||||||||||||||||||
| 39 | { | ||||||||||||||||||||||||||||
| 40 | 14 | bless $self, $class->gen_package( $parent_class ); | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 15 | return $self; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | sub check_class_loaded | ||||||||||||||||||||||||||||
| 47 | 15 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
check_class_loaded: called CC=2 SCAR=8.4
Pod
documented
| |||||||||||||||||||||||||||||
| 48 | 15 | my ($self, $parent_class) = @_; | |||||||||||||||||||||||||||
| 49 | 15 | my $result = Test::MockObject->check_class_loaded( | |||||||||||||||||||||||||||
| 50 | $parent_class | ||||||||||||||||||||||||||||
| 51 | ); | ||||||||||||||||||||||||||||
| 52 | 15 | ▶ | return $result if $result; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 0 | (my $load_class = $parent_class) =~ s/::/\//g; | |||||||||||||||||||||||||||
| 55 | 0 | require $load_class . '.pm'; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | sub get_class | ||||||||||||||||||||||||||||
| 59 | 15 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
get_class: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 60 | 15 | my ($self, $invocant) = @_; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 15 | ▶ | return $invocant unless blessed $invocant; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 10 | return ref $invocant; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 5 | my $packname = 'a'; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | sub gen_package | ||||||||||||||||||||||||||||
| 69 | 15 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
gen_package: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 70 | 15 | my ($class, $parent) = @_; | |||||||||||||||||||||||||||
| 71 | 15 | my $package = 'T::MO::E::' . $packname++; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 5 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 74 | 15 | *{ $package . '::mock' } = \&mock; | |||||||||||||||||||||||||||
| 75 | 15 | *{ $package . '::unmock' } = \&unmock; | |||||||||||||||||||||||||||
| 76 | 15 | @{ $package . '::ISA' } = ( $parent ); | |||||||||||||||||||||||||||
| 77 | 15 | *{ $package . '::can' } = $class->gen_can( $parent ); | |||||||||||||||||||||||||||
| 78 | 15 | *{ $package . '::isa' } = $class->gen_isa( $parent ); | |||||||||||||||||||||||||||
| 79 | 15 | *{ $package . '::AUTOLOAD' } = $class->gen_autoload( $parent ); | |||||||||||||||||||||||||||
| 80 | 15 | *{ $package . '::__get_parents' } = $class->gen_get_parents( $parent ); | |||||||||||||||||||||||||||
| 81 | 15 | *{ $package . '::DESTROY' } = $class->gen_destroy( $parent ); | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 15 | return $package; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | sub gen_get_parents | ||||||||||||||||||||||||||||
| 87 | 15 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
gen_get_parents: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 88 | 15 | my ($self, $parent) = @_; | |||||||||||||||||||||||||||
| 89 | return sub | ||||||||||||||||||||||||||||
| 90 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 91 | 5 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 92 | 1 | return @{ $parent . '::ISA' }; | |||||||||||||||||||||||||||
| 93 | 15 | }; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | sub gen_isa | ||||||||||||||||||||||||||||
| 97 | 15 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
gen_isa: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 98 | 15 | my ($class, $parent) = @_; | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | sub | ||||||||||||||||||||||||||||
| 101 | 11 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 102 | 11 | local *__ANON__ = 'isa'; | |||||||||||||||||||||||||||
| 103 | 11 | my ($self, $class) = @_; | |||||||||||||||||||||||||||
| 104 | 11 | ▶ | return 1 if $class eq $parent; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 4 | my $isa = $parent->can( 'isa' ); | |||||||||||||||||||||||||||
| 106 | 4 | return $isa->( $self, $class ); | |||||||||||||||||||||||||||
| 107 | 15 | }; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | sub gen_can | ||||||||||||||||||||||||||||
| 111 | 15 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
gen_can: called CC=1 SCAR=3.9
Pod
documented
| |||||||||||||||||||||||||||||
| 112 | 15 | my ($class, $parent) = @_; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | sub | ||||||||||||||||||||||||||||
| 115 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 116 | 0 | local *__ANON__ = 'can'; | |||||||||||||||||||||||||||
| 117 | 0 | my ($self, $method) = @_; | |||||||||||||||||||||||||||
| 118 | 0 | my $parent_method = $self->SUPER::can( $method ); | |||||||||||||||||||||||||||
| 119 | 0 | ▶ | return $parent_method if $parent_method; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 0 | return Test::MockObject->can( $method ); | |||||||||||||||||||||||||||
| 121 | 15 | }; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | sub gen_destroy | ||||||||||||||||||||||||||||
| 125 | 15 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
gen_destroy: called CC=1 SCAR=0.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 126 | 15 | my ($class, $parent) = @_; | |||||||||||||||||||||||||||
| 127 | 15 | my $destroy; | |||||||||||||||||||||||||||
| 128 | $destroy = sub | ||||||||||||||||||||||||||||
| 129 | 14 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=12.0
| |||||||||||||||||||||||||||||
| 130 | 14 | my $self = shift; | |||||||||||||||||||||||||||
| 131 | 14 | my $parent_destroy = $parent->can( 'DESTROY' ); | |||||||||||||||||||||||||||
| 132 | 14 | ▶ | $self->$parent_destroy if $parent_destroy && $parent_destroy != $destroy; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$parent_destroy and $parent_destroy != $destroy
MC/DC0%0/2
$parent_destroy and $parent_destroy != $destroy
$parent_destroy
$parent_destroy != $destroy
Truth table33%1/3
A and B
A= $parent_destroyB= $parent_destroy != $destroy
| |||||||||||||||||||||||||||||
| 133 | 14 | $self->Test::MockObject::DESTROY; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | 15 | } | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | sub gen_autoload | ||||||||||||||||||||||||||||
| 138 | 15 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
gen_autoload: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 139 | 15 | my ($class, $parent) = @_; | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | sub | ||||||||||||||||||||||||||||
| 142 | 83 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 143 | 83 | our $AUTOLOAD; | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 83 | my $method = substr( $AUTOLOAD, rindex( $AUTOLOAD, ':' ) +1 ); | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 83 | my $self = shift; | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 83 | ▶ | if (my $parent_method = $parent->can( $method )) | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 150 | { | ||||||||||||||||||||||||||||
| 151 | 0 | return $self->$parent_method( @_ ); | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | elsif (my $mock_method = Test::MockObject->can( $method )) | ||||||||||||||||||||||||||||
| 154 | { | ||||||||||||||||||||||||||||
| 155 | 78 | return $self->$mock_method( @_ ); | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | elsif (my $parent_al = $parent->can( 'AUTOLOAD' )) | ||||||||||||||||||||||||||||
| 158 | { | ||||||||||||||||||||||||||||
| 159 | 4 | my ($parent_pack) = CvGV( $parent_al ) =~ /\*(.*)::AUTOLOAD/; | |||||||||||||||||||||||||||
| 160 | { | ||||||||||||||||||||||||||||
| 161 | 5 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 162 | 4 | ${ "${parent_pack}::AUTOLOAD" } = "${parent}::${method}"; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | 4 | unshift @_, $self; | |||||||||||||||||||||||||||
| 165 | 4 | goto &$parent_al; | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | else | ||||||||||||||||||||||||||||
| 168 | { | ||||||||||||||||||||||||||||
| 169 | 1 | die "Undefined method $method at ", join( ' ', caller() ), "\n"; | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | 15 | }; | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | sub mock | ||||||||||||||||||||||||||||
| 175 | 9 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
mock: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 176 | 9 | my ($self, $name, $sub) = @_; | |||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 9 | ▶ | Test::MockObject::_set_log( $self, $name, ( $name =~ s/^-// ? 0 : 1 ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | my $mock_sub = sub | ||||||||||||||||||||||||||||
| 181 | 8 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 182 | 8 | my ($self) = @_; | |||||||||||||||||||||||||||
| 183 | 8 | $self->log_call( $name, @_ ); | |||||||||||||||||||||||||||
| 184 | 8 | $sub->( @_ ); | |||||||||||||||||||||||||||
| 185 | 9 | }; | |||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | { | ||||||||||||||||||||||||||||
| 188 | 5 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 189 | 5 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 190 | 9 | *{ ref( $self ) . '::' . $name } = $mock_sub; | |||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 9 | return $self; | |||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | sub unmock | ||||||||||||||||||||||||||||
| 197 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
unmock: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 198 | 1 | my ($self, $name) = @_; | |||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 1 | Test::MockObject::_set_log( $self, $name, 0 ); | |||||||||||||||||||||||||||
| 201 | 5 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 202 | 1 | my $glob = *{ ref( $self ) . '::' }; | |||||||||||||||||||||||||||
| 203 | 1 | delete $glob->{ $name }; | |||||||||||||||||||||||||||
| 204 | 1 | return $self; | |||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 5 | 1; | |||||||||||||||||||||||||||
| 208 | __END__ | ||||||||||||||||||||||||||||