Skip to content

Commit

Permalink
Add DBD::Mock::Session->current_state method
Browse files Browse the repository at this point in the history
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
wu-lee committed Mar 30, 2011
1 parent 4dcccc7 commit 23af0c7
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions lib/DBD/Mock.pm
Expand Up @@ -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;
};
Expand Down Expand Up @@ -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}});
Expand All @@ -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} . ")";
Expand Down Expand Up @@ -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}))
Expand Down

0 comments on commit 23af0c7

Please sign in to comment.