| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
exists $state->{"statement"} and exists $state->{"results"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%6/6
ref $stmt ne "" and $ref ne "CODE" and $ref ne "Regexp"
ref $stmt ne "" and $ref ne "CODE"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$ref eq "Regexp" and not $got =~ /$expected/
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/
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$ref eq "CODE" and not &$expected($got, $state)
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)
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
not $ref and $got ne $expected
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | 21 | my $expected = $current_state->{bound_params}; | |||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | 21 | ▶ | if ( scalar @$expected != scalar @$params ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$ref eq "Regexp" and $statement =~ /$stmt/
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/
| |||||||||||||||||||||||||||||||||||||||||||
| 149 | 35 | ▶ | return $_ if ( $ref eq 'CODE' and $stmt->( $statement, $_ ) ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$ref eq "CODE" and &$stmt($statement, $_)
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, $_)
| |||||||||||||||||||||||||||||||||||||||||||
| 150 | 31 | ▶ | return $_ if ( not $ref and $stmt eq $statement ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $ref and $stmt eq $statement
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | 1 | ▶ | if ( $got !~ /$expected/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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; | |||||||||||||||||||||||||||||||||||||||||