blib/lib/DBD/Mock/Session.pm

filter: Statementstmt 98.8% 83 / 84 Branchbran 97.0% 33 / 34 Conditioncond 92.5% 25 / 27 MC/DCmcdc 88.2% 15 / 17 Subroutinesub 100% 17 / 17 Podpod 0.0% 0 / 8 totaltotal 92.5% 173 / 187 SCAR 34.0
CC
30
Cov
97%
_find_state_for
8.0
verify_statement
8.0
_verify_state
7.0
CRAP 30.0
« blib/lib/DBD/Mock/StatementTrack.pm ↑ Summary blib/lib/DBD/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 sub new {
11 35 ▶ my $class = shift;
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
12 35 ▶ my $name = ref( $_[0] ) ? "Session $INSTANCE_COUNT" : shift;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 15 20 35
13 35 $INSTANCE_COUNT++;
14  
15 35 $class->_verify_states( $name, @_ );
16  
17 26 bless {
18 name => $name,
19 states => \@_,
20 state_index => 0
21 }, $class;
22 }
23  
24 sub _verify_state {
25 55 ▶ my ( $class, $state, $index, $name ) = @_;
Subroutine _verify_state: called CC=7 SCAR=19.5
26  
27 55 ▶ die "You must specify session states as HASH refs"
Branch: 1 branch
BranchTrueFalseTotal
if ref $state ne "HASH" 3 52 55
28 if ref($state) ne 'HASH';
29  
30 die "Bad state '$index' in DBD::Mock::Session ($name)"
31 if not exists $state->{statement}
32 52 ▶ or not exists $state->{results};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $state->{"statement"} and exists $state->{"results"} 3 49 52
Condition100%3/3
exists $state->{"statement"} and exists $state->{"results"}
!ll&&!rl&&r
2149
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 49 my $stmt = $state->{statement};
35 49 my $ref = ref $stmt;
36  
37 49 ▶ 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 48 49
Condition100%6/6
ref $stmt ne "" and $ref ne "CODE" and $ref ne "Regexp"
!ll&&!rl&&r
4081
ref $stmt ne "" and $ref ne "CODE"
!ll&&!rl&&r
3559
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 sub _verify_states {
44 35 ▶ my ( $class, $name, @states ) = @_;
Subroutine _verify_states: called CC=3 SCAR=11.0
45  
46 35 ▶ die "You must specify at least one session state"
Branch: 1 branch
BranchTrueFalseTotal
if scalar @states == 0 2 33 35
47 if scalar @states == 0;
48  
49 33 for ( 0 .. scalar @states - 1 ) {
50 55 $class->_verify_state( $states[$_], $_, $name );
51 }
52 }
53  
54 # - Instance - #
55  
56 sub name {
57 6 ▶ my $self = shift;
Subroutine name: called CC=1 SCAR=0.0
Pod undocumented
58 6 $self->{name};
59 }
60  
61 sub reset {
62 1 ▶ my $self = shift;
Subroutine reset: called CC=1 SCAR=0.0
Pod undocumented
63 1 $self->{state_index} = 0;
64 }
65  
66 sub current_state {
67 138 ▶ my $self = shift;
Subroutine current_state: called CC=1 SCAR=0.0
Pod undocumented
68 138 my $idx = $self->{state_index};
69 138 return $self->{states}[$idx];
70 }
71  
72 sub has_states_left {
73 66 ▶ my $self = shift;
Subroutine has_states_left: called CC=1 SCAR=0.0
Pod undocumented
74 66 return $self->{state_index} < $self->_num_states;
75 }
76  
77 sub verify_statement {
78 52 ▶ my ( $self, $got ) = @_;
Subroutine verify_statement: called CC=8 SCAR=20.8
Pod undocumented
79  
80 52 ▶ unless ( $self->has_states_left ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->has_states_left) 6 46 52
81 6 die "Session states exhausted, only '"
82 . $self->_num_states
83 . "' in DBD::Mock::Session ($self->name})";
84 }
85  
86 46 my $state = $self->current_state;
87 46 my $expected = $state->{statement};
88 46 my $ref = ref($expected);
89  
90 46 ▶ if ( $ref eq 'Regexp' and $got !~ /$expected/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "Regexp" and not $got =~ /$expected/) 1 45 46
Condition100%3/3
$ref eq "Regexp" and not $got =~ /$expected/
!ll&&!rl&&r
3871
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 45 ▶ if ( $ref eq 'CODE' and not $expected->( $got, $state ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "CODE" and not &$expected($got, $state)) 1 44 45
Condition100%3/3
$ref eq "CODE" and not &$expected($got, $state)
!ll&&!rl&&r
4041
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 44 ▶ if ( not $ref and $got ne $expected ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ref and $got ne $expected) 1 43 44
Condition100%3/3
not $ref and $got ne $expected
!ll&&!rl&&r
11321
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 sub results_for {
112 46 ▶ my ( $self, $statment ) = @_;
Subroutine results_for: called CC=1 SCAR=0.0
Pod undocumented
113 46 $self->_find_state_for($statment)->{results};
114 }
115  
116 sub verify_bound_params {
117 43 ▶ my ( $self, $params ) = @_;
Subroutine verify_bound_params: called CC=4 SCAR=13.9
Pod undocumented
118  
119 43 my $current_state = $self->current_state;
120 43 ▶ if ( exists ${$current_state}{bound_params} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists do { $current_state }->{"bound_params"}) 21 22 43
121 21 my $expected = $current_state->{bound_params};
122  
123 21 ▶ if ( scalar @$expected != scalar @$params ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$expected != scalar @$params) 1 20 21
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 20 for ( 0 .. scalar @{$params} - 1 ) {
131 26 $self->_verify_bound_param( $params->[$_], $expected->[$_], $_ );
132 }
133  
134 }
135  
136 # and make sure we go to
137 # the next statement
138 40 $self->{state_index}++;
139 }
140  
141 sub _find_state_for {
142 46 ▶ my ( $self, $statement ) = @_;
Subroutine _find_state_for: called CC=8 SCAR=20.8
143  
144 46 foreach ( $self->_remaining_states ) {
145 42 my $stmt = $_->{statement};
146 42 my $ref = ref($stmt);
147  
148 42 ▶ return $_ if ( $ref eq 'Regexp' and $statement =~ /$stmt/ );
Branch: 1 branch
BranchTrueFalseTotal
if $ref eq "Regexp" and $statement =~ /$stmt/ 7 35 42
Condition100%3/3
$ref eq "Regexp" and $statement =~ /$stmt/
!ll&&!rl&&r
3417
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 35 ▶ return $_ if ( $ref eq 'CODE' and $stmt->( $statement, $_ ) );
Branch: 1 branch
BranchTrueFalseTotal
if $ref eq "CODE" and &$stmt($statement, $_) 4 31 35
Condition66%2/3
$ref eq "CODE" and &$stmt($statement, $_)
!ll&&!rl&&r
3104
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 31 ▶ return $_ if ( not $ref and $stmt eq $statement );
Branch: 1 branch
BranchTrueFalseTotal
if not $ref and $stmt eq $statement 30 1 31
Condition66%2/3
not $ref and $stmt eq $statement
!ll&&!rl&&r
1030
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 sub _num_states {
157 118 ▶ my $self = shift;
Subroutine _num_states: called CC=1 SCAR=0.0
158 118 scalar @{ $self->{states} };
159 }
160  
161 sub _remaining_states {
162 46 ▶ my $self = shift;
Subroutine _remaining_states: called CC=1 SCAR=0.0
163 46 my $start_index = $self->{state_index};
164 46 my $end_index = $self->_num_states - 1;
165 46 @{ $self->{states} }[ $start_index .. $end_index ];
166 }
167  
168 sub _verify_bound_param {
169 26 ▶ my ( $self, $got, $expected, $index ) = @_;
Subroutine _verify_bound_param: called CC=4 SCAR=14.0
170 41 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
171  
172 26 my $ref = ref $expected;
173  
174 26 ▶ if ( $ref eq 'Regexp' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq 'Regexp') { } 1 25 26
elsif ($got ne $expected) { } 2 23 25
175  
176 1 ▶ if ( $got !~ /$expected/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $got =~ /$expected/) 0 1 1
177 0 die "Bound param $index do not match (using regexp) "
178 . "in current state in DBD::Mock::Session ($self->{name})"
179 . " got: $got\n"
180 . " expected: $expected";
181 }
182  
183 } elsif ( $got ne $expected ) {
184 2 die "Bound param $index do not match "
185 . "in current state in DBD::Mock::Session ($self->{name})\n"
186 . " got: $got\n"
187 . " expected: $expected";
188 }
189 }
190  
191 41 1;
« blib/lib/DBD/Mock/StatementTrack.pm ↑ Summary blib/lib/DBD/Mock/dr.pm »