Generalized method for test hooks

This commit is contained in:
Jared Johnson 2014-12-17 18:43:17 -06:00
parent 7d33c42d35
commit 46d26848a9

View File

@ -84,26 +84,37 @@ sub validate_password {
return $deny, "$file - wrong password"; return $deny, "$file - wrong password";
} }
sub fake_hook {
my ( $self, $hook, $sub ) = @_;
unshift @{ $self->qp->hooks->{$hook} ||= [] },
{
name => '___FakeHook___',
code => $sub,
};
}
sub unfake_hook {
my ( $self, $hook ) = @_;
$self->qp->hooks->{$hook} = [
grep { $_->{name} ne '___FakeHook___' }
@{ $self->qp->hooks->{$hook} || [] }
];
}
sub fake_config { sub fake_config {
my $self = shift; my $self = shift;
my $fake_config = {@_}; my $fake_config = {@_};
unshift @{ $self->qp->hooks->{config} ||= [] }, $self->fake_hook( 'config',
{ sub {
name => '___FakeHook___', my ( $self, $txn, $conf ) = @_;
code => sub { return DECLINED if ! exists $fake_config->{$conf};
my ( $self, $txn, $conf ) = @_; return OK, $fake_config->{$conf};
return DECLINED if ! exists $fake_config->{$conf}; } );
return OK, $fake_config->{$conf};
},
};
} }
sub unfake_config { sub unfake_config {
my ( $self ) = @_; my ( $self ) = @_;
$self->qp->hooks->{config} = [ $self->unfake_hook('config');
grep { $_->{name} ne '___FakeHook___' }
@{ $self->qp->hooks->{config} || [] }
];
} }
1; 1;