| 1 | package Test2::Tools::Mock; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 162 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 162 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 162 | ▶ | use Carp qw/croak/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 162 | ▶ | use Scalar::Util qw/blessed reftype weaken/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 162 | ▶ | use Test2::Util qw/try/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 162 | ▶ | use Test2::Util::Sub qw/gen_accessor gen_reader gen_writer/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 162 | ▶ | use Test2::Mock(); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 162 | ▶ | use base 'Exporter'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 162 | our $VERSION = '1.302226'; | |||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 162 | our @CARP_NOT = (__PACKAGE__, 'Test2::Mock'); | |||||||||||||||||||||||||||||||||||||||||
| 17 | 162 | our @EXPORT = qw/mock mocked/; | |||||||||||||||||||||||||||||||||||||||||
| 18 | 162 | our @EXPORT_OK = qw{ | |||||||||||||||||||||||||||||||||||||||||
| 19 | mock_obj mock_class | ||||||||||||||||||||||||||||||||||||||||||
| 20 | mock_do mock_build | ||||||||||||||||||||||||||||||||||||||||||
| 21 | mock_accessor mock_accessors | ||||||||||||||||||||||||||||||||||||||||||
| 22 | mock_getter mock_getters | ||||||||||||||||||||||||||||||||||||||||||
| 23 | mock_setter mock_setters | ||||||||||||||||||||||||||||||||||||||||||
| 24 | mock_building | ||||||||||||||||||||||||||||||||||||||||||
| 25 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | 162 | my %HANDLERS; | |||||||||||||||||||||||||||||||||||||||||
| 28 | my %MOCKS; | ||||||||||||||||||||||||||||||||||||||||||
| 29 | 162 | my @BUILD; | |||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | 35 | ▶ | sub add_handler { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_handler: called CC=4 SCAR=15.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 35 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 33 | 35 | my ($for, $code) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 35 | ▶ | croak "Must specify a package for the mock handler" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | unless $for; | ||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | 35 | ▶ | croak "Handlers must be code references (got: $code)" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$code and ref $code eq "CODE"
MC/DC0%0/2
$code and ref $code eq "CODE"
$code
ref $code eq "CODE"
Truth table33%1/3
A and B
A= $codeB= ref $code eq "CODE"
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | unless $code && ref($code) eq 'CODE'; | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | 35 | push @{$HANDLERS{$for}} => $code; | |||||||||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | 7 | ▶ | sub mock_building { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
mock_building: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 7 | ▶ | return unless @BUILD; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 3 | return $BUILD[-1]; | |||||||||||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | 44 | ▶ | sub mocked { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
mocked: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 44 | my $proto = shift; | |||||||||||||||||||||||||||||||||||||||||
| 51 | 44 | ▶ | my $class = blessed($proto) || $proto; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
builtin::blessed($proto) || $proto
MC/DC0%0/2
builtin::blessed($proto) || $proto
builtin::blessed($proto)
$proto
Truth table66%2/3
A || B
A= builtin::blessed($proto)B= $proto
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | # Check if we have any mocks. | ||||||||||||||||||||||||||||||||||||||||||
| 54 | 44 | ▶ | my $set = $MOCKS{$class} || return; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$MOCKS{$class} || (return)
MC/DC100%1/1
$MOCKS{$class} || (return)
$MOCKS{$class}
Truth table100%2/2
A
A= $MOCKS{$class}
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | # Remove dead mocks (undef due to weaken) | ||||||||||||||||||||||||||||||||||||||||||
| 57 | 22 | ▶ | pop @$set while @$set && !defined($set->[-1]); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@$set and not defined $set->[-1]
MC/DC100%2/2
@$set and not defined $set->[-1]
@$set
not defined $set->[-1]
Truth table100%3/3
A and not(B)
A= @$setB= not defined $set->[-1]
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | # Remove the list if it is empty | ||||||||||||||||||||||||||||||||||||||||||
| 60 | 22 | ▶ | delete $MOCKS{$class} unless @$set; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | # Return the controls (may be empty list) | ||||||||||||||||||||||||||||||||||||||||||
| 63 | 22 | return @$set; | |||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | 24 | ▶ | sub _delegate { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_delegate: called CC=13 SCAR=25.8
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 24 | my ($args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | 24 | my $do = __PACKAGE__->can('mock_do'); | |||||||||||||||||||||||||||||||||||||||||
| 70 | 24 | my $obj = __PACKAGE__->can('mock_obj'); | |||||||||||||||||||||||||||||||||||||||||
| 71 | 24 | my $class = __PACKAGE__->can('mock_class'); | |||||||||||||||||||||||||||||||||||||||||
| 72 | 24 | my $build = __PACKAGE__->can('mock_build'); | |||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | 24 | ▶ | return $obj unless @$args; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | 23 | my ($proto, $arg1) = @$args; | |||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | 23 | ▶ | return $obj if ref($proto) && !blessed($proto); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $proto and not builtin::blessed($proto)
MC/DC100%2/2
ref $proto and not builtin::blessed($proto)
ref $proto
not builtin::blessed($proto)
Truth table100%3/3
A and not(B)
A= ref $protoB= not builtin::blessed($proto)
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | 22 | ▶ | if (blessed($proto)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | 2 | ▶ | return $class unless $proto->isa('Test2::Mock'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | 1 | ▶ | return $build if $arg1 && ref($arg1) && reftype($arg1) eq 'CODE'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$arg1 and ref $arg1 and builtin::reftype($arg1) eq "CODE"
$arg1 and ref $arg1
MC/DC0%0/3
$arg1 and ref $arg1 and builtin::reftype($arg1) eq "CODE"
$arg1
ref $arg1
builtin::reftype($arg1) eq "CODE"
Truth table25%1/4
A and B and C
A= $arg1B= ref $arg1C= builtin::reftype($arg1) eq "CODE"
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | 20 | ▶ | return $class if $proto =~ m/(?:::|')/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | 15 | ▶ | return $class if $proto =~ m/^_*[A-Z]/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | 3 | ▶ | return $do if Test2::Mock->can($proto); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | 2 | ▶ | if (my $sub = __PACKAGE__->can("mock_$proto")) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | 1 | shift @$args; | |||||||||||||||||||||||||||||||||||||||||
| 92 | 1 | return $sub; | |||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | 1 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | 25 | ▶ | sub mock { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
mock: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 25 | ▶ | croak "undef is not a valid first argument to mock()" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@_ and not defined $_[0]
MC/DC100%2/2
@_ and not defined $_[0]
@_
not defined $_[0]
Truth table100%3/3
A and not(B)
A= @_B= not defined $_[0]
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | if @_ && !defined($_[0]); | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | 24 | my $sub = _delegate(\@_); | |||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | 24 | ▶ | croak "'$_[0]' does not look like a package name, and is not a valid control method" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | unless $sub; | ||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | 23 | $sub->(@_); | |||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | 8 | ▶ | sub mock_build { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
mock_build: called CC=8 SCAR=21.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | 8 | my ($control, $sub) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | 8 | ▶ | croak "mock_build requires a Test2::Mock object as its first argument" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$control and builtin::blessed($control) and $control->isa("Test2::Mock")
$control and builtin::blessed($control)
MC/DC33%1/3
$control and builtin::blessed($control) and $control->isa("Test2::Mock")
$control
builtin::blessed($control)
$control->isa("Test2::Mock")
Truth table50%2/4
A and B and C
A= $controlB= builtin::blessed($control)C= $control->isa("Test2::Mock")
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | unless $control && blessed($control) && $control->isa('Test2::Mock'); | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | 6 | ▶ | croak "mock_build requires a coderef as its second argument" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$sub and ref $sub and builtin::reftype($sub) eq "CODE"
$sub and ref $sub
MC/DC33%1/3
$sub and ref $sub and builtin::reftype($sub) eq "CODE"
$sub
ref $sub
builtin::reftype($sub) eq "CODE"
Truth table50%2/4
A and B and C
A= $subB= ref $subC= builtin::reftype($sub) eq "CODE"
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | unless $sub && ref($sub) && reftype($sub) eq 'CODE'; | ||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | 4 | push @BUILD => $control; | |||||||||||||||||||||||||||||||||||||||||
| 120 | 4 | my ($ok, $err) = &try($sub); | |||||||||||||||||||||||||||||||||||||||||
| 121 | 4 | pop @BUILD; | |||||||||||||||||||||||||||||||||||||||||
| 122 | 4 | ▶ | die $err unless $ok; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | 7 | ▶ | sub mock_do { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
mock_do: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | 7 | my ($meth, @args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | 7 | ▶ | croak "Not currently building a mock" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | unless @BUILD; | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | 5 | my $build = $BUILD[-1]; | |||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | 5 | ▶ | croak "'$meth' is not a valid action for mock_do()" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$meth =~ /^_/ or not $build->can($meth)
MC/DC50%1/2
$meth =~ /^_/ or not $build->can($meth)
$meth =~ /^_/
not $build->can($meth)
Truth table66%2/3
A or not(B)
A= $meth =~ /^_/B= not $build->can($meth)
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | if $meth =~ m/^_/ || !$build->can($meth); | ||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | 3 | $build->$meth(@args); | |||||||||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | 6 | ▶ | sub mock_obj { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
mock_obj: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | 6 | my ($proto) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | 6 | ▶ | if ($proto && ref($proto) && reftype($proto) ne 'CODE') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$proto and ref $proto and builtin::reftype($proto) ne "CODE"
$proto and ref $proto
MC/DC66%2/3
$proto and ref $proto and builtin::reftype($proto) ne "CODE"
$proto
ref $proto
builtin::reftype($proto) ne "CODE"
Truth table75%3/4
A and B and C
A= $protoB= ref $protoC= builtin::reftype($proto) ne "CODE"
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | 4 | shift @_; | |||||||||||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||||||||||
| 145 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 146 | 2 | $proto = {}; | |||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | 6 | my $class = _generate_class(); | |||||||||||||||||||||||||||||||||||||||||
| 150 | 6 | my $control; | |||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | 6 | ▶ | if (@_ == 1 && reftype($_[0]) eq 'CODE') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@_ == 1 and builtin::reftype($_[0]) eq "CODE"
MC/DC50%1/2
@_ == 1 and builtin::reftype($_[0]) eq "CODE"
@_ == 1
builtin::reftype($_[0]) eq "CODE"
Truth table66%2/3
A and B
A= @_ == 1B= builtin::reftype($_[0]) eq "CODE"
| |||||||||||||||||||||||||||||||||||||||||||
| 153 | 1 | my $orig = shift @_; | |||||||||||||||||||||||||||||||||||||||||
| 154 | $control = mock_class( | ||||||||||||||||||||||||||||||||||||||||||
| 155 | $class, | ||||||||||||||||||||||||||||||||||||||||||
| 156 | 1 | ▶ | sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | 1 | my $c = mock_building; | |||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||
| 159 | # We want to do these BEFORE anything that the sub may do. | ||||||||||||||||||||||||||||||||||||||||||
| 160 | 1 | $c->block_load(1); | |||||||||||||||||||||||||||||||||||||||||
| 161 | 1 | $c->purge_on_destroy(1); | |||||||||||||||||||||||||||||||||||||||||
| 162 | 1 | $c->autoload(1); | |||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||
| 164 | 1 | $orig->(@_); | |||||||||||||||||||||||||||||||||||||||||
| 165 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 166 | 1 | ); | |||||||||||||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||||||||||||
| 168 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 169 | 5 | $control = mock_class( | |||||||||||||||||||||||||||||||||||||||||
| 170 | $class, | ||||||||||||||||||||||||||||||||||||||||||
| 171 | # Do these before anything the user specified. | ||||||||||||||||||||||||||||||||||||||||||
| 172 | block_load => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 173 | purge_on_destroy => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 174 | autoload => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 175 | @_, | ||||||||||||||||||||||||||||||||||||||||||
| 176 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||
| 179 | 6 | my $new = bless($proto, $control->class); | |||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||
| 181 | # We need to ensure there is a reference to the control object, and we want | ||||||||||||||||||||||||||||||||||||||||||
| 182 | # it to go away with the object. | ||||||||||||||||||||||||||||||||||||||||||
| 183 | 6 | $new->{'~~MOCK~CONTROL~~'} = $control; | |||||||||||||||||||||||||||||||||||||||||
| 184 | 6 | return $new; | |||||||||||||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||
| 187 | 6 | ▶ | sub _generate_class { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_generate_class: called CC=5 SCAR=16.3
| |||||||||||||||||||||||||||||||||||||||||||
| 188 | 6 | my $prefix = __PACKAGE__; | |||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | 6 | for (1 .. 100) { | |||||||||||||||||||||||||||||||||||||||||
| 191 | my $postfix = join '', map { | ||||||||||||||||||||||||||||||||||||||||||
| 192 | 6 | my $cp = rand(26) + 65; | |||||||||||||||||||||||||||||||||||||||||
| 193 | 192 | ▶ | $cp = utf8::unicode_to_native($cp) if "$]" >= 5.008; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 194 | 192 | chr($cp); | |||||||||||||||||||||||||||||||||||||||||
| 195 | } 1 .. 32; | ||||||||||||||||||||||||||||||||||||||||||
| 196 | 6 | my $class = $prefix . '::__TEMP__::' . $postfix; | |||||||||||||||||||||||||||||||||||||||||
| 197 | 6 | my $file = $class; | |||||||||||||||||||||||||||||||||||||||||
| 198 | 6 | $file =~ s{::}{/}g; | |||||||||||||||||||||||||||||||||||||||||
| 199 | 6 | $file .= '.pm'; | |||||||||||||||||||||||||||||||||||||||||
| 200 | 6 | ▶ | next if $INC{$file}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 201 | 162 | ▶ | my $stash = do { no strict 'refs'; \%{"${class}\::"} }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 202 | 6 | ▶ | next if keys %$stash; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 203 | 6 | return $class; | |||||||||||||||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||
| 206 | 0 | croak "Could not generate a unique class name after 100 attempts"; | |||||||||||||||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||
| 209 | 29 | ▶ | sub mock_class { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
mock_class: called CC=8 SCAR=20.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 210 | 29 | my $proto = shift; | |||||||||||||||||||||||||||||||||||||||||
| 211 | 29 | ▶ | my $class = blessed($proto) || $proto; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
builtin::blessed($proto) || $proto
MC/DC0%0/2
builtin::blessed($proto) || $proto
builtin::blessed($proto)
$proto
Truth table66%2/3
A || B
A= builtin::blessed($proto)B= $proto
| |||||||||||||||||||||||||||||||||||||||||||
| 212 | 29 | my @args = @_; | |||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||
| 214 | 29 | my $void = !defined(wantarray); | |||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||
| 216 | 30 | ▶ | my $callback = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 217 | 30 | my ($parent) = reverse mocked($class); | |||||||||||||||||||||||||||||||||||||||||
| 218 | 30 | my $control; | |||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||
| 220 | 30 | ▶ | if (@args == 1 && ref($args[0]) && reftype($args[0]) eq 'CODE') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
@args == 1 and ref $args[0] and builtin::reftype($args[0]) eq "CODE"
@args == 1 and ref $args[0]
MC/DC33%1/3
@args == 1 and ref $args[0] and builtin::reftype($args[0]) eq "CODE"
@args == 1
ref $args[0]
builtin::reftype($args[0]) eq "CODE"
Truth table50%2/4
A and B and C
A= @args == 1B= ref $args[0]C= builtin::reftype($args[0]) eq "CODE"
| |||||||||||||||||||||||||||||||||||||||||||
| 221 | 1 | $control = Test2::Mock->new(class => $class); | |||||||||||||||||||||||||||||||||||||||||
| 222 | 1 | mock_build($control, @args); | |||||||||||||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||||||||||||
| 224 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 225 | 29 | $control = Test2::Mock->new(class => $class, @args); | |||||||||||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||
| 228 | 29 | ▶ | if ($parent) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 229 | 3 | $control->{parent} = $parent; | |||||||||||||||||||||||||||||||||||||||||
| 230 | 3 | weaken($parent->{child} = $control); | |||||||||||||||||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||
| 233 | 29 | ▶ | $MOCKS{$class} ||= []; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$MOCKS{$class} ||= []
MC/DC100%1/1
$MOCKS{$class} ||= []
$MOCKS{$class}
Truth table100%2/2
A
A= $MOCKS{$class}
| |||||||||||||||||||||||||||||||||||||||||||
| 234 | 29 | push @{$MOCKS{$class}} => $control; | |||||||||||||||||||||||||||||||||||||||||
| 235 | 29 | weaken($MOCKS{$class}->[-1]); | |||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||
| 237 | 29 | return $control; | |||||||||||||||||||||||||||||||||||||||||
| 238 | 29 | }; | |||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||
| 240 | 29 | ▶ | return $callback->() unless $void; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||
| 242 | 8 | my $level = 0; | |||||||||||||||||||||||||||||||||||||||||
| 243 | 8 | my $caller; | |||||||||||||||||||||||||||||||||||||||||
| 244 | 8 | ▶ | while (my @call = caller($level++)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 245 | 16 | ▶ | next if $call[0] eq __PACKAGE__; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 246 | 8 | $caller = \@call; | |||||||||||||||||||||||||||||||||||||||||
| 247 | 8 | last; | |||||||||||||||||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||||||||||
| 250 | 8 | my $handled; | |||||||||||||||||||||||||||||||||||||||||
| 251 | 8 | for my $handler (@{$HANDLERS{$caller->[0]}}) { | |||||||||||||||||||||||||||||||||||||||||
| 252 | 8 | ▶ | $handled++ if $handler->( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 253 | class => $class, | ||||||||||||||||||||||||||||||||||||||||||
| 254 | caller => $caller, | ||||||||||||||||||||||||||||||||||||||||||
| 255 | builder => $callback, | ||||||||||||||||||||||||||||||||||||||||||
| 256 | args => \@args, | ||||||||||||||||||||||||||||||||||||||||||
| 257 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||||||||
| 260 | 8 | ▶ | croak "mock_class should not be called in a void context without a registered handler" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 261 | unless $handled; | ||||||||||||||||||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||||||
| 264 | 1 | ▶ | sub mock_accessors { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
mock_accessors: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 265 | 1 | return map {( $_ => gen_accessor($_) )} @_; | |||||||||||||||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||
| 268 | 2 | ▶ | sub mock_accessor { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
mock_accessor: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 269 | 2 | my ($field) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 270 | 2 | return gen_accessor($field); | |||||||||||||||||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||
| 273 | 1 | ▶ | sub mock_getters { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
mock_getters: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 274 | 1 | my ($prefix, @list) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 275 | 1 | return map {( "$prefix$_" => gen_reader($_) )} @list; | |||||||||||||||||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||
| 278 | 1 | ▶ | sub mock_getter { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
mock_getter: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 279 | 1 | my ($field) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 280 | 1 | return gen_reader($field); | |||||||||||||||||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||
| 283 | 1 | ▶ | sub mock_setters { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
mock_setters: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 284 | 1 | my ($prefix, @list) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 285 | 1 | return map {( "$prefix$_" => gen_writer($_) )} @list; | |||||||||||||||||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||
| 288 | 1 | ▶ | sub mock_setter { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
mock_setter: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 289 | 1 | my ($field) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 290 | 1 | return gen_writer($field); | |||||||||||||||||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||||||
| 293 | 162 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||
| 295 | __END__ | ||||||||||||||||||||||||||||||||||||||||||