Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
more tests for hooks
  • Loading branch information
kraih committed Jun 17, 2013
1 parent 587b100 commit 103817e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lib/Mojolicious/Plugins.pm
Expand Up @@ -14,13 +14,13 @@ sub emit_hook {
sub emit_chain {
my ($self, $name, @args) = @_;

my $wrapper;
my $wrapper = sub { };
for my $cb (reverse @{$self->subscribers($name)}) {
my $next = $wrapper;
$wrapper = sub { $cb->($next, @args) };
}

!$wrapper ? return : return $wrapper->();
return $wrapper->();
}

sub emit_hook_reverse {
Expand Down
24 changes: 19 additions & 5 deletions t/mojolicious/app.t
Expand Up @@ -101,12 +101,26 @@ ok $t->app->routes->is_hidden('write'), 'is hidden';
ok $t->app->routes->is_hidden('write_chunk'), 'is hidden';

# Unknown hooks
ok !$t->app->plugins->emit_chain('does_not_exist'),
'unknown chained hook has been emitted';
ok !!$t->app->plugins->emit_hook('does_not_exist'),
'unknown hook has been emitted';
ok !$t->app->plugins->emit_chain('does_not_exist'), 'hook has been emitted';
ok !!$t->app->plugins->emit_hook('does_not_exist'), 'hook has been emitted';
ok !!$t->app->plugins->emit_hook_reverse('does_not_exist'),
'unknown hook has been emitted';
'hook has been emitted';

# Custom hooks
my $custom;
$t->app->hook('custom_hook' => sub { $custom++ });
$t->app->plugins->emit_hook('custom_hook');
is $custom, 1, 'hook has been emitted';
$t->app->plugins->emit_hook_reverse('custom_hook');
is $custom, 2, 'hook has been emitted again';
$t->app->hook(
'custom_chain' => sub {
my $next = shift;
$next->();
return 3;
}
);
is $t->app->plugins->emit_chain('custom_chain'), 3, 'hook has been emitted';

# MojoliciousTest::Command::test_command (with abbreviation)
is $t->app->start(qw(test_command --to)), 'works too!', 'right result';
Expand Down

0 comments on commit 103817e

Please sign in to comment.