Skip to content
Browse files

Add DBD::Mock::Session->current_state method

This just makes the code simpler to follow. Particularly the portion
in validate_bound_params where results are loaded into the $tracker
object.
  • Loading branch information...
1 parent 4dcccc7 commit 23af0c7d29711a326be2a530b45297042e9c4b68 wu-lee committed
Showing with 14 additions and 4 deletions.
  1. +14 −4 lib/DBD/Mock.pm
View
18 lib/DBD/Mock.pm
@@ -622,9 +622,12 @@ sub execute {
if (my $session = $dbh->{mock_session}) {
eval {
+ my $state = $session->current_state;
$session->verify_bound_params($dbh, $tracker->bound_params());
- my $idx = $session->{state_index} - 1;
- my @results = @{$session->{states}->[$idx]->{results}};
+
+ # Load a copy of the results to return (minus the field
+ # names) into the tracker
+ my @results = @{$state->{results}};
shift @results;
$tracker->{return_data} = \@results;
};
@@ -1193,6 +1196,12 @@ sub name { (shift)->{name} }
sub reset { (shift)->{state_index} = 0 }
sub num_states { scalar( @{ (shift)->{states} } ) }
+sub current_state {
+ my $self = shift;
+ my $idx = $self->{state_index};
+ return $self->{states}[$idx];
+}
+
sub has_states_left {
my $self = shift;
return $self->{state_index} < scalar(@{$self->{states}});
@@ -1204,7 +1213,7 @@ sub verify_statement {
($self->has_states_left)
|| die "Session states exhausted, only '" . scalar(@{$self->{states}}) . "' in DBD::Mock::Session (" . $self->{name} . ")";
- my $current_state = $self->{states}->[$self->{state_index}];
+ my $current_state = $self->current_state;
# make sure our state is good
(exists ${$current_state}{statement} && exists ${$current_state}{results})
|| die "Bad state '" . $self->{state_index} . "' in DBD::Mock::Session (" . $self->{name} . ")";
@@ -1236,7 +1245,8 @@ sub verify_statement {
sub verify_bound_params {
my ($self, $dbh, $params) = @_;
- my $current_state = $self->{states}->[$self->{state_index}];
+
+ my $current_state = $self->current_state;
if (exists ${$current_state}{bound_params}) {
my $expected = $current_state->{bound_params};
(scalar(@{$expected}) == scalar(@{$params}))

0 comments on commit 23af0c7

Please sign in to comment.
Something went wrong with that request. Please try again.