| 1 | package Contract::Declare; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 8 | ▶ | use v5.14; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 8 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 8 | ▶ | use Role::Tiny (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 8 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 8 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 8 | our @EXPORT = qw(contract interface method returns); | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 8 | our $CURRENT_PKG; | |||||||||||||||||||||||||||
| 13 | 8 | my %REGISTRY; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 8 | our $VERSION = '1.0.0'; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 11 | ▶ | sub contract { | ||||||||||||||||||||||||||
|
Subroutine
contract: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 19 | 11 | local $CURRENT_PKG; | |||||||||||||||||||||||||||
| 20 | 11 | my $block; | |||||||||||||||||||||||||||
| 21 | 11 | ($CURRENT_PKG, $block) = @_; | |||||||||||||||||||||||||||
| 22 | 11 | $block->(); | |||||||||||||||||||||||||||
| 23 | 6 | _build_contract($CURRENT_PKG, $REGISTRY{$CURRENT_PKG}); | |||||||||||||||||||||||||||
| 24 | 6 | ▶ | delete $REGISTRY{$CURRENT_PKG} unless $ENV{CONTRACT_DECLARE_KEEP_CONTRACT}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 11 | ▶ | sub interface (&) { shift } | ||||||||||||||||||||||||||
|
Subroutine
interface: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 28 | 12 | ▶ | sub returns { [ @_ ] } | ||||||||||||||||||||||||||
|
Subroutine
returns: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 12 | ▶ | sub method { | ||||||||||||||||||||||||||
|
Subroutine
method: called CC=11 SCAR=24.1
Pod
documented
| |||||||||||||||||||||||||||||
| 31 | 12 | my ($name, @parts) = @_; | |||||||||||||||||||||||||||
| 32 | 12 | my (@in, $out); | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 12 | for my $p (@parts) { | |||||||||||||||||||||||||||
| 35 | 21 | ▶ | if (ref($p) eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 12 | $out = $p; | |||||||||||||||||||||||||||
| 37 | 12 | last; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | 9 | push @in, $p; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 12 | for my $arg (@in) { | |||||||||||||||||||||||||||
| 43 | 9 | ▶ | croak "Contract violation: input argument for method '$name' must be an object with 'compiled_check'" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($arg) and $arg->can("compiled_check")
MC/DC50%1/2
builtin::blessed($arg) and $arg->can("compiled_check")
builtin::blessed($arg)
$arg->can("compiled_check")
Truth table66%2/3
A and B
A= builtin::blessed($arg)B= $arg->can("compiled_check")
| |||||||||||||||||||||||||||||
| 44 | unless blessed($arg) && $arg->can('compiled_check'); | ||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 10 | ▶ | if ($out) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 10 | ▶ | croak "Contract violation: return type for method '$name' must be an arrayref" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | unless ref($out) eq 'ARRAY'; | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 10 | for my $ret (@$out) { | |||||||||||||||||||||||||||
| 52 | 11 | ▶ | croak "Contract violation: each return type for method '$name' must be an object with 'compiled_check'" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
builtin::blessed($ret) and $ret->can("compiled_check")
MC/DC100%2/2
builtin::blessed($ret) and $ret->can("compiled_check")
builtin::blessed($ret)
$ret->can("compiled_check")
Truth table100%3/3
A and B
A= builtin::blessed($ret)B= $ret->can("compiled_check")
| |||||||||||||||||||||||||||||
| 53 | unless blessed($ret) && $ret->can('compiled_check'); | ||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 7 | $REGISTRY{$CURRENT_PKG}{$name} = [ \@in, $out ]; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 6 | ▶ | sub _build_contract { | ||||||||||||||||||||||||||
|
Subroutine
_build_contract: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 61 | 6 | my ($pkg, $contract) = @_; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 8 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 6 | ▶ | *{"${pkg}::new"} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 66 | 5 | my ($class, $impl) = @_; | |||||||||||||||||||||||||||
| 67 | 5 | my %cache; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 5 | for my $method (keys %$contract) { | |||||||||||||||||||||||||||
| 70 | 6 | my $code = $impl->can($method); | |||||||||||||||||||||||||||
| 71 | 6 | ▶ | croak "Contract violation: Implementation does not provide method '$method' for interface '$pkg'" unless $code; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 5 | $cache{$method} = $code; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | bless { | ||||||||||||||||||||||||||||
| 76 | 4 | _impl => $impl, | |||||||||||||||||||||||||||
| 77 | _cache => \%cache, | ||||||||||||||||||||||||||||
| 78 | }, $pkg; | ||||||||||||||||||||||||||||
| 79 | 6 | }; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 6 | for my $method (keys %$contract) { | |||||||||||||||||||||||||||
| 82 | 7 | my ($in_rules, $out_rules) = @{$contract->{$method}}; | |||||||||||||||||||||||||||
| 83 | 7 | my @in_checks = map { $_->compiled_check } @$in_rules; | |||||||||||||||||||||||||||
| 84 | 7 | my @out_checks = map { $_->compiled_check } @$out_rules; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 7 | ▶ | *{"${pkg}::$method"} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 87 | 7 | my ($self, @args) = @_; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 7 | _validate(\@args, \@in_checks, "$pkg\::$method args"); | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 5 | my @res = $self->{_cache}{$method}->($self->{_impl}, @args); | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 5 | _validate(\@res, \@out_checks, "$pkg\::$method return"); | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 4 | ▶ | return wantarray ? @res : $res[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 7 | }; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 6 | Role::Tiny->make_role($pkg); | |||||||||||||||||||||||||||
| 100 | 6 | $Role::Tiny::INFO{$pkg}{requires} = [ sort keys %$contract ]; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 8 | ▶ | use strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 12 | ▶ | sub _validate { | ||||||||||||||||||||||||||
|
Subroutine
_validate: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 106 | 12 | my ($values, $checkers, $label) = @_; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 12 | ▶ | return if @$checkers == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 9 | ▶ | if (@$values != @$checkers) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 1 | croak "Contract violation in $label: expected " . scalar(@$checkers) . " argument(s), got " . scalar(@$values); | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 8 | ▶ | for (my $i = 0; $i < @$checkers; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 10 | ▶ | next if $checkers->[$i]->($values->[$i]); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 2 | ▶ | my $val = defined($values->[$i]) ? "'$values->[$i]'" : 'undef'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 2 | croak "Contract violation in $label: argument #$i ($val) failed type check"; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 8 | 1; | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | __END__ | ||||||||||||||||||||||||||||