Contract.pm

filter: Statementstmt 98.7% 157 / 159 Branchbran 80.3% 53 / 66 Conditioncond 50.0% 27 / 54 MC/DCmcdc 21.8% 7 / 32 Subroutinesub 100% 33 / 33 Podpod 100% 16 / 16 totaltotal 81.3% 293 / 360 CC 56 SCAR 16.0
CC
56
Cov
85%
_set_in_out
12.0
new
8.0
is_one_of
5.2
CRAP 4.9
« Contract/Compiler.pm ↑ Summary Contract/Cache.pm »
1 #-------------------------------------------------------------------
2 #
3 # Sub::Contract - Programming by contract and memoizing in one
4 #
5 # $Id: Contract.pm,v 1.35 2009/06/16 12:23:57 erwan_lemonnier Exp $
6 #
7  
8 package Sub::Contract;
9  
10 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 23 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 23 ▶use Carp qw(croak confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 23 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 23 ▶use Sub::Contract::ArgumentChecks;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 23 ▶use Sub::Contract::Debug qw(debug);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 23 ▶use Sub::Contract::Pool qw(get_contract_pool);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 23 ▶use Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 # Add compiling and memoizing abilities through multiple inheritance, to keep code separate
20 23 ▶use base qw( Exporter
Subroutine BEGIN: called CC=1 SCAR=0.0
21 Sub::Contract::Compiler
22 23 Sub::Contract::Memoizer );
23  
24 23 our @EXPORT = qw();
25 23 our @EXPORT_OK = qw( contract
26 undef_or
27 defined_and
28 is_defined_and
29 is_undefined_or
30 is_not
31 is_one_of
32 is_all_of
33 is_a
34 );
35  
36 23 our $VERSION = '0.12';
37  
38 23 my $pool = Sub::Contract::Pool::get_contract_pool();
39  
40 # the argument list passed to the contractor
41 23 local @Sub::Contract::args;
42 23 local $Sub::Contract::wantarray;
43 23 local @Sub::Contract::results;
44  
45 #---------------------------------------------------------------
46 #
47 # contract - declare a contract
48 #
49  
50 50 ▶sub contract {
Subroutine contract: called CC=3 SCAR=11.0
Pod documented
51 50 ▶ croak "contract() expects only one argument, a subroutine name" if (scalar @_ != 1 || !defined $_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ != 1 or not defined $_[0] 3 47 50
Condition100%3/3
scalar @_ != 1 or not defined $_[0]
l!l&&r!l&&!r
2147
MC/DC100%2/2
scalar @_ != 1 or not defined $_[0]
scalar @_ != 1 not defined $_[0]
Truth table100%3/3
A or not(B)
A= scalar @_ != 1B= not defined $_[0]
ABresult
1X1
011
000
52 47 my $caller = caller;
53 47 return new Sub::Contract($_[0], caller => $caller);
54 }
55  
56 #---------------------------------------------------------------
57 #
58 #
59 # functions to combine constraints
60 #
61 #
62 #---------------------------------------------------------------
63  
64 5 ▶sub is_undefined_or {
Subroutine is_undefined_or: called CC=4 SCAR=15.2
Pod documented
65 5 ▶ croak "is_undefined_or() expects a coderef" if (scalar @_ != 1 || !defined $_[0] || ref $_[0] ne 'CODE');
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ != 1 or not defined $_[0] or ref $_[0] ne "CODE" 0 5 5
Condition33%2/6
scalar @_ != 1 or not defined $_[0] or ref $_[0] ne "CODE"
l!l&&r!l&&!r
005
scalar @_ != 1 or not defined $_[0]
l!l&&r!l&&!r
005
MC/DC0%0/3
scalar @_ != 1 or not defined $_[0] or ref $_[0] ne "CODE"
scalar @_ != 1 not defined $_[0] ref $_[0] ne "CODE"
Truth table25%1/4
A or not(B) or C
A= scalar @_ != 1B= not defined $_[0]C= ref $_[0] ne "CODE"
ABCresult
1XX1
01X1
0011
0000
66 5 my $test = shift;
67 27 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
68 27 ▶ return 1 if (!defined $_[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 8 19 27
69 19 return &$test(@_);
70 5 };
71 }
72  
73 # backward compatibility
74 23 *undef_or = *is_undefined_or;
75  
76 3 ▶sub is_defined_and {
Subroutine is_defined_and: called CC=4 SCAR=15.2
Pod documented
77 3 ▶ croak "is_defined_and() expects a coderef" if (scalar @_ != 1 || !defined $_[0] || ref $_[0] ne 'CODE');
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ != 1 or not defined $_[0] or ref $_[0] ne "CODE" 0 3 3
Condition33%2/6
scalar @_ != 1 or not defined $_[0] or ref $_[0] ne "CODE"
l!l&&r!l&&!r
003
scalar @_ != 1 or not defined $_[0]
l!l&&r!l&&!r
003
MC/DC0%0/3
scalar @_ != 1 or not defined $_[0] or ref $_[0] ne "CODE"
scalar @_ != 1 not defined $_[0] ref $_[0] ne "CODE"
Truth table25%1/4
A or not(B) or C
A= scalar @_ != 1B= not defined $_[0]C= ref $_[0] ne "CODE"
ABCresult
1XX1
01X1
0011
0000
78 3 my $test = shift;
79 19 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
80 19 ▶ return 0 if (!defined $_[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 5 14 19
81 14 return &$test(@_);
82 3 };
83 }
84  
85 # backward compatibility
86 23 *defined_and = *is_defined_and;
87  
88 2 ▶sub is_not {
Subroutine is_not: called CC=4 SCAR=16.4
Pod documented
89 2 ▶ croak "is_not() expects a coderef" if (scalar @_ != 1 || !defined $_[0] || ref $_[0] ne 'CODE');
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ != 1 or not defined $_[0] or ref $_[0] ne "CODE" 0 2 2
Condition33%2/6
scalar @_ != 1 or not defined $_[0] or ref $_[0] ne "CODE"
l!l&&r!l&&!r
002
scalar @_ != 1 or not defined $_[0]
l!l&&r!l&&!r
002
MC/DC0%0/3
scalar @_ != 1 or not defined $_[0] or ref $_[0] ne "CODE"
scalar @_ != 1 not defined $_[0] ref $_[0] ne "CODE"
Truth table25%1/4
A or not(B) or C
A= scalar @_ != 1B= not defined $_[0]C= ref $_[0] ne "CODE"
ABCresult
1XX1
01X1
0011
0000
90 2 my $test = shift;
91 7 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
92 7 return !&$test(@_);
93 2 };
94 }
95  
96 2 ▶sub is_one_of {
Subroutine is_one_of: called CC=5 SCAR=16.6
Pod documented
97 2 my @tests = @_;
98 2 ▶ croak "is_one_of() expects at least two coderefs" if (scalar @tests < 2);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @tests < 2 0 2 2
99 2 foreach my $test (@tests) {
100 5 ▶ croak "is_one_of() expects only coderefs" if (!defined $test || ref $test ne 'CODE');
Branch: 1 branch
BranchTrueFalseTotal
if not defined $test or ref $test ne "CODE" 0 5 5
Condition33%1/3
not defined $test or ref $test ne "CODE"
l!l&&r!l&&!r
005
MC/DC0%0/2
not defined $test or ref $test ne "CODE"
not defined $test ref $test ne "CODE"
Truth table33%1/3
not(A) or B
A= not defined $testB= ref $test ne "CODE"
ABresult
1X1
011
000
101 }
102  
103 8 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
104 8 my @args = @_;
105 8 foreach my $test (@tests) {
106 14 ▶ if (&$test(@args)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$test(@args)) 6 8 14
107 6 return 1;
108 }
109 }
110 2 return 0;
111 2 };
112 }
113  
114 1 ▶sub is_all_of {
Subroutine is_all_of: called CC=5 SCAR=16.6
Pod documented
115 1 my @tests = @_;
116 1 ▶ croak "is_all_of() expects at least two coderefs" if (scalar @tests < 2);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @tests < 2 0 1 1
117 1 foreach my $test (@tests) {
118 2 ▶ croak "is_all_of() expects only coderefs" if (!defined $test || ref $test ne 'CODE');
Branch: 1 branch
BranchTrueFalseTotal
if not defined $test or ref $test ne "CODE" 0 2 2
Condition33%1/3
not defined $test or ref $test ne "CODE"
l!l&&r!l&&!r
002
MC/DC0%0/2
not defined $test or ref $test ne "CODE"
not defined $test ref $test ne "CODE"
Truth table33%1/3
not(A) or B
A= not defined $testB= ref $test ne "CODE"
ABresult
1X1
011
000
119 }
120  
121 2 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
122 2 my @args = @_;
123 2 foreach my $test (@tests) {
124 4 ▶ if (!&$test(@args)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&$test(@args)) 1 3 4
125 1 return 0;
126 }
127 }
128 1 return 1;
129 1 };
130 }
131  
132 3 ▶sub is_a {
Subroutine is_a: called CC=4 SCAR=14.8
Pod documented
133 3 ▶ croak "is_a() expects a package name" if (scalar @_ != 1 || !defined $_[0] || ref $_[0] ne '');
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ != 1 or not defined $_[0] or ref $_[0] ne "" 0 3 3
Condition33%2/6
scalar @_ != 1 or not defined $_[0] or ref $_[0] ne ""
l!l&&r!l&&!r
003
scalar @_ != 1 or not defined $_[0]
l!l&&r!l&&!r
003
MC/DC0%0/3
scalar @_ != 1 or not defined $_[0] or ref $_[0] ne ""
scalar @_ != 1 not defined $_[0] ref $_[0] ne ""
Truth table25%1/4
A or not(B) or C
A= scalar @_ != 1B= not defined $_[0]C= ref $_[0] ne ""
ABCresult
1XX1
01X1
0011
0000
134 3 my $type = shift;
135 11 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
136 11 ▶ return 0 if (!defined $_[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 1 10 11
137 10 ▶ return (ref $_[0] eq $type) ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq $type ? : 5 5 10
138 3 };
139 }
140  
141 ################################################################
142 #
143 #
144 # Object API
145 #
146 #
147 ################################################################
148  
149 #---------------------------------------------------------------
150 #
151 # new - instantiate a new subroutine contract
152 #
153  
154 56 ▶sub new {
Subroutine new: called CC=8 SCAR=20.8
Pod documented
155 56 my ($class,$fullname,%args) = @_;
156 56 ▶ $class = ref $class || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
0560
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
157 56 ▶ my $caller = delete $args{caller} || caller();
Condition66%2/3
delete $args{'caller'} || caller()
l!l&&r!l&&!r
5150
MC/DC0%0/2
delete $args{'caller'} || caller()
delete $args{'caller'} caller()
Truth table66%2/3
A || B
A= delete $args{'caller'}B= caller()
ABresult
1X1
011
000
158  
159 56 ▶ croak "new() expects a subroutine name as first argument" if (!defined $fullname);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fullname 1 55 56
160 55 ▶ croak "new() got unknown arguments: ".Dumper(%args) if (keys %args != 0);
Branch: 1 branch
BranchTrueFalseTotal
if keys %args != 0 1 54 55
161  
162 # identify the subroutine to contract and make sure it exists
163 54 my $contractor_cref;
164 my $contractor;
165  
166 54 ▶ if ($fullname !~ /::/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $fullname =~ /::/) { } 46 8 54
167 46 $contractor_cref = *{ qualify_to_ref($fullname,$caller) }{CODE};
168 46 $contractor = qualify($fullname,$caller);
169 } else {
170 8 $contractor_cref = *{ qualify_to_ref($fullname) }{CODE};
171 8 $contractor = qualify($fullname);
172 }
173  
174 54 ▶ if (!defined $contractor_cref) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $contractor_cref) 4 50 54
175 4 croak "Can't find subroutine named '".$contractor."'";
176 }
177  
178 # create instance of contract
179 50 my $self = bless({}, $class);
180 50 $self->{is_enabled} = 0; # 1 if contract is enabled
181 50 $self->{is_memoized} = 0; # TODO: needed?
182 50 $self->{contractor} = $contractor; # The fully qualified name of the contracted subroutine
183 50 $self->{contractor_cref} = $contractor_cref; # A code reference to the contracted subroutine
184  
185 50 $self->reset;
186  
187 # add self to the contract pool (if not already in)
188 50 ▶ croak "trying to contract function [$contractor] twice"
Branch: 1 branch
BranchTrueFalseTotal
if $pool->has_contract($contractor) 1 49 50
189 if ($pool->has_contract($contractor));
190  
191 49 $pool->_add_contract($self);
192  
193 49 return $self;
194 }
195  
196 #---------------------------------------------------------------
197 #
198 # reset - reset all constraints in a contract
199 #
200  
201 57 ▶sub reset {
Subroutine reset: called CC=2 SCAR=7.2
Pod documented
202 57 my $self = shift;
203 57 $self->{in} = undef; # An array of coderefs checking respective input arguments
204 57 $self->{out} = undef; # An array of coderefs checking respective return arguments
205 57 $self->{pre} = undef; # Coderef checking pre conditions
206 57 $self->{post} = undef; # Coderef checking post conditions
207 57 $self->{invariant} = undef; # Coderef checking an invariant condition
208 57 ▶ if (exists $self->{cache}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"cache"}) 0 57 57
209 0 $self->{cache}->clear;
210 0 delete $self->{cache};
211 }
212 57 return $self;
213 }
214  
215 #---------------------------------------------------------------
216 #
217 # in, out - declare conditions for each of the subroutine's in- and out-arguments
218 #
219  
220 107 ▶sub _set_in_out {
Subroutine _set_in_out: called CC=12 SCAR=24.8
221 107 my ($type,$self,@checks) = @_;
222 107 local $Carp::CarpLevel = 2;
223 107 my $validator = new Sub::Contract::ArgumentChecks($type);
224  
225 107 my $pos = 0;
226  
227 # check arguments passed in list-style
228 107 ▶ while (@checks) {
Branch: 1 branch
BranchTrueFalseTotal
if (@checks) 181 39 220
229 181 my $check = shift @checks;
230  
231 181 ▶ if (!defined $check || ref $check eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $check or ref $check eq 'CODE') { } 113 68 181
elsif (ref $check eq '') { } 60 8 68
Condition100%3/3
not defined $check or ref $check eq "CODE"
l!l&&r!l&&!r
367768
MC/DC100%2/2
not defined $check or ref $check eq "CODE"
not defined $check ref $check eq "CODE"
Truth table100%3/3
not(A) or B
A= not defined $checkB= ref $check eq "CODE"
ABresult
1X1
011
000
232 # ok
233 113 $validator->add_list_check($check);
234 } elsif (ref $check eq '') {
235 # this is a hash key. we expect hash syntax from there on
236 60 unshift @checks, $check;
237 60 last;
238 } else {
239 8 croak "invalid contract definition: argument at position $pos in $type() should be undef or a coderef or a string";
240 }
241 113 $pos++;
242 }
243  
244 # @checks should be either empty or describe hash checks (sequence of string => coderef)
245 99 ▶ if (scalar @checks % 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @checks % 2) 12 87 99
246 12 croak "invalid contract definition: odd number of arguments from position $pos in $type(), can't ".
247 "constrain hash-style passed arguments";
248 }
249  
250 # check arguments passed in hash-style
251 87 my %known_keys;
252 87 ▶ while (@checks) {
Branch: 1 branch
BranchTrueFalseTotal
if (@checks) 88 71 159
253 88 my $key = shift @checks;
254 88 my $check = shift @checks;
255  
256 88 ▶ if (defined $key && ref $key eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key and ref $key eq '') { } 82 6 88
Condition66%2/3
defined $key and ref $key eq ""
!ll&&!rl&&r
0682
MC/DC50%1/2
defined $key and ref $key eq ""
defined $key ref $key eq ""
Truth table66%2/3
A and B
A= defined $keyB= ref $key eq ""
ABresult
0X0
100
111
257 # is this key defined more than once?
258 82 ▶ if (exists $known_keys{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $known_keys{$key}) 2 80 82
259 2 croak "invalid contract definition: constraining argument \'$key\' twice in $type()";
260 }
261 80 $known_keys{$key} = 1;
262  
263 # ok with key. verify $check
264 80 ▶ if (!defined $check || ref $check eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $check or ref $check eq 'CODE') { } 72 8 80
Condition100%3/3
not defined $check or ref $check eq "CODE"
l!l&&r!l&&!r
22508
MC/DC100%2/2
not defined $check or ref $check eq "CODE"
not defined $check ref $check eq "CODE"
Truth table100%3/3
not(A) or B
A= not defined $checkB= ref $check eq "CODE"
ABresult
1X1
011
000
265 # ok
266 72 $validator->add_hash_check($key,$check);
267 } else {
268 8 croak "invalid contract definition: check for \'$key\' should be undef or a coderef in $type()";
269 }
270 } else {
271 6 croak "invalid contract definition: argument at position $pos should be a string in $type()";
272 }
273 72 $pos += 2;
274 }
275  
276 # everything ok!
277 71 $self->{$type} = $validator;
278 71 return $self;
279 }
280  
281 58 ▶sub in { return _set_in_out('in',@_); }
Subroutine in: called CC=1 SCAR=0.0
Pod documented
282 49 ▶sub out { return _set_in_out('out',@_); }
Subroutine out: called CC=1 SCAR=0.0
Pod documented
283  
284 #---------------------------------------------------------------
285 #
286 # pre, post - declare pre and post conditions on subroutine
287 #
288  
289 8 ▶sub _set_pre_post {
Subroutine _set_pre_post: called CC=4 SCAR=14.3
290 8 my ($type,$self,$subref) = @_;
291 8 local $Carp::CarpLevel = 2;
292  
293 8 ▶ croak "the method $type() expects exactly one argument"
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ != 3 0 8 8
294 if (scalar @_ != 3);
295 8 ▶ croak "the method $type() expects a code reference as argument"
Branch: 1 branch
BranchTrueFalseTotal
if defined $subref and ref $subref ne "CODE" 0 8 8
Condition66%2/3
defined $subref and ref $subref ne "CODE"
!ll&&!rl&&r
170
MC/DC0%0/2
defined $subref and ref $subref ne "CODE"
defined $subref ref $subref ne "CODE"
Truth table66%2/3
A and B
A= defined $subrefB= ref $subref ne "CODE"
ABresult
0X0
100
111
296 if (defined $subref && ref $subref ne 'CODE');
297  
298 8 $self->{$type} = $subref;
299  
300 8 return $self;
301 }
302  
303 4 ▶sub pre { return _set_pre_post('pre',@_); }
Subroutine pre: called CC=1 SCAR=0.0
Pod documented
304 4 ▶sub post { return _set_pre_post('post',@_); }
Subroutine post: called CC=1 SCAR=0.0
Pod documented
305  
306 #---------------------------------------------------------------
307 #
308 # invariant - adds an invariant condition
309 #
310  
311 8 ▶sub invariant {
Subroutine invariant: called CC=4 SCAR=15.2
Pod documented
312 8 my ($self,$subref) = @_;
313  
314 8 ▶ croak "the method invariant() expects exactly one argument"
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ != 2 0 8 8
315 if (scalar @_ != 2);
316 8 ▶ croak "the method invariant() expects a code reference as argument"
Branch: 1 branch
BranchTrueFalseTotal
if defined $subref and ref $subref ne "CODE" 0 8 8
Condition33%1/3
defined $subref and ref $subref ne "CODE"
!ll&&!rl&&r
080
MC/DC0%0/2
defined $subref and ref $subref ne "CODE"
defined $subref ref $subref ne "CODE"
Truth table33%1/3
A and B
A= defined $subrefB= ref $subref ne "CODE"
ABresult
0X0
100
111
317 if (defined $subref && ref $subref ne 'CODE');
318  
319 8 $self->{invariant} = $subref;
320  
321 8 return $self;
322 }
323  
324 #---------------------------------------------------------------
325 #
326 # contractor - returns the contractor subroutine's fully qualified name
327 #
328  
329 451 ▶sub contractor {
Subroutine contractor: called CC=1 SCAR=0.0
Pod documented
330 451 return $_[0]->{contractor};
331 }
332  
333 #---------------------------------------------------------------
334 #
335 # contractor_cref - return a code ref to the contractor subroutine
336 #
337  
338 56 ▶sub contractor_cref {
Subroutine contractor_cref: called CC=1 SCAR=0.0
Pod documented
339 56 return $_[0]->{contractor_cref};
340 }
341  
342  
343 # TODO: implement return?
344  
345 23 1;
346  
347 __END__
« Contract/Compiler.pm ↑ Summary Contract/Cache.pm »