Debian Codegraph @

t/classes/Dancer2-Core-Hook/new.t

raw
1 use strict;
2 use warnings;
3 use Test::More tests => 12;
4 use Test::Fatal;
5
6 use_ok('Dancer2::Core::Hook');
7
8 like(
9 exception { Dancer2::Core::Hook->new( name => 'myname' ) },
10 qr/^Missing required arguments: code/,
11 'Must provide code attribute',
12 );
13
14 like(
15 exception { Dancer2::Core::Hook->new( code => sub {1} ) },
16 qr/^Missing required arguments: name/,
17 'Must provide name attribute',
18 );
19
20 is(
21 exception {
22 Dancer2::Core::Hook->new( name => 'myname', code => sub {1} )
23 },
24 undef,
25 'Can create hook with name and code',
26 );
27
28 {
29 my $hook = Dancer2::Core::Hook->new(
30 name => 'before_template',
31 code => sub {
32 my $input = shift;
33 ::is( $input, 'input', 'Correct input for hook' );
34 return 'output';
35 },
36 );
37
38 isa_ok( $hook, 'Dancer2::Core::Hook' );
39 can_ok( $hook, qw<name code> );
40
41 is(
42 $hook->name,
43 'before_template_render',
44 'before_template becomes before_template_render',
45 );
46
47 isa_ok( $hook->code, 'CODE' );
48
49 is(
50 $hook->code->('input'),
51 'output',
52 'Hook returned proper output',
53 );
54 }
55
56 {
57 my $hook = Dancer2::Core::Hook->new(
58 name => 'CrashingHook',
59 code => sub { die 'dying' },
60 );
61
62 isa_ok( $hook, 'Dancer2::Core::Hook' );
63
64 like(
65 exception { $hook->code->() },
66 qr/^Hook error: dying/,
67 'Hook crashing caught',
68 );
69 }