root/lang/perl/Class-Hookable/trunk/t/07_run_hook.t @ 1612

Revision 1612, 1.7 kB (checked in by nyarla, 6 years ago)

lang/perl/Class-Hookable: I implemented run_hook method and fix POD.

Line 
1#!perl -T
2
3use strict;
4use warnings;
5
6use Test::More tests => 5 + 2 + 4;
7use Class::Hookable;
8
9my $hook    = Class::Hookable->new;
10my $plugin  = Plugin->new;
11
12$hook->register_hook(
13    $plugin,
14    'once'      => \&once,
15    'context'   => \&context,
16    'foobar'    => $plugin->can('foo'),
17    'foobar'    => $plugin->can('bar'),
18    'dispatch'  => $plugin->can('foo'),
19);
20
21# -- run_hook test ------------------- #
22
23is(
24    $hook->run_hook_once('once', { foo => 'bar' }, \&callback),
25    'FOO'
26);
27
28sub once {
29    my ( $plugin, $context, $args ) = @_;
30
31    isa_ok( $plugin, 'Plugin' );
32    isa_ok( $context, 'Class::Hookable' );
33    is_deeply(
34        $args,
35        { foo => 'bar' },
36    );
37
38    return 'FOO',
39}
40
41sub callback {
42    my ( $result ) = @_;
43    is( $result, 'FOO' );
44}
45
46# -- context test -------------------- #
47
48$hook->context( Context->new );
49
50$hook->run_hook_once('context');
51
52sub context {
53    my ( $plugin, $context, $args ) = @_;
54
55    isa_ok( $context, 'Context' );
56}
57is_deeply(
58    [ $hook->run_hook('foobar') ],
59    [qw( FOO BAR )],
60);
61
62# -- dispatch_plugin test ------------ #
63
64no warnings 'redefine';
65*Class::Hookable::dispatch_plugin = sub {
66    my ( $self, $hook, $args, $action ) = @_;
67
68    is( $hook, 'dispatch' );
69    is( $args, undef );
70    is_deeply(
71        $action,
72        {
73            plugin      => $plugin,
74            callback    => $plugin->can('foo'),
75        }
76    );
77
78    return 0;
79};
80
81is(
82    $hook->run_hook_once('dispatch'),
83    undef,
84);
85
86package Plugin;
87sub new { bless {}, shift }
88sub foo { 'FOO' }
89sub bar { 'BAR' }
90
91package Context;
92sub new { bless {}, shift }
Note: See TracBrowser for help on using the browser.