From 3b0d60545dcc139bdd806fd3811fb7cb3bad5cd5 Mon Sep 17 00:00:00 2001 From: Jared Johnson Date: Wed, 28 Jan 2015 17:14:45 -0600 Subject: [PATCH] Tests for Qpsmtpd::Plugin::register_hook() --- lib/Qpsmtpd/Plugin.pm | 2 +- t/qpsmtpd-plugin.t | 27 +++++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/lib/Qpsmtpd/Plugin.pm b/lib/Qpsmtpd/Plugin.pm index 72441d5..ef8b2c1 100644 --- a/lib/Qpsmtpd/Plugin.pm +++ b/lib/Qpsmtpd/Plugin.pm @@ -30,7 +30,7 @@ sub hook_name { sub register_hook { 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) unless $hook =~ /logging/; # can't log during load_logging() diff --git a/t/qpsmtpd-plugin.t b/t/qpsmtpd-plugin.t index 9bb0e37..95695fe 100644 --- a/t/qpsmtpd-plugin.t +++ b/t/qpsmtpd-plugin.t @@ -5,10 +5,12 @@ use Test::More; use lib 'lib'; # test lib/Qpsmtpd (vs site_perl) use lib 't'; +use Test::Qpsmtpd; use_ok('Qpsmtpd::Plugin'); __db(); +__register_hook(); done_testing(); @@ -20,12 +22,33 @@ sub __db { is( $db->{name}, 'testfoo', 'accepts name argument' ); delete $plugin->{db}; $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; use parent 'Qpsmtpd::Plugin'; -sub plugin_name { 'testbar' } +sub plugin_name { '___FakeHook___' } package FakeDB; sub new {