MethodFixtures.pm

filter: Statementstmt 100% 102 / 102 Branchbran 92.1% 35 / 38 Conditioncond 85.1% 23 / 27 MC/DCmcdc 46.6% 7 / 15 Subroutinesub 100% 16 / 16 Podpod 100% 4 / 4 totaltotal 92.5% 187 / 202 CC 30 SCAR 20.2
CC
30
Cov
96%
new
13.2
__ANON__
11.0
retrieve
5.0
CRAP 7.6
↑ Summary MethodFixtures/Storage/File.pm »
1 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Test::MethodFixtures;
5  
6 14 our $VERSION = '0.08';
7  
8 14 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 14 ▶use Hook::LexWrap qw( wrap );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 14 ▶use Scalar::Util qw( weaken blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 14 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 14 ▶use base 'Class::Accessor::Fast';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 14 __PACKAGE__->mk_accessors(qw( mode storage _wrapped ));
16  
17 14 our $DEFAULT_STORAGE = 'File';
18 14 our ( $MODE, $STORAGE );
19 14 my %VALID_MODES = (
20 playback => 1, # default mode
21 record => 1,
22 auto => 1,
23 passthrough => 1,
24 );
25  
26 16 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
27 16 my ( $class, %args ) = @_;
28  
29 16 $MODE = $args{'-mode'};
30 16 $STORAGE = $args{'-storage'};
31 }
32  
33 23 ▶sub new {
Subroutine new: called CC=13 SCAR=25.8
Pod documented
34 23 my $class = shift;
35 23 ▶ my %args = %{ shift() || {} };
Condition100%2/2
shift() || {}
l!l
221
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
36  
37 23 ▶ my $mode = delete $args{mode} || $MODE || 'playback';
Condition100%5/5
delete $args{'mode'} || $Test::MethodFixtures::MODE || 'playback'
l!l
1013
delete $args{"mode"} or $Test::MethodFixtures::MODE
l!l&&r!l&&!r
9113
MC/DC100%2/2
delete $args{'mode'} || $Test::MethodFixtures::MODE || 'playback'
delete $args{"mode"} $Test::MethodFixtures::MODE
Truth table100%3/3
A or B
A= delete $args{"mode"}B= $Test::MethodFixtures::MODE
ABresult
1X1
011
000
38 23 ▶ my $storage = delete $args{storage} || $STORAGE || $DEFAULT_STORAGE;
Condition83%5/6
delete $args{'storage'} || $Test::MethodFixtures::STORAGE || $Test::MethodFixtures::DEFAULT_STORAGE
l!l&&r!l&&!r
2030
delete $args{"storage"} or $Test::MethodFixtures::STORAGE
l!l&&r!l&&!r
1913
MC/DC0%0/3
delete $args{'storage'} || $Test::MethodFixtures::STORAGE || $Test::MethodFixtures::DEFAULT_STORAGE
delete $args{"storage"} $Test::MethodFixtures::STORAGE $Test::MethodFixtures::DEFAULT_STORAGE
Truth table75%3/4
A or B || C
A= delete $args{"storage"}B= $Test::MethodFixtures::STORAGEC= $Test::MethodFixtures::DEFAULT_STORAGE
ABCresult
1XX1
01X1
0011
0000
39  
40 # testing mode
41 23 ▶ $mode = $ENV{TEST_MF_MODE} || $mode;
Condition66%2/3
$ENV{'TEST_MF_MODE'} || $mode
l!l&&r!l&&!r
1220
MC/DC0%0/2
$ENV{'TEST_MF_MODE'} || $mode
$ENV{'TEST_MF_MODE'} $mode
Truth table66%2/3
A || B
A= $ENV{'TEST_MF_MODE'}B= $mode
ABresult
1X1
011
000
42  
43 23 ▶ croak "Invalid mode '$MODE'" unless $VALID_MODES{$mode};
Branch: 1 branch
BranchTrueFalseTotal
unless $VALID_MODES{$mode} 0 23 23
44  
45 # storage mechanism
46 23 ▶ $storage = { $storage => {} } unless ref $storage;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $storage 21 2 23
47  
48 23 ▶ if ( !blessed $storage ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($storage)) 22 1 23
49  
50 22 my ( $storage_class, $storage_args ) = %{$storage};
51  
52 22 ▶ $storage_class = __PACKAGE__ . "::Storage::" . $storage_class
Branch: 1 branch
BranchTrueFalseTotal
unless $storage_class =~ s/^\++// 2 20 22
53 unless $storage_class =~ s/^\++//;
54  
55 22 eval "require $storage_class";
56 22 ▶ croak "Unable to load '$storage_class': $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 22 22
57  
58 $storage = $storage_class->new(
59 22 ▶ { %{ $storage_args || {} },
Condition50%1/2
$storage_args || {}
l!l
220
MC/DC0%0/1
$storage_args || {}
$storage_args
Truth table50%1/2
A
A= $storage_args
Aresult
11
00
60 %args, # pass in any remaining arguments
61 }
62 );
63 }
64  
65 23 return $class->SUPER::new(
66 { mode => $mode,
67 storage => $storage,
68 _wrapped => {},
69 }
70 );
71 }
72  
73 26 ▶sub store {
Subroutine store: called CC=1 SCAR=0.0
Pod documented
74 26 my $self = shift;
75  
76 26 my %args = %{ shift() };
77  
78 26 $args{ ref $self } = $self->VERSION;
79 26 $args{ ref $self->storage } = $self->storage->VERSION;
80  
81 26 $self->storage->store( \%args );
82  
83 26 return $self;
84 }
85  
86 34 ▶sub retrieve {
Subroutine retrieve: called CC=5 SCAR=16.1
Pod documented
87 34 my ( $self, $args ) = @_;
88  
89 34 my $stored = $self->storage->retrieve($args);
90  
91 34 my $self_class = ref $self;
92 34 my $storage_class = ref $self->storage;
93  
94 _compare_versions( $self_class, $stored->{$self_class} )
95 34 ▶ if exists $stored->{$self_class};
Branch: 1 branch
BranchTrueFalseTotal
if exists $stored->{$self_class} 30 4 34
96  
97 _compare_versions( $storage_class, $stored->{$storage_class} )
98 34 ▶ if exists $stored->{$storage_class};
Branch: 1 branch
BranchTrueFalseTotal
if exists $stored->{$storage_class} 30 4 34
99  
100 34 ▶ unless ( defined $stored->{output} || $stored->{no_output} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $stored->{"output"} or $stored->{"no_output"}) 4 30 34
Condition100%3/3
defined $stored->{"output"} or $stored->{"no_output"}
l!l&&r!l&&!r
2734
MC/DC100%2/2
defined $stored->{"output"} or $stored->{"no_output"}
defined $stored->{"output"} $stored->{"no_output"}
Truth table100%3/3
A or B
A= defined $stored->{"output"}B= $stored->{"no_output"}
ABresult
1X1
011
000
101 4 die "Nothing stored for " . $args->{method};
102 }
103  
104 30 return $stored;
105 }
106  
107 60 ▶sub _compare_versions {
Subroutine _compare_versions: called CC=2 SCAR=6.9
108 60 my ( $class, $version ) = @_;
109  
110 60 ▶ carp "Data saved with a more recent version ($version) of $class!"
Branch: 1 branch
BranchTrueFalseTotal
if "version"->parse($class->VERSION) < "version"->parse($version) 6 54 60
111 if version->parse( $class->VERSION ) < version->parse($version);
112 }
113  
114 # pass in optional coderef to return list of values to use
115 # (for example to stringify objects)
116 15 ▶sub _get_key_sub {
Subroutine _get_key_sub: called CC=1 SCAR=0.0
117 15 my $value = shift;
118  
119 59 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
120 59 my ( $config, @args ) = @_;
121 59 ▶ if ($value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) 11 48 59
122 11 my @replace = $value->(@args);
123 11 splice( @args, 0, scalar(@replace), @replace );
124 }
125 59 return [ $config, @args ];
126 15 };
127 }
128  
129 13 ▶sub mock {
Subroutine mock: called CC=2 SCAR=6.9
Pod documented
130 13 my $self = shift;
131  
132 13 my $self_ref = $self;
133 13 weaken $self_ref; # otherwise reference to $self within wrapped methods
134  
135 13 ▶ while ( my ( $name, $value ) = splice @_, 0, 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 13 13 26
136  
137 13 my $original_fn = \&{$name};
138  
139 13 my $key_sub = _get_key_sub($value);
140  
141 59 ▶ $self->_wrapped->{$name} = wrap $name, pre => sub {
Subroutine __ANON__: called CC=11 SCAR=24.0
142  
143 59 my $mode = $self_ref->mode;
144  
145 59 ▶ return if $mode eq 'passthrough';
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "passthrough" 2 57 59
146  
147 57 my @args = @_; # original arguments that method received
148 57 pop @args; # currently undef, will be the return value
149  
150 57 my $key = $key_sub->( { wantarray => wantarray() }, @args );
151  
152 57 ▶ if ( $mode eq 'playback' or $mode eq 'auto' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "playback" or $mode eq "auto") 34 23 57
Condition100%3/3
$mode eq "playback" or $mode eq "auto"
l!l&&r!l&&!r
28623
MC/DC100%2/2
$mode eq "playback" or $mode eq "auto"
$mode eq "playback" $mode eq "auto"
Truth table100%3/3
A or B
A= $mode eq "playback"B= $mode eq "auto"
ABresult
1X1
011
000
153  
154 34 my $retrieved = eval {
155 34 $self_ref->retrieve(
156 { method => $name,
157 key => $key,
158 input => \@args,
159 }
160 );
161 };
162 34 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 4 30 34
163 4 ▶ croak "Unable to retrieve $name - in $mode mode: $@"
Branch: 1 branch
BranchTrueFalseTotal
unless $mode eq "auto" 1 3 4
164 unless $mode eq 'auto';
165 } else {
166  
167 # add cached value into extra arg,
168 # so original sub will not be called
169 30 $_[-1] = $retrieved->{output};
170 30 return;
171 }
172 }
173  
174 26 ▶ if ( $mode eq 'record' or $mode eq 'auto' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "record" or $mode eq "auto") 26 0 26
Condition66%2/3
$mode eq "record" or $mode eq "auto"
l!l&&r!l&&!r
2330
MC/DC0%0/2
$mode eq "record" or $mode eq "auto"
$mode eq "record" $mode eq "auto"
Truth table66%2/3
A or B
A= $mode eq "record"B= $mode eq "auto"
ABresult
1X1
011
000
175  
176 26 my $result;
177 26 ▶ if (wantarray) {
Branch: 2 branches
BranchTrueFalseTotal
if (wantarray) { } 6 20 26
elsif (defined wantarray) { } 17 3 20
178 6 $result = [ $original_fn->(@args) ];
179 } elsif ( defined wantarray ) {
180 17 $result = $original_fn->(@args);
181 } else {
182 3 $original_fn->(@args);
183 }
184  
185 26 ▶ $self_ref->store(
Branch: 1 branch
BranchTrueFalseTotal
defined wantarray ? : 23 3 26
186 { method => $name,
187 key => $key,
188 input => \@args,
189 defined wantarray()
190 ? ( output => $result )
191 : ( no_output => 1 ),
192 }
193 );
194  
195 26 $_[-1] = $result;
196 26 return;
197 }
198 13 };
199 }
200  
201 13 return $self;
202 }
203  
204 14 1;
205  
206 __END__
↑ Summary MethodFixtures/Storage/File.pm »