blib/lib/Class/DbC.pm

filter: Statementstmt 96.6% 174 / 180 Branchbran 86.6% 52 / 60 Conditioncond 70.5% 12 / 17 MC/DCmcdc 50.0% 5 / 10 Subroutinesub 100% 34 / 34 Podpod 0.0% 0 / 1 totaltotal 91.7% 277 / 302 CC 45 SCAR 17.6
CC
45
Cov
93%
_govern
14.0
__ANON__
10.0
__ANON__
7.1
CRAP 5.8
↑ Summary
1 package Class::DbC;
2  
3 14 our $VERSION = '0.000002';
4 14 $VERSION = eval $VERSION;
5  
6 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 14 ▶use Class::Method::Modifiers qw(install_modifier);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 14 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 14 ▶use Module::Runtime qw(require_module);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 14 ▶use Params::Validate qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 14 ▶use Storable qw( dclone );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 14 my %Spec_for;
14 my %Contract_pkg_for;
15  
16 14 my %Contract_validation_spec = (
17 type => HASHREF,
18 optional => 1,
19 );
20  
21 15 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
22 15 strict->import();
23 15 my $class = shift;
24 15 my %arg = validate(@_, {
25 interface => \%Contract_validation_spec,
26 invariant => \%Contract_validation_spec,
27 extends => { type => SCALAR, optional => 1 },
28 clone_with => { type => CODEREF, optional => 1 },
29 constructor_name => { type => SCALAR, default => 'new' },
30 });
31  
32 15 my $caller_pkg = (caller)[0];
33 15 $Spec_for{ $caller_pkg } = \%arg;
34 15 _handle_extentions($caller_pkg, $arg{extends});
35 15 _add_governor($caller_pkg);
36 }
37  
38 1 ▶sub merge {
Subroutine merge: called CC=5 SCAR=17.2
Pod undocumented
39 1 my ($h1, $h2) = @_;
40  
41 1 foreach my $k (keys %{ $h2 }) {
42 2 ▶ if (exists $h1->{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $h1->{$k}) { } 1 1 2
43 1 ▶ if ( ref $h1->{$k} eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
if (ref $h1->{$k} eq "HASH" and ref $h2->{$k} eq "HASH") 0 1 1
Condition33%1/3
ref $h1->{$k} eq "HASH" and ref $h2->{$k} eq "HASH"
!ll&&!rl&&r
100
MC/DC0%0/2
ref $h1->{$k} eq "HASH" and ref $h2->{$k} eq "HASH"
ref $h1->{$k} eq "HASH" ref $h2->{$k} eq "HASH"
Truth table33%1/3
A and B
A= ref $h1->{$k} eq "HASH"B= ref $h2->{$k} eq "HASH"
ABresult
0X0
100
111
44 && ref $h2->{$k} eq 'HASH'
45 ) {
46 0 merge($h1->{$k}, $h2->{$k});
47 }
48 }
49 else {
50 1 $h1->{$k} = $h2->{$k};
51 }
52 }
53 }
54  
55 15 ▶sub _handle_extentions {
Subroutine _handle_extentions: called CC=2 SCAR=6.9
56 15 my ($pkg, $super) = @_;
57  
58 15 ▶ return unless $super;
Branch: 1 branch
BranchTrueFalseTotal
unless $super 14 1 15
59  
60 1 require_module($super);
61 1 merge($Spec_for{$pkg}, $Spec_for{$super});
62 }
63  
64 15 ▶sub _add_governor {
Subroutine _add_governor: called CC=1 SCAR=0.0
65 15 my ($pkg) = @_;
66  
67 14 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
68 15 *{"${pkg}::govern"} = \&_govern;
69 }
70  
71 18 ▶sub _govern {
Subroutine _govern: called CC=14 SCAR=26.4
72 18 my $class = shift;
73 18 my ($pkg, $opt) = validate_pos(@_,
74 { type => SCALAR },
75 { type => HASHREF, default => { all => 1 } },
76 );
77 18 _validate_govern_options(%$opt);
78
79 18 ▶ if ($opt->{all}
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"all"} or $opt->{"emulate"} and scalar keys %$opt == 1) 12 6 18
Condition100%6/6
$opt->{"all"} or $opt->{"emulate"} and scalar keys %$opt == 1
l!l&&r!l&&!r
666
$opt->{"emulate"} and scalar keys %$opt == 1
!ll&&!rl&&r
156
MC/DC100%3/3
$opt->{"all"} or $opt->{"emulate"} and scalar keys %$opt == 1
$opt->{"all"} $opt->{"emulate"} scalar keys %$opt == 1
Truth table100%4/4
A or B and C
A= $opt->{"all"}B= $opt->{"emulate"}C= scalar keys %$opt == 1
ABCresult
1XX1
0111
00X0
0100
80 || ($opt->{emulate} && scalar keys %$opt == 1 )) {
81 12 $opt->{$_} = 1 for qw/pre post invariant/;
82 }
83  
84 18 my $interface_hash = $Spec_for{$class}{interface};
85 18 ▶ scalar keys %$interface_hash > 0
Branch: 1 branch
BranchTrueFalseTotal
scalar keys %$interface_hash > 0 or confess("Contract $class has no specified methods") 18 0 18
86 or confess "Contract $class has no specified methods";
87  
88 18 my $invariant_hash = $Spec_for{$class}{invariant};
89  
90 18 my $contract_pkg_prefix = _contract_pkg_prefix($class, $pkg);
91  
92 18 my $target_pkg = $pkg;
93 18 my $emulated = $pkg;
94  
95 18 ▶ if ($opt->{emulate}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"emulate"}) 11 7 18
96 11 my @types = grep { $opt->{$_} } qw[invariant post pre];
97 11 my $key = join '_', @types;
98  
99 11 $Contract_pkg_for{$class}{$pkg}{$key} = "${contract_pkg_prefix}$key";
100 11 ($emulated, $target_pkg) = _emulate($class, $pkg, $key);
101 }
102 18 foreach my $name (keys %{ $interface_hash }) {
103 126 ▶ $pkg->can($name)
Branch: 1 branch
BranchTrueFalseTotal
$pkg->can($name) or confess("Class $pkg does not have a '${name}' method, which is required by $class") 126 0 126
104 or confess "Class $pkg does not have a '$name' method, which is required by $class";
105  
106 126 ▶ if ($opt->{pre}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"pre"}) 98 28 126
107 98 my $contract = $interface_hash->{$name};
108 98 _validate_contract_definition(%$contract);
109 98 _add_pre_conditions($class, $target_pkg, $name, $contract->{precond});
110 }
111 126 ▶ if ($opt->{post}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"post"}) 84 42 126
112 84 my $contract = $interface_hash->{$name};
113 84 _validate_contract_definition(%$contract);
114 84 _add_post_conditions($class, $target_pkg, $name, $contract->{postcond});
115 }
116 126 ▶ if ($opt->{invariant} && %$invariant_hash) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"invariant"} and %$invariant_hash) 105 21 126
Condition66%2/3
$opt->{"invariant"} and %$invariant_hash
!ll&&!rl&&r
210105
MC/DC50%1/2
$opt->{"invariant"} and %$invariant_hash
$opt->{"invariant"} %$invariant_hash
Truth table66%2/3
A and B
A= $opt->{"invariant"}B= %$invariant_hash
ABresult
0X0
100
111
117 105 _add_invariants($class, $target_pkg, $name, $invariant_hash, $emulated);
118 }
119 }
120 18 ▶ if ($opt->{emulate}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"emulate"}) 11 7 18
121 11 return $emulated;
122 }
123 }
124  
125 182 ▶sub _validate_contract_definition {
Subroutine _validate_contract_definition: called CC=1 SCAR=0.0
126 182 validate(@_, {
127 precond => \%Contract_validation_spec,
128 postcond => \%Contract_validation_spec,
129 });
130 }
131  
132 29 ▶sub _contract_pkg_prefix {
Subroutine _contract_pkg_prefix: called CC=1 SCAR=0.0
133 29 my ($class, $pkg) = @_;
134  
135 29 sprintf '%s_%s_', $pkg, $class;
136 }
137  
138 98 ▶sub _add_pre_conditions {
Subroutine _add_pre_conditions: called CC=2 SCAR=6.9
139 98 my ($class, $pkg, $name, $pre_cond_hash) = @_;
140  
141 98 ▶ return unless $pre_cond_hash;
Branch: 1 branch
BranchTrueFalseTotal
unless $pre_cond_hash 70 28 98
142  
143 18 ▶ my $guard = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
144 18 foreach my $desc (keys %{ $pre_cond_hash }) {
145 18 my $sub = $pre_cond_hash->{$desc};
146 18 ▶ ref $sub eq 'CODE'
Branch: 1 branch
BranchTrueFalseTotal
ref $sub eq "CODE" or confess("precondition of $class, '${desc}' on '${name}' is not a code ref") 18 0 18
147 or confess "precondition of $class, '$desc' on '$name' is not a code ref";
148 18 ▶ $sub->(@_)
Branch: 1 branch
BranchTrueFalseTotal
&$sub(@_) or confess("Precondition '${desc}' on '${name}', mandated by $class is not satisfied") 16 2 18
149 or confess "Precondition '$desc' on '$name', mandated by $class is not satisfied";
150 }
151 28 };
152 28 install_modifier($pkg, 'before', $name, $guard);
153 }
154  
155 84 ▶sub _add_post_conditions {
Subroutine _add_post_conditions: called CC=3 SCAR=11.0
156 84 my ($class, $pkg, $name, $post_cond_hash) = @_;
157
158 84 ▶ return unless $post_cond_hash;
Branch: 1 branch
BranchTrueFalseTotal
unless $post_cond_hash 48 36 84
159  
160 36 ▶ my $cloner = $Spec_for{$class}{clone_with} || \&dclone;
Condition50%1/2
$Spec_for{$class}{'clone_with'} || \&Class::DbC::dclone
l!l
036
MC/DC0%0/1
$Spec_for{$class}{'clone_with'} || \&Class::DbC::dclone
$Spec_for{$class}{'clone_with'}
Truth table50%1/2
A
A= $Spec_for{$class}{'clone_with'}
Aresult
11
00
161  
162 44 ▶ my $guard = sub {
Subroutine __ANON__: called CC=10 SCAR=23.0
163 44 my $orig = shift;
164 44 my $self = shift;
165  
166 44 my @old;
167 44 my @invocant = ($self);
168  
169 44 ▶ my $type = ref $self ? 'object' : 'class';
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 34 10 44
170 44 ▶ if ($type eq 'object') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "object") 34 10 44
171 34 @old = ( $cloner->($self) );
172 }
173 44 my $results = [$orig->($self, @_)];
174 44 my $results_to_check = $results;
175  
176 44 ▶ if ($type eq 'class' && $name eq $Spec_for{$class}{constructor_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "class" and $name eq $Spec_for{$class}{"constructor_name"}) 10 34 44
Condition66%2/3
$type eq "class" and $name eq $Spec_for{$class}{"constructor_name"}
!ll&&!rl&&r
34010
MC/DC50%1/2
$type eq "class" and $name eq $Spec_for{$class}{"constructor_name"}
$type eq "class" $name eq $Spec_for{$class}{"constructor_name"}
Truth table66%2/3
A and B
A= $type eq "class"B= $name eq $Spec_for{$class}{"constructor_name"}
ABresult
0X0
100
111
177 10 $results_to_check = $results->[0];
178 10 @invocant = ();
179 }
180  
181 44 foreach my $desc (keys %{ $post_cond_hash }) {
182 73 my $sub = $post_cond_hash->{$desc};
183 73 ▶ ref $sub eq 'CODE'
Branch: 1 branch
BranchTrueFalseTotal
ref $sub eq "CODE" or confess("postcondition of $class, '${desc}' on '${name}' is not a code ref") 73 0 73
184 or confess "postcondition of $class, '$desc' on '$name' is not a code ref";
185  
186 73 ▶ $sub->(@invocant, @old, $results_to_check, @_)
Branch: 1 branch
BranchTrueFalseTotal
&$sub(@invocant, @old, $results_to_check, @_) or confess("Method '$main::pkg::${name}' failed postcondition '${desc}' mandated by $class") 67 5 72
187 or confess "Method '$pkg::$name' failed postcondition '$desc' mandated by $class";
188 }
189 38 ▶ return unless defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 29 9 38
190 9 ▶ return wantarray ? @$results : $results->[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 7 9
191 36 };
192 36 install_modifier($pkg, 'around', $name, $guard);
193 }
194  
195 105 ▶sub _add_invariants {
Subroutine _add_invariants: called CC=3 SCAR=11.0
196 105 my ($class, $pkg, $name, $invariant_hash, $emulated) = @_;
197
198 798 ▶ my $guard = sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
199 # skip methods called by the invariant
200 798 ▶ return if (caller 1)[0] eq $class;
Branch: 1 branch
BranchTrueFalseTotal
if (caller 1)[0] eq $class 414 384 798
201 384 ▶ return if (caller 2)[0] eq $class;
Branch: 1 branch
BranchTrueFalseTotal
if (caller 2)[0] eq $class 272 112 384
202  
203 112 my $self = shift;
204 112 ▶ return unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 112 112
205  
206 112 foreach my $desc (keys %{ $invariant_hash }) {
207 112 my $sub = $invariant_hash->{$desc};
208 112 ▶ ref $sub eq 'CODE'
Branch: 1 branch
BranchTrueFalseTotal
ref $sub eq "CODE" or confess("invariant of $class, '${desc}' is not a code ref") 112 0 112
209 or confess "invariant of $class, '$desc' is not a code ref";
210 112 ▶ $sub->($self)
Branch: 1 branch
BranchTrueFalseTotal
&$sub($self) or confess("Invariant '${desc}' mandated by $class has been violated") 106 6 112
211 or confess "Invariant '$desc' mandated by $class has been violated";
212 }
213 105 };
214  
215 105 ▶ if ( $name eq $Spec_for{$class}{constructor_name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq $Spec_for{$class}{'constructor_name'}) { } 15 90 105
216 13 ▶ my $around = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
217 13 my $orig = shift;
218 13 my $class = shift;
219 13 my $obj = $orig->($class, @_);
220 10 $guard->($obj);
221 7 return $obj;
222 15 };
223 15 install_modifier($pkg, 'around', $name, $around);
224 }
225 else {
226 90 foreach my $type ( qw[before after] ) {
227 180 install_modifier($pkg, $type, $name, $guard);
228 }
229 }
230 }
231  
232 11 ▶sub _emulate {
Subroutine _emulate: called CC=1 SCAR=0.0
233 11 my ($class, $pkg, $key) = @_;
234  
235 11 my $contract_pkg = $Contract_pkg_for{$class}{$pkg}{$key};
236 11 _add_super($pkg, $contract_pkg);
237  
238 11 my $emulated = sprintf '%semulated', _contract_pkg_prefix($class, $pkg);
239 11 _setup_forwards($class, $pkg, $emulated, $contract_pkg);
240  
241 11 return ($emulated, $contract_pkg);
242 }
243  
244 11 ▶sub _add_super {
Subroutine _add_super: called CC=2 SCAR=8.1
245 11 my ($super, $pkg) = @_;
246  
247 14 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
248  
249 11 ▶ if ( @{"${pkg}::ISA"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{"${pkg}::ISA";}) { } 0 11 11
250 0 my $between = shift @{"${pkg}::ISA"};
251 0 unshift @{"${pkg}::ISA"}, $super;
252 0 _add_super($between, $super);
253 }
254 else {
255 11 unshift @{"${pkg}::ISA"}, $super;
256 }
257 }
258  
259 11 ▶sub _setup_forwards {
Subroutine _setup_forwards: called CC=3 SCAR=11.0
260 11 my ($class, $orig_pkg, $from_pkg, $to_pkg) = @_;
261  
262 11 my $version;
263 {
264 14 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
265 11 ${"${from_pkg}::Target"} = $to_pkg;
266 11 $version = ${"${from_pkg}::VERSION"};
267 }
268  
269 11 ▶ if ( ! $version ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $version) 7 4 11
270  
271 7 my $interface_hash = $Spec_for{$class}{interface};
272 7 my @code = (
273 "package $from_pkg;",
274 "our \$VERSION = 0.000002;",
275 "our \@ISA = ('$orig_pkg');",
276 "our \$Target;",
277 );
278  
279 7 foreach my $name (keys %{ $interface_hash }) {
280  
281 49 push @code, qq[
282 sub $name {
283 \$Target->can('$name')->(\@_);
284 }
285 ];
286 }
287 7 ▶ eval join "\n", @code, '1;';
Subroutine head: called CC=1 SCAR=0.0
Subroutine max_size: called CC=1 SCAR=0.0
Subroutine new: called CC=1 SCAR=0.0
Subroutine pop: called CC=1 SCAR=0.0
Subroutine push: called CC=1 SCAR=0.0
Subroutine size: called CC=1 SCAR=0.0
Subroutine tail: called CC=1 SCAR=0.0
288 }
289 }
290  
291 18 ▶sub _validate_govern_options {
Subroutine _validate_govern_options: called CC=1 SCAR=0.0
292 18 validate(@_, {
293 all => { type => BOOLEAN, optional => 1 },
294 pre => { type => BOOLEAN, optional => 1 },
295 post => { type => BOOLEAN, optional => 1 },
296 invariant => { type => BOOLEAN, optional => 1 },
297 emulate => { type => BOOLEAN, optional => 1 },
298 });
299 }
300  
301 14 1;
302  
303 __END__
↑ Summary