Merge pull request #211 from jaredj/test-register-hook

Tests for Qpsmtpd::Plugin::register_hook()
This commit is contained in:
Matt Simerson 2015-01-28 15:57:18 -08:00
commit 17d0c42d58
2 changed files with 26 additions and 3 deletions

View File

@ -30,7 +30,7 @@ sub hook_name {
sub register_hook { sub register_hook {
my ($plugin, $hook, $method, $unshift) = @_; my ($plugin, $hook, $method, $unshift) = @_;
die $plugin->plugin_name . " : Invalid hook: $hook" unless $hooks{$hook}; die $plugin->plugin_name . ": Invalid hook: $hook" unless $hooks{$hook};
$plugin->{_qp}->log(LOGDEBUG, $plugin->plugin_name, "hooking", $hook) $plugin->{_qp}->log(LOGDEBUG, $plugin->plugin_name, "hooking", $hook)
unless $hook =~ /logging/; # can't log during load_logging() unless $hook =~ /logging/; # can't log during load_logging()

View File

@ -5,10 +5,12 @@ use Test::More;
use lib 'lib'; # test lib/Qpsmtpd (vs site_perl) use lib 'lib'; # test lib/Qpsmtpd (vs site_perl)
use lib 't'; use lib 't';
use Test::Qpsmtpd;
use_ok('Qpsmtpd::Plugin'); use_ok('Qpsmtpd::Plugin');
__db(); __db();
__register_hook();
done_testing(); done_testing();
@ -20,12 +22,33 @@ sub __db {
is( $db->{name}, 'testfoo', 'accepts name argument' ); is( $db->{name}, 'testfoo', 'accepts name argument' );
delete $plugin->{db}; delete $plugin->{db};
$db = $plugin->db( class => 'FakeDB' ); $db = $plugin->db( class => 'FakeDB' );
is( $db->{name}, 'testbar', 'name argument defaults to plugin name' ); is( $db->{name}, '___FakeHook___', 'db name defaults to plugin name' );
}
sub __register_hook {
eval {
my $plugin = FakePlugin->new;
$plugin->register_hook('bogus_hook');
};
ok( $@ =~ /^___FakeHook___: Invalid hook: bogus_hook/,
'register_hook() validates hook name' );
my $qp = Test::Qpsmtpd->new;
my $plugin = FakePlugin->new;
$plugin->{_qp} = $qp;
$plugin->register_hook('logging', sub { shift; return "arguments:@_" });
ok( my $registered = $qp->hooks->{logging}->[-1] );
is( $registered->{name}, '___FakeHook___',
'register_hook() sets plugin name' );
my $code = $registered->{code};
is( ref $code, 'CODE', 'register_hook() sets a coderef' );
is( join('',$code->(undef,qw[arg1 arg2])), 'arguments:arg1 arg2',
'register_hook(): coderef set correctly' );
$qp->unfake_hook('logging');
} }
package FakePlugin; package FakePlugin;
use parent 'Qpsmtpd::Plugin'; use parent 'Qpsmtpd::Plugin';
sub plugin_name { 'testbar' } sub plugin_name { '___FakeHook___' }
package FakeDB; package FakeDB;
sub new { sub new {