From 23af0c7d29711a326be2a530b45297042e9c4b68 Mon Sep 17 00:00:00 2001 From: wu-lee Date: Wed, 30 Mar 2011 14:29:26 +0100 Subject: [PATCH] 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. --- lib/DBD/Mock.pm | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/lib/DBD/Mock.pm b/lib/DBD/Mock.pm index 3c40ba2..3ab7738 100644 --- a/lib/DBD/Mock.pm +++ b/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}))