| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
use Test::More tests => 4; |
| 4 |
use Test::Fatal; |
| 5 |
|
| 6 |
use_ok('Dancer2::Core::Hook'); |
| 7 |
|
| 8 |
{ |
| 9 |
package Serializer::OK; |
| 10 |
use Moo; |
| 11 |
with 'Dancer2::Core::Role::Serializer'; |
| 12 |
has '+content_type' => ( default => sub {'plain/test'} ); |
| 13 |
|
| 14 |
sub serialize {'{'.$_[1].'}'} |
| 15 |
sub deserialize {'['.$_[1].']'} |
| 16 |
} |
| 17 |
|
| 18 |
subtest 'Successful' => sub { |
| 19 |
plan tests => 5; |
| 20 |
|
| 21 |
my $srl = Serializer::OK->new; |
| 22 |
isa_ok( $srl, 'Serializer::OK' ); |
| 23 |
|
| 24 |
$srl->add_hook( |
| 25 |
Dancer2::Core::Hook->new( |
| 26 |
name => 'engine.serializer.before', |
| 27 |
code => sub { |
| 28 |
my $content = shift; |
| 29 |
::is( $content, 'foo', 'Correct content in before hook' ); |
| 30 |
}, |
| 31 |
) |
| 32 |
); |
| 33 |
|
| 34 |
$srl->add_hook( |
| 35 |
Dancer2::Core::Hook->new( |
| 36 |
name => 'engine.serializer.after', |
| 37 |
code => sub { |
| 38 |
my $content = shift; |
| 39 |
::is( $content, '{foo}', 'Correct content in after hook' ); |
| 40 |
}, |
| 41 |
) |
| 42 |
); |
| 43 |
|
| 44 |
is( $srl->serialize('foo'), '{foo}', 'Serializing' ); |
| 45 |
is( $srl->deserialize('bar'), '[bar]', 'Deserializing' ); |
| 46 |
}; |
| 47 |
|
| 48 |
{ |
| 49 |
package Serializer::NotOK; |
| 50 |
use Moo; |
| 51 |
with 'Dancer2::Core::Role::Serializer'; |
| 52 |
has '+content_type' => ( default => sub {'plain/test'} ); |
| 53 |
|
| 54 |
sub serialize { die '+' . $_[1] . '+' } |
| 55 |
sub deserialize { die '-' . $_[1] . '-' } |
| 56 |
} |
| 57 |
|
| 58 |
subtest 'Unsuccessful' => sub { |
| 59 |
plan tests => 21; |
| 60 |
|
| 61 |
use_ok('Dancer2::Logger::Capture'); |
| 62 |
|
| 63 |
{ |
| 64 |
my $logger = Dancer2::Logger::Capture->new; |
| 65 |
isa_ok( $logger, 'Dancer2::Logger::Capture' ); |
| 66 |
|
| 67 |
my $srl = Serializer::NotOK->new( |
| 68 |
log_cb => sub { $logger->log(@_) } |
| 69 |
); |
| 70 |
|
| 71 |
isa_ok( $srl, 'Serializer::NotOK' ); |
| 72 |
is( $srl->serialize('foo'), undef, 'Serialization result' ); |
| 73 |
|
| 74 |
my $trap = $logger->trapper; |
| 75 |
isa_ok( $trap, 'Dancer2::Logger::Capture::Trap' ); |
| 76 |
|
| 77 |
my $errors = $trap->read; |
| 78 |
isa_ok( $errors, 'ARRAY' ); |
| 79 |
is( scalar @{$errors}, 1, 'One error caught' ); |
| 80 |
|
| 81 |
my $msg = $errors->[0]; |
| 82 |
isa_ok( $msg, 'HASH' ); |
| 83 |
is( scalar keys %{$msg}, 3, 'Two items in the error' ); |
| 84 |
|
| 85 |
is( $msg->{'level'}, 'core', 'Correct level' ); |
| 86 |
like( |
| 87 |
$msg->{'message'}, |
| 88 |
qr{^Failed to serialize content: \+foo\+}, |
| 89 |
'Correct error message', |
| 90 |
); |
| 91 |
} |
| 92 |
|
| 93 |
{ |
| 94 |
my $logger = Dancer2::Logger::Capture->new; |
| 95 |
isa_ok( $logger, 'Dancer2::Logger::Capture' ); |
| 96 |
|
| 97 |
my $srl = Serializer::NotOK->new( |
| 98 |
log_cb => sub { $logger->log(@_) } |
| 99 |
); |
| 100 |
|
| 101 |
isa_ok( $srl, 'Serializer::NotOK' ); |
| 102 |
is( $srl->deserialize('bar'), undef, 'Deserialization result' ); |
| 103 |
|
| 104 |
my $trap = $logger->trapper; |
| 105 |
isa_ok( $trap, 'Dancer2::Logger::Capture::Trap' ); |
| 106 |
|
| 107 |
my $errors = $trap->read; |
| 108 |
isa_ok( $errors, 'ARRAY' ); |
| 109 |
is( scalar @{$errors}, 1, 'One error caught' ); |
| 110 |
|
| 111 |
my $msg = $errors->[0]; |
| 112 |
isa_ok( $msg, 'HASH' ); |
| 113 |
is( scalar keys %{$msg}, 3, 'Two items in the error' ); |
| 114 |
|
| 115 |
is( $msg->{'level'}, 'core', 'Correct level' ); |
| 116 |
like( |
| 117 |
$msg->{'message'}, |
| 118 |
qr{^Failed to deserialize content: \-bar\-}, |
| 119 |
'Correct error message', |
| 120 |
); |
| 121 |
} |
| 122 |
}; |
| 123 |
|
| 124 |
{ |
| 125 |
package Serializer::Empty; |
| 126 |
use Moo; |
| 127 |
with 'Dancer2::Core::Role::Serializer'; |
| 128 |
has '+content_type' => ( default => 'plain/test' ); |
| 129 |
sub serialize {'BAD SERIALIZE'} |
| 130 |
sub deserialize {'BAD DESERIALIZE'} |
| 131 |
} |
| 132 |
|
| 133 |
subtest 'Called with empty content' => sub { |
| 134 |
plan tests => 6; |
| 135 |
|
| 136 |
my $srl = Serializer::Empty->new; |
| 137 |
isa_ok( $srl, 'Serializer::Empty' ); |
| 138 |
can_ok( $srl, qw<serialize deserialize> ); |
| 139 |
|
| 140 |
is( |
| 141 |
$srl->serialize(), |
| 142 |
undef, |
| 143 |
'Do not try to serialize without input', |
| 144 |
); |
| 145 |
|
| 146 |
is( |
| 147 |
$srl->serialize(''), |
| 148 |
'', |
| 149 |
'Do not try to serialize with empty input', |
| 150 |
); |
| 151 |
|
| 152 |
is( |
| 153 |
$srl->deserialize(), |
| 154 |
undef, |
| 155 |
'Do not try to deserialize without input', |
| 156 |
); |
| 157 |
|
| 158 |
is( |
| 159 |
$srl->deserialize(''), |
| 160 |
'', |
| 161 |
'Do not try to deserialize with empty input', |
| 162 |
); |
| 163 |
} |
| 164 |
|