Contract/Pool.pm

filter: Statementstmt 90.9% 60 / 66 Branchbran 77.7% 14 / 18 Conditioncond 51.8% 14 / 27 MC/DCmcdc 23.0% 3 / 13 Subroutinesub 87.5% 14 / 16 Podpod 100% 9 / 9 totaltotal 76.5% 114 / 149 CC 19 SCAR 14.1
CC
19
Cov
79%
find_contracts_matching
6.6
find_contract
5.9
_add_contract
5.6
CRAP 4.1
« Contract/Cache.pm ↑ Summary Contract/Debug.pm »
1 #
2 # Sub::Contract::Pool - The pool of contracts
3 #
4 # $Id: Pool.pm,v 1.15 2009/06/16 12:23:58 erwan_lemonnier Exp $
5 #
6  
7 package Sub::Contract::Pool;
8  
9 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 23 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 23 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 23 ▶use vars qw($AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 23 ▶use accessors qw( _contract_index
Subroutine BEGIN: called CC=1 SCAR=0.0
16 23 );
17  
18 23 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 23 our $VERSION = '0.12';
21  
22 23 our @EXPORT = ();
23 23 our @EXPORT_OK = ('get_contract_pool');
24  
25 #---------------------------------------------------------------
26 #
27 # A singleton pattern with lazy initialization and embedded constructor
28 #
29  
30 23 my $pool;
31  
32 25 ▶sub get_contract_pool {
Subroutine get_contract_pool: called CC=2 SCAR=6.9
Pod documented
33 25 ▶ if (!defined $pool) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pool) 23 2 25
34 23 $pool = bless({},__PACKAGE__);
35 23 $pool->_contract_index({});
36 }
37 25 return $pool;
38 }
39  
40 #---------------------------------------------------------------
41 #
42 # list_all_contracts - return all contracts registered in the pool
43 #
44  
45 21 ▶sub list_all_contracts {
Subroutine list_all_contracts: called CC=1 SCAR=0.0
Pod documented
46 21 my $self = shift;
47 21 return values %{$self->_contract_index};
48 }
49  
50 #---------------------------------------------------------------
51 #
52 # has_contract -
53 #
54  
55 # TODO: should it be removed? to use find_contract instead? would it be too slow?
56  
57 151 ▶sub has_contract {
Subroutine has_contract: called CC=5 SCAR=16.2
Pod documented
58 151 my ($self, $contractor) = @_;
59  
60 151 ▶ croak "method has_contract() expects a fully qualified function name as argument"
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ != 2 or not defined $contractor or ref $contractor ne "" or not $contractor =~ /::/ 7 144 151
Condition77%7/9
scalar @_ != 2 or not defined $contractor or ref $contractor ne "" or not $contractor =~ /::/
l!l&&r!l&&!r
25144
scalar @_ != 2 or not defined $contractor or ref $contractor ne ""
l!l&&r!l&&!r
20149
scalar @_ != 2 or not defined $contractor
l!l&&r!l&&!r
20149
MC/DC50%2/4
scalar @_ != 2 or not defined $contractor or ref $contractor ne "" or not $contractor =~ /::/
scalar @_ != 2 not defined $contractor ref $contractor ne "" not $contractor =~ /::/
Truth table60%3/5
A or not(B) or C or not(D)
A= scalar @_ != 2B= not defined $contractorC= ref $contractor ne ""D= not $contractor =~ /::/
ABCDresult
1XXX1
01XX1
001X1
00011
00000
61 if ( scalar @_ != 2 ||
62 !defined $contractor ||
63 ref $contractor ne '' ||
64 $contractor !~ /::/
65 );
66  
67 144 my $index = $self->_contract_index;
68 144 return exists $index->{$contractor};
69 }
70  
71 #---------------------------------------------------------------
72 #
73 # _add_contract
74 #
75  
76 49 ▶sub _add_contract {
Subroutine _add_contract: called CC=5 SCAR=17.3
77 49 my ($self, $contract) = @_;
78  
79 49 ▶ croak "method add_contract() expects only 1 argument"
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ != 2 0 49 49
80 if (scalar @_ != 2);
81 49 ▶ croak "method add_contract() expects an instance of Sub::contract as argument"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $contract or ref $contract ne "Sub::Contract" 0 49 49
Condition33%1/3
not defined $contract or ref $contract ne "Sub::Contract"
l!l&&r!l&&!r
0049
MC/DC0%0/2
not defined $contract or ref $contract ne "Sub::Contract"
not defined $contract ref $contract ne "Sub::Contract"
Truth table33%1/3
not(A) or B
A= not defined $contractB= ref $contract ne "Sub::Contract"
ABresult
1X1
011
000
82 if (!defined $contract || ref $contract ne 'Sub::Contract');
83  
84 49 my $index = $self->_contract_index;
85 49 my $contractor = $contract->contractor;
86  
87 49 ▶ croak "trying to contract function [$contractor] twice"
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_contract($contractor) 0 49 49
88 if ($self->has_contract($contractor));
89  
90 49 $index->{$contractor} = $contract;
91  
92 49 return $self;
93 }
94  
95 ################################################################
96 #
97 #
98 # Operations on contracts during runtime
99 #
100 #
101 ################################################################
102  
103 1 ▶sub enable_all_contracts {
Subroutine enable_all_contracts: called CC=1 SCAR=0.0
Pod documented
104 1 my $self = shift;
105 1 map { $_->enable } $self->list_all_contracts;
106 }
107  
108 1 ▶sub disable_all_contracts {
Subroutine disable_all_contracts: called CC=1 SCAR=0.0
Pod documented
109 1 my $self = shift;
110 1 map { $_->disable } $self->list_all_contracts;
111 }
112  
113 0 ▶sub enable_contracts_matching {
Subroutine enable_contracts_matching: not called CC=1 SCAR=6.9
Pod documented
114 0 my $self = shift;
115 0 map { $_->enable } $self->find_contracts_matching(@_);
116 }
117  
118 0 ▶sub disable_contracts_matching {
Subroutine disable_contracts_matching: not called CC=1 SCAR=6.9
Pod documented
119 0 my $self = shift;
120 0 map { $_->disable } $self->find_contracts_matching(@_);
121 }
122  
123 50 ▶sub find_contract {
Subroutine find_contract: called CC=5 SCAR=17.8
Pod documented
124 50 my ($self, $contractor) = @_;
125  
126 50 ▶ croak "method find_contract() expects a fully qualified function name as argument"
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ != 2 or not defined $contractor or ref $contractor ne "" or not $contractor =~ /::/ 5 45 50
Condition44%4/9
scalar @_ != 2 or not defined $contractor or ref $contractor ne "" or not $contractor =~ /::/
l!l&&r!l&&!r
0545
scalar @_ != 2 or not defined $contractor or ref $contractor ne ""
l!l&&r!l&&!r
0050
scalar @_ != 2 or not defined $contractor
l!l&&r!l&&!r
0050
MC/DC25%1/4
scalar @_ != 2 or not defined $contractor or ref $contractor ne "" or not $contractor =~ /::/
scalar @_ != 2 not defined $contractor ref $contractor ne "" not $contractor =~ /::/
Truth table40%2/5
A or not(B) or C or not(D)
A= scalar @_ != 2B= not defined $contractorC= ref $contractor ne ""D= not $contractor =~ /::/
ABCDresult
1XXX1
01XX1
001X1
00011
00000
127 if ( scalar @_ != 2 ||
128 !defined $contractor ||
129 ref $contractor ne '' ||
130 $contractor !~ /::/
131 );
132  
133 45 my $index = $self->_contract_index;
134 45 return $index->{$contractor};
135 }
136  
137 10 ▶sub find_contracts_matching {
Subroutine find_contracts_matching: called CC=6 SCAR=18.8
Pod documented
138 10 my $self = shift;
139 10 my $match = shift;
140 10 my @contracts;
141  
142 # TODO: fix croak level when called from enable/disable_matching
143 # local $Carp::CarpLevel = 2 if ((caller(1))[3] =~ /^Sub::Contract::Pool::(enable|disable)_contracts_matching$/);
144  
145 10 ▶ croak "method find_contracts_matching() expects a regular expression"
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ != 0 or not defined $match or ref $match ne "" 0 10 10
Condition33%2/6
scalar @_ != 0 or not defined $match or ref $match ne ""
l!l&&r!l&&!r
0010
scalar @_ != 0 or not defined $match
l!l&&r!l&&!r
0010
MC/DC0%0/3
scalar @_ != 0 or not defined $match or ref $match ne ""
scalar @_ != 0 not defined $match ref $match ne ""
Truth table25%1/4
A or not(B) or C
A= scalar @_ != 0B= not defined $matchC= ref $match ne ""
ABCresult
1XX1
01X1
0011
0000
146 if (scalar @_ != 0 || !defined $match || ref $match ne '');
147  
148 10 ▶ while ( my ($name,$contract) = each %{$self->_contract_index} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$self->_contract_index;}) 40 10 50
149 40 ▶ push @contracts, $contract if ($name =~ /^$match$/);
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^$match$/ 13 27 40
150 }
151  
152 10 return @contracts;
153 }
154  
155 23 1;
156  
157 __END__
« Contract/Cache.pm ↑ Summary Contract/Debug.pm »