Contract.pm

filter: Statementstmt 96.2% 76 / 79 Branchbran 60.7% 17 / 28 Conditioncond 53.3% 8 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 100% 20 / 20 Podpod 0.0% 0 / 3 totaltotal 78.0% 121 / 155 CC 20 SCAR 10.3
CC
20
Cov
83%
__ANON__
4.7
_validate_output
4.6
__ANON__
4.1
CRAP 2.8
↑ Summary Contract/Exception.pm »
1 package LIVR::Contract;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use Validator::LIVR;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Class::Method::Modifiers qw/install_modifier/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use LIVR::Contract::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 our @EXPORT = ( 'contract' );
15 1 our @CARP_NOT = (__PACKAGE__);
16  
17 1 our $VERSION = '0.04';
18  
19 2 ▶sub contract {
Subroutine contract: called CC=2 SCAR=7.1
Pod undocumented
20 2 my ( $subname, %args ) = @_;
21 2 ▶ croak "Subname is required" unless $subname;
Branch: 1 branch
BranchTrueFalseTotal
unless $subname 0 2 2
22  
23 return __PACKAGE__->new(
24 'subname' => $subname,
25 'package' => scalar( caller ),
26 'ensures' => $args{ensures},
27 'requires' => $args{requires},
28 2 )->enable();
29 }
30  
31 2 ▶sub new {
Subroutine new: called CC=3 SCAR=11.3
Pod undocumented
32 2 my ( $class, %args ) = @_;
33  
34 2 ▶ croak '"subname" is required' unless $args{subname};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"subname"} 0 2 2
35 2 ▶ croak '"package" is required' unless $args{package};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"package"} 0 2 2
36  
37 my $self = bless {
38 subname => $args{subname},
39 package => $args{package},
40 ensures => $args{ensures},
41 requires => $args{requires},
42 input_preparator => $args{input_preparator},
43 output_preparator => $args{output_preparator},
44 on_fail => $args{on_fail},
45 2 input_validator => undef,
46 output_validator => undef,
47 }, $class;
48  
49 2 return $self;
50 }
51  
52 2 ▶sub enable {
Subroutine enable: called CC=1 SCAR=0.3
Pod undocumented
53 2 my $self = shift;
54  
55 install_modifier(
56 $self->{package},
57 'around',
58 $self->{subname},
59 3 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=15.5
60 3 my $orig = shift;
61  
62 3 my $input = $self->_get_input_preparator->( @_ );
63 3 $self->_validate_input( $input );
64  
65 1 my $is_wantarray = wantarray;
66 1 ▶ my $output = $is_wantarray ? [ $orig->( @_ ) ] : $orig->( @_ );
Branch: 1 branch
BranchTrueFalseTotal
$is_wantarray ? : 0 1 1
67  
68 1 ▶ my $prepared_output = $self->_get_output_preparator->( $is_wantarray ? @$output : $output );
Branch: 1 branch
BranchTrueFalseTotal
$is_wantarray ? : 0 1 1
69 1 $self->_validate_output( $prepared_output );
70  
71 0 ▶ return $is_wantarray ? @$output : $output;
Branch: 1 branch
BranchTrueFalseTotal
$is_wantarray ? : 0 0 0
72 }
73 2 );
74 }
75  
76 3 ▶sub _validate_input {
Subroutine _validate_input: called CC=4 SCAR=14.0
77 3 my ( $self, $input ) = @_;
78 3 ▶ return unless $self->{requires};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"requires"} 0 3 3
79  
80 3 ▶ my $validator = $self->{input_validator} ||= Validator::LIVR->new( $self->{requires} )->prepare();
Condition66%2/3
$self->{"input_validator"} ||= "Validator::LIVR"->new($self->{"requires"})->prepare
l!l&&r!l&&!r
120
MC/DC0%0/2
$self->{"input_validator"} ||= "Validator::LIVR"->new($self->{"requires"})->prepare
$self->{"input_validator"} "Validator::LIVR"->new($self->{"requires"})->prepare
Truth table66%2/3
A ||= B
A= $self->{"input_validator"}B= "Validator::LIVR"->new($self->{"requires"})->prepare
ABresult
1X1
011
000
81  
82 3 ▶ if ( !$validator->validate( $input ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $validator->validate($input)) 2 1 3
83 2 $self->_get_on_fail->( 'input', $self->{package}, $self->{subname}, $validator->get_errors() );
84 }
85 }
86  
87 1 ▶sub _validate_output {
Subroutine _validate_output: called CC=4 SCAR=15.2
88 1 my ( $self, $output ) = @_;
89 1 ▶ return unless $self->{ensures};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"ensures"} 0 1 1
90  
91 1 ▶ my $validator = $self->{output_validator} ||= Validator::LIVR->new( $self->{ensures} )->prepare();
Condition33%1/3
$self->{"output_validator"} ||= "Validator::LIVR"->new($self->{"ensures"})->prepare
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->{"output_validator"} ||= "Validator::LIVR"->new($self->{"ensures"})->prepare
$self->{"output_validator"} "Validator::LIVR"->new($self->{"ensures"})->prepare
Truth table33%1/3
A ||= B
A= $self->{"output_validator"}B= "Validator::LIVR"->new($self->{"ensures"})->prepare
ABresult
1X1
011
000
92  
93 1 ▶ if ( !$validator->validate( $output ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $validator->validate($output)) 1 0 1
94 1 $self->_get_on_fail->( 'output', $self->{package}, $self->{subname}, $validator->get_errors() );
95 }
96 }
97  
98 3 ▶sub _get_input_preparator {
Subroutine _get_input_preparator: called CC=2 SCAR=7.1
99 3 my $self = shift;
100  
101 3 ▶ return $self->{input_preparator} ||= sub {
Subroutine __ANON__: called CC=4 SCAR=14.1
102 3 my %numbered;
103 3 ▶ for (my $i=0; $i< @_; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @_) 10 3 13
104 10 $numbered{$i} = $_[$i];
105 }
106  
107 3 my %named;
108 3 ▶ foreach ( my $i = @_ % 2; $i < @_; $i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @_) 4 3 7
109 4 ▶ if ( ref $_[$i] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[$i]) { } 0 4 4
110 0 undef %named;
111 0 last;
112 } else {
113 4 $named{ $_[$i] } = $_[$i+1];
114 }
115 }
116  
117 3 return {%numbered, %named};
118 3 ▶ };
Condition66%2/3
$self->{"input_preparator"} ||= sub { my %numbered; for (my $i = 0; $i < @_; ++$i) { $numbered{$i} = $_[$i]; } ; my %named; for (my $i = @_ % 2; $i < @_; $i += 2) { if (ref $_[$i]) { undef %named; last; } else { $named{$_[$i]} = $_[$i + 1]; } ; } ; return {%numbered, %named}; }
l!l&&r!l&&!r
120
MC/DC0%0/2
$self->{"input_preparator"} ||= sub { my %numbered; for (my $i = 0; $i < @_; ++$i) { $numbered{$i} = $_[$i]; } ; my %named; for (my $i = @_ % 2; $i < @_; $i += 2) { if (ref $_[$i]) { undef %named; last; } else { $named{$_[$i]} = $_[$i + 1]; } ; } ; return {%numbered, %named}; }
$self->{"input_preparator"} sub { my %numbered; for (my $i = 0; $i < @_; ++$i) { $numbered{$i} = $_[$i]; } ; my %named; for (my $i = @_ % 2; $i < @_; $i += 2) { if (ref $_[$i]) { undef %named; last; } else { $named{$_[$i]} = $_[$i + 1]; } ; } ; return {%numbered, %named}; }
Truth table66%2/3
A ||= B
A= $self->{"input_preparator"}B= sub { my %numbered; for (my $i = 0; $i < @_; ++$i) { $numbered{$i} = $_[$i]; } ; my %named; for (my $i = @_ % 2; $i < @_; $i += 2) { if (ref $_[$i]) { undef %named; last; } else { $named{$_[$i]} = $_[$i + 1]; } ; } ; return {%numbered, %named}; }
ABresult
1X1
011
000
119 }
120  
121 1 ▶sub _get_output_preparator {
Subroutine _get_output_preparator: called CC=2 SCAR=7.1
122 1 my $self = shift;
123  
124 1 ▶ return $self->{output_preparator} ||= sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
125 1 my %numbered;
126  
127 1 ▶ for (my $i=0; $i< @_; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @_) 1 1 2
128 1 $numbered{$i} = $_[$i];
129 }
130  
131 1 return \%numbered;
132 1 ▶ };
Condition33%1/3
$self->{"output_preparator"} ||= sub { my %numbered; for (my $i = 0; $i < @_; ++$i) { $numbered{$i} = $_[$i]; } ; return \%numbered; }
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->{"output_preparator"} ||= sub { my %numbered; for (my $i = 0; $i < @_; ++$i) { $numbered{$i} = $_[$i]; } ; return \%numbered; }
$self->{"output_preparator"} sub { my %numbered; for (my $i = 0; $i < @_; ++$i) { $numbered{$i} = $_[$i]; } ; return \%numbered; }
Truth table33%1/3
A ||= B
A= $self->{"output_preparator"}B= sub { my %numbered; for (my $i = 0; $i < @_; ++$i) { $numbered{$i} = $_[$i]; } ; return \%numbered; }
ABresult
1X1
011
000
133 }
134  
135 3 ▶sub _get_on_fail {
Subroutine _get_on_fail: called CC=2 SCAR=7.0
136 3 my $self = shift;
137  
138 3 ▶ return $self->{on_fail} ||= sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
139 3 my ( $type, $package, $subname, $errors ) = @_;
140  
141 3 local $Carp::Internal{ (__PACKAGE__) } = 1;
142  
143 3 die LIVR::Contract::Exception->new(
144 type => $type,
145 package => $package,
146 subname => $subname,
147 errors => $errors
148 );
149 }
150 3 ▶}
Condition66%2/3
$self->{"on_fail"} ||= sub { my($type, $package, $subname, $errors) = @_; local $Carp::Internal{"LIVR::Contract"} = 1; die "LIVR::Contract::Exception"->new("type", $type, "package", $package, "subname", $subname, "errors", $errors); }
l!l&&r!l&&!r
120
MC/DC0%0/2
$self->{"on_fail"} ||= sub { my($type, $package, $subname, $errors) = @_; local $Carp::Internal{"LIVR::Contract"} = 1; die "LIVR::Contract::Exception"->new("type", $type, "package", $package, "subname", $subname, "errors", $errors); }
$self->{"on_fail"} sub { my($type, $package, $subname, $errors) = @_; local $Carp::Internal{"LIVR::Contract"} = 1; die "LIVR::Contract::Exception"->new("type", $type, "package", $package, "subname", $subname, "errors", $errors); }
Truth table66%2/3
A ||= B
A= $self->{"on_fail"}B= sub { my($type, $package, $subname, $errors) = @_; local $Carp::Internal{"LIVR::Contract"} = 1; die "LIVR::Contract::Exception"->new("type", $type, "package", $package, "subname", $subname, "errors", $errors); }
ABresult
1X1
011
000
151  
152  
153 =head1 NAME
154  
155 LIVR::Contract - Design by Contract in Perl with Language Independent Validation Rules (LIVR).
156  
157 =head1 SYNOPSIS
158  
159 # Common usage
160 use LIVR::Contract;
161  
162 # Positional arguments
163 contract 'my_method1' => (
164 requires => {
165 0 => [ 'required' ]
166 1 => [ 'required', 'positive_integer' ]
167 2 => [ 'required' ],
168 },
169 ensures => {
170 0 => ['required', 'positive_integer' ]
171 }
172 );
173  
174 # Named arguments
175 contract 'my_method2' => (
176 requires => {
177 0 => [ 'required' ],
178 id => [ 'required', 'positive_integer' ],
179 name => [ 'required' ],
180 },
181 ensures => {
182 0 => ['required', 'positive_integer' ]
183 }
184 );
185  
186 # Named arguments in hashref
187 contract 'my_method3' => (
188 requires => {
189 0 => [ 'required' ],
190 1 => [ 'required', { nested_object => {
191 id => [ 'required', 'positive_integer' ],
192 name => [ 'required' ],
193 }
194 }
195 },
196 ensures => {
197 0 => ['required', 'positive_integer' ]
198 }
199 );
200
201 sub my_method1 {
202 my ($self, $id, $name) = @_;
203 return 100;
204 }
205  
206 sub my_method2 {
207 my ($self, %named_args) = @_;
208 return 100;
209 }
210  
211 sub my_method3 {
212 my ($self, $named_args_hashref) = @_;
213 return 100;
214 }
215  
216 # Somewhere in your code
217 $self->my_method1( 100, 'Some Name');
218  
219 # Somewhere in your code
220 $self->my_method2(
221 id => 100,
222 name => 'Some Name',
223 );
224  
225 # Somewhere in your code
226 $self->my_method3({
227 id => 100,
228 name => 'Some Name',
229 });
230  
231  
232  
233 =head1 WARNING
234  
235 B<This software is under heavy development and considered ALPHA
236 quality. Things might be broken, not all
237 features have been implemented, and APIs are likely to change. YOU
238 HAVE BEEN WARNED.>
239  
240 =head1 DESCRIPTION
241  
242 L<LIVR::Contract> design by Contract in Perl with Language Independent Validation Rules (LIVR). Uses L<Validator::LIVR> underneath.
243  
244 See L<https://github.com/koorchik/LIVR> for rules descriptions.
245  
246 =head1 TODO
247  
248 =over 4
249  
250 =item * Contracts in separate files (Roles)
251  
252 =back
253  
254 =head1 AUTHOR
255  
256 Viktor Turskyi, C<< <koorchik at cpan.org> >>
257  
258 =head1 BUGS
259  
260 Please report any bugs or feature requests to Github L<https://github.com/koorchik/LIVR-Contract>
261  
262  
263 =head1 SUPPORT
264  
265 You can find documentation for this module with the perldoc command.
266  
267 perldoc LIVR::Contract
268  
269 =head1 ACKNOWLEDGEMENTS
270  
271  
272 =head1 LICENSE AND COPYRIGHT
273  
274 Copyright 2013 Viktor Turskyi.
275  
276 This program is free software; you can redistribute it and/or modify it
277 under the terms of the the Artistic License (2.0). You may obtain a
278 copy of the full license at:
279  
280 L<http://www.perlfoundation.org/artistic_license_2_0>
281  
282 Any use, modification, and distribution of the Standard or Modified
283 Versions is governed by this Artistic License. By using, modifying or
284 distributing the Package, you accept this license. Do not use, modify,
285 or distribute the Package, if you do not accept this license.
286  
287 If your Modified Version has been derived from a Modified Version made
288 by someone other than you, you are nevertheless required to ensure that
289 your Modified Version complies with the requirements of this license.
290  
291 This license does not grant you the right to use any trademark, service
292 mark, tradename, or logo of the Copyright Holder.
293  
294 This license includes the non-exclusive, worldwide, free-of-charge
295 patent license to make, have made, use, offer to sell, sell, import and
296 otherwise transfer the Package with respect to any patent claims
297 licensable by the Copyright Holder that are necessarily infringed by the
298 Package. If you institute patent litigation (including a cross-claim or
299 counterclaim) against any party alleging that the Package constitutes
300 direct or contributory patent infringement, then this Artistic License
301 to you shall terminate on the date that such litigation is filed.
302  
303 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
304 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
305 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
306 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
307 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
308 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
309 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
310 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
311  
312  
313 =cut
314  
315 1 1;
↑ Summary Contract/Exception.pm »