Mock/Session.pm

filter: Statementstmt 99.1% 114 / 115 Branchbran 98.0% 51 / 52 Conditioncond 89.7% 35 / 39 MC/DCmcdc 84.0% 21 / 25 Subroutinesub 100% 19 / 19 Podpod 0.0% 0 / 8 totaltotal 93.0% 240 / 258 CC 44 SCAR 19.2
CC
44
Cov
97%
_array_compare
13.0
_find_state_for
8.0
verify_statement
8.0
CRAP 6.8
« Mock/st.pm ↑ Summary Mock/dr.pm »
1 package DBD::Mock::Session;
2  
3 41 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 41 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 41 my $INSTANCE_COUNT = 1;
7  
8 # - Class - #
9  
10 43 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
11 43 my $class = shift;
12 43 ▶ my $name = ref( $_[0] ) ? "Session $INSTANCE_COUNT" : shift;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 23 20 43
13 43 $INSTANCE_COUNT++;
14  
15 43 $class->_verify_states( $name, @_ );
16  
17 34 bless {
18 name => $name,
19 states => \@_,
20 state_index => 0
21 }, $class;
22 }
23  
24 65 ▶sub _verify_state {
Subroutine _verify_state: called CC=7 SCAR=19.5
25 65 my ( $class, $state, $index, $name ) = @_;
26  
27 65 ▶ die "You must specify session states as HASH refs"
Branch: 1 branch
BranchTrueFalseTotal
if ref $state ne "HASH" 3 62 65
28 if ref($state) ne 'HASH';
29  
30 die "Bad state '$index' in DBD::Mock::Session ($name)"
31 if not exists $state->{statement}
32 62 ▶ or not exists $state->{results};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $state->{"statement"} and exists $state->{"results"} 3 59 62
Condition100%3/3
exists $state->{"statement"} and exists $state->{"results"}
!ll&&!rl&&r
2159
MC/DC100%2/2
exists $state->{"statement"} and exists $state->{"results"}
exists $state->{"statement"} exists $state->{"results"}
Truth table100%3/3
A and B
A= exists $state->{"statement"}B= exists $state->{"results"}
ABresult
0X0
100
111
33  
34 59 my $stmt = $state->{statement};
35 59 my $ref = ref $stmt;
36  
37 59 ▶ die "Bad 'statement' value '$stmt' in DBD::Mock::Session ($name)",
Branch: 1 branch
BranchTrueFalseTotal
if ref $stmt ne "" and $ref ne "CODE" and $ref ne "Regexp" 1 58 59
Condition100%6/6
ref $stmt ne "" and $ref ne "CODE" and $ref ne "Regexp"
!ll&&!rl&&r
5081
ref $stmt ne "" and $ref ne "CODE"
!ll&&!rl&&r
4559
MC/DC100%3/3
ref $stmt ne "" and $ref ne "CODE" and $ref ne "Regexp"
ref $stmt ne "" $ref ne "CODE" $ref ne "Regexp"
Truth table100%4/4
A and B and C
A= ref $stmt ne ""B= $ref ne "CODE"C= $ref ne "Regexp"
ABCresult
0XX0
10X0
1100
1111
38 if ref($stmt) ne ''
39 and $ref ne 'CODE'
40 and $ref ne 'Regexp';
41 }
42  
43 43 ▶sub _verify_states {
Subroutine _verify_states: called CC=3 SCAR=11.0
44 43 my ( $class, $name, @states ) = @_;
45  
46 43 ▶ die "You must specify at least one session state"
Branch: 1 branch
BranchTrueFalseTotal
if scalar @states == 0 2 41 43
47 if scalar @states == 0;
48  
49 41 for ( 0 .. scalar @states - 1 ) {
50 65 $class->_verify_state( $states[$_], $_, $name );
51 }
52 }
53  
54 # - Instance - #
55  
56 6 ▶sub name {
Subroutine name: called CC=1 SCAR=0.0
Pod undocumented
57 6 my $self = shift;
58 6 $self->{name};
59 }
60  
61 1 ▶sub reset {
Subroutine reset: called CC=1 SCAR=0.0
Pod undocumented
62 1 my $self = shift;
63 1 $self->{state_index} = 0;
64 }
65  
66 168 ▶sub current_state {
Subroutine current_state: called CC=1 SCAR=0.0
Pod undocumented
67 168 my $self = shift;
68 168 my $idx = $self->{state_index};
69 168 return $self->{states}[$idx];
70 }
71  
72 76 ▶sub has_states_left {
Subroutine has_states_left: called CC=1 SCAR=0.0
Pod undocumented
73 76 my $self = shift;
74 76 return $self->{state_index} < $self->_num_states;
75 }
76  
77 62 ▶sub verify_statement {
Subroutine verify_statement: called CC=8 SCAR=20.8
Pod undocumented
78 62 my ( $self, $got ) = @_;
79  
80 62 ▶ unless ( $self->has_states_left ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->has_states_left) 6 56 62
81 6 die "Session states exhausted, only '"
82 . $self->_num_states
83 . "' in DBD::Mock::Session ($self->name})";
84 }
85  
86 56 my $state = $self->current_state;
87 56 my $expected = $state->{statement};
88 56 my $ref = ref($expected);
89  
90 56 ▶ if ( $ref eq 'Regexp' and $got !~ /$expected/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "Regexp" and not $got =~ /$expected/) 1 55 56
Condition100%3/3
$ref eq "Regexp" and not $got =~ /$expected/
!ll&&!rl&&r
4871
MC/DC100%2/2
$ref eq "Regexp" and not $got =~ /$expected/
$ref eq "Regexp" not $got =~ /$expected/
Truth table100%3/3
A and not(B)
A= $ref eq "Regexp"B= not $got =~ /$expected/
ABresult
0X0
100
111
91 1 die "Statement does not match current state (with Regexp) in "
92 . "DBD::Mock::Session ($self->{name})\n"
93 . " got: $got\n"
94 . " expected: $expected",
95  
96 }
97  
98 55 ▶ if ( $ref eq 'CODE' and not $expected->( $got, $state ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "CODE" and not &$expected($got, $state)) 1 54 55
Condition100%3/3
$ref eq "CODE" and not &$expected($got, $state)
!ll&&!rl&&r
5041
MC/DC100%2/2
$ref eq "CODE" and not &$expected($got, $state)
$ref eq "CODE" not &$expected($got, $state)
Truth table100%3/3
A and not(B)
A= $ref eq "CODE"B= not &$expected($got, $state)
ABresult
0X0
100
111
99 1 die "Statement does not match current state (with CODE ref) in "
100 . "DBD::Mock::Session ($self->{name})";
101 }
102  
103 54 ▶ if ( not $ref and $got ne $expected ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ref and $got ne $expected) 1 53 54
Condition100%3/3
not $ref and $got ne $expected
!ll&&!rl&&r
11421
MC/DC100%2/2
not $ref and $got ne $expected
not $ref $got ne $expected
Truth table100%3/3
not(A) and B
A= not $refB= $got ne $expected
ABresult
0X0
100
111
104 1 die "Statement does not match current state in "
105 . "DBD::Mock::Session ($self->{name})\n"
106 . " got: $got\n"
107 . " expected: $expected";
108 }
109 }
110  
111 54 ▶sub results_for {
Subroutine results_for: called CC=1 SCAR=0.0
Pod undocumented
112 54 my ( $self, $statment ) = @_;
113 54 $self->_find_state_for($statment)->{results};
114 }
115  
116 53 ▶sub verify_bound_params {
Subroutine verify_bound_params: called CC=4 SCAR=13.9
Pod undocumented
117 53 my ( $self, $params, $options ) = @_;
118  
119 53 my $current_state = $self->current_state;
120 53 ▶ if ( exists ${$current_state}{bound_params} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists do { $current_state }->{"bound_params"}) 31 22 53
121 31 my $expected = $current_state->{bound_params};
122  
123 31 ▶ if ( scalar @$expected != scalar @$params ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$expected != scalar @$params) 1 30 31
124 1 die "Not the same number of bound params in current state in "
125 . "DBD::Mock::Session ($self->{name})\n"
126 1 . " got: @{$params}"
127 1 . " expected: @{$expected}";
128 }
129  
130 30 for ( 0 .. scalar @{$params} - 1 ) {
131 36 $self->_verify_bound_param( $params->[$_], $expected->[$_], $_, $options );
132 }
133  
134 }
135  
136 # and make sure we go to
137 # the next statement
138 43 $self->{state_index}++;
139 }
140  
141 54 ▶sub _find_state_for {
Subroutine _find_state_for: called CC=8 SCAR=20.8
142 54 my ( $self, $statement ) = @_;
143  
144 54 foreach ( $self->_remaining_states ) {
145 50 my $stmt = $_->{statement};
146 50 my $ref = ref($stmt);
147  
148 50 ▶ return $_ if ( $ref eq 'Regexp' and $statement =~ /$stmt/ );
Branch: 1 branch
BranchTrueFalseTotal
if $ref eq "Regexp" and $statement =~ /$stmt/ 7 43 50
Condition100%3/3
$ref eq "Regexp" and $statement =~ /$stmt/
!ll&&!rl&&r
4217
MC/DC100%2/2
$ref eq "Regexp" and $statement =~ /$stmt/
$ref eq "Regexp" $statement =~ /$stmt/
Truth table100%3/3
A and B
A= $ref eq "Regexp"B= $statement =~ /$stmt/
ABresult
0X0
100
111
149 43 ▶ return $_ if ( $ref eq 'CODE' and $stmt->( $statement, $_ ) );
Branch: 1 branch
BranchTrueFalseTotal
if $ref eq "CODE" and &$stmt($statement, $_) 4 39 43
Condition66%2/3
$ref eq "CODE" and &$stmt($statement, $_)
!ll&&!rl&&r
3904
MC/DC50%1/2
$ref eq "CODE" and &$stmt($statement, $_)
$ref eq "CODE" &$stmt($statement, $_)
Truth table66%2/3
A and B
A= $ref eq "CODE"B= &$stmt($statement, $_)
ABresult
0X0
100
111
150 39 ▶ return $_ if ( not $ref and $stmt eq $statement );
Branch: 1 branch
BranchTrueFalseTotal
if not $ref and $stmt eq $statement 38 1 39
Condition66%2/3
not $ref and $stmt eq $statement
!ll&&!rl&&r
1038
MC/DC50%1/2
not $ref and $stmt eq $statement
not $ref $stmt eq $statement
Truth table66%2/3
not(A) and B
A= not $refB= $stmt eq $statement
ABresult
0X0
100
111
151 }
152  
153 5 die "Statement '$statement' not found in session ($self->{name})";
154 }
155  
156 136 ▶sub _num_states {
Subroutine _num_states: called CC=1 SCAR=0.0
157 136 my $self = shift;
158 136 scalar @{ $self->{states} };
159 }
160  
161 54 ▶sub _remaining_states {
Subroutine _remaining_states: called CC=1 SCAR=0.0
162 54 my $self = shift;
163 54 my $start_index = $self->{state_index};
164 54 my $end_index = $self->_num_states - 1;
165 54 @{ $self->{states} }[ $start_index .. $end_index ];
166 }
167  
168 9 ▶sub _array_compare {
Subroutine _array_compare: called CC=13 SCAR=25.7
169 9 my ( $self, $got, $expected, $index ) = @_;
170 41 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
171  
172 9 my $ref_got = ref $got;
173  
174 9 ▶ if (not $ref_got or $ref_got ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ref_got or $ref_got ne "ARRAY") 1 8 9
Condition66%2/3
not $ref_got or $ref_got ne "ARRAY"
l!l&&r!l&&!r
108
MC/DC50%1/2
not $ref_got or $ref_got ne "ARRAY"
not $ref_got $ref_got ne "ARRAY"
Truth table66%2/3
not(A) or B
A= not $ref_gotB= $ref_got ne "ARRAY"
ABresult
1X1
011
000
175 1 die "Bound param $index does not match (using ARRAY) "
176 . "in current state in DBD::Mock::Session ($self->{name})\n"
177 . " got: $got\n"
178 . " expected: ARRAY\n";
179 }
180  
181 8 ▶ if ( scalar( @{ $got } ) != scalar( @{ $expected } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$got;} != scalar @{$expected;}) 1 7 8
182 1 my $got_len = scalar( @{ $got } );
183 1 my $expected_len = scalar( @{ $expected } );
184 1 die "Bound param $index does not match (using ARRAY) "
185 . "in current state in DBD::Mock::Session ($self->{name})\n"
186 . " got: array of length $got_len\n"
187 . " expected: array of length $expected_len\n";
188 }
189  
190 7 for my $i ( 0 .. $#{ $expected } ) {
191 10 my $got_value = $got->[$i];
192 10 my $expected_value = $expected->[$i];
193  
194 10 ▶ if ( not defined $expected_value
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $expected_value or defined $got_value) 1 9 10
Condition100%3/3
defined $expected_value or defined $got_value
l!l&&r!l&&!r
811
MC/DC100%2/2
defined $expected_value or defined $got_value
defined $expected_value defined $got_value
Truth table100%3/3
A or B
A= defined $expected_valueB= defined $got_value
ABresult
1X1
011
000
195 and not defined $got_value ) {
196 1 next;
197 }
198  
199 9 ▶ if ( not defined $got_value ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $got_value) 1 8 9
200 1 die "Bound array param $index differs at offset $i "
201 . "in current state in DBD::Mock::Session ($self->{name})\n"
202 . " got: <undef>\n"
203 . " expected: $expected_value\n";
204 }
205  
206 8 ▶ if ( not defined $expected_value ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $expected_value) 1 7 8
207 1 die "Bound array param $index differs at offset $i "
208 . "in current state in DBD::Mock::Session ($self->{name})\n"
209 . " got: $got_value"
210 . " expected: <undef>\n";
211 }
212  
213 7 my $ref = ref $expected_value;
214 7 ▶ if ( $ref and $ref eq 'Regexp' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref and $ref eq 'Regexp') { } 2 5 7
elsif ($got_value ne $expected_value) { } 1 4 5
Condition66%2/3
$ref and $ref eq "Regexp"
!ll&&!rl&&r
502
MC/DC50%1/2
$ref and $ref eq "Regexp"
$ref $ref eq "Regexp"
Truth table66%2/3
A and B
A= $refB= $ref eq "Regexp"
ABresult
0X0
100
111
215  
216 2 ▶ if ( $got_value !~ /$expected_value/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($got_value =~ /$expected_value/) 1 1 2
217 1 die "Bound array param $index differs at offset $i "
218 . "in current state in DBD::Mock::Session ($self->{name})\n"
219 . " got: $got_value\n"
220 . " expected: $expected_value\n";
221 }
222  
223 } elsif ( $got_value ne $expected_value ) {
224 1 die "Bound array param $index differs at offset $i "
225 . "in current state in DBD::Mock::Session ($self->{name})\n"
226 . " got: $got_value"
227 . " expected: $expected_value\n";
228 }
229 }
230 }
231  
232 36 ▶sub _verify_bound_param {
Subroutine _verify_bound_param: called CC=6 SCAR=18.0
233 36 my ( $self, $got, $expected, $index, $options ) = @_;
234 41 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
235  
236 36 my $ref = ref $expected;
237  
238 36 ▶ if ( $ref eq 'Regexp' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($ref eq 'Regexp') { } 1 35 36
elsif ($ref eq 'ARRAY' and $options->{'bound_arrays'}) { } 9 26 35
elsif ($got ne $expected) { } 3 23 26
239  
240 1 ▶ if ( $got !~ /$expected/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $got =~ /$expected/) 0 1 1
241 0 die "Bound param $index do not match (using regexp) "
242 . "in current state in DBD::Mock::Session ($self->{name})"
243 . " got: $got\n"
244 . " expected: $expected";
245 }
246 ▶ } elsif ( $ref eq 'ARRAY' and $options->{bound_arrays} ) {
Condition100%3/3
$ref eq "ARRAY" and $options->{"bound_arrays"}
!ll&&!rl&&r
2519
MC/DC100%2/2
$ref eq "ARRAY" and $options->{"bound_arrays"}
$ref eq "ARRAY" $options->{"bound_arrays"}
Truth table100%3/3
A and B
A= $ref eq "ARRAY"B= $options->{"bound_arrays"}
ABresult
0X0
100
111
247 9 $self->_array_compare( $got, $expected, $index, $options );
248 } elsif ( $got ne $expected ) {
249 3 die "Bound param $index do not match "
250 . "in current state in DBD::Mock::Session ($self->{name})\n"
251 . " got: $got\n"
252 . " expected: $expected";
253 }
254 }
255  
256 41 1;
« Mock/st.pm ↑ Summary Mock/dr.pm »