| 1 | #!perl -T
|
|---|
| 2 |
|
|---|
| 3 | use strict;
|
|---|
| 4 | use warnings;
|
|---|
| 5 |
|
|---|
| 6 | use Test::More tests => 5 + 2 + 4;
|
|---|
| 7 | use Class::Hookable;
|
|---|
| 8 |
|
|---|
| 9 | my $hook = Class::Hookable->new;
|
|---|
| 10 | my $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 |
|
|---|
| 23 | is(
|
|---|
| 24 | $hook->run_hook_once('once', { foo => 'bar' }, \&callback),
|
|---|
| 25 | 'FOO'
|
|---|
| 26 | );
|
|---|
| 27 |
|
|---|
| 28 | sub 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 |
|
|---|
| 41 | sub 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 |
|
|---|
| 52 | sub context {
|
|---|
| 53 | my ( $plugin, $context, $args ) = @_;
|
|---|
| 54 |
|
|---|
| 55 | isa_ok( $context, 'Context' );
|
|---|
| 56 | }
|
|---|
| 57 | is_deeply(
|
|---|
| 58 | [ $hook->run_hook('foobar') ],
|
|---|
| 59 | [qw( FOO BAR )],
|
|---|
| 60 | );
|
|---|
| 61 |
|
|---|
| 62 | # -- dispatch_plugin test ------------ #
|
|---|
| 63 |
|
|---|
| 64 | no 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 |
|
|---|
| 81 | is(
|
|---|
| 82 | $hook->run_hook_once('dispatch'),
|
|---|
| 83 | undef,
|
|---|
| 84 | );
|
|---|
| 85 |
|
|---|
| 86 | package Plugin;
|
|---|
| 87 | sub new { bless {}, shift }
|
|---|
| 88 | sub foo { 'FOO' }
|
|---|
| 89 | sub bar { 'BAR' }
|
|---|
| 90 |
|
|---|
| 91 | package Context;
|
|---|
| 92 | sub new { bless {}, shift }
|
|---|