5
use Sereal::Encoder::Constants qw(:all);
9
use lib File::Spec->catdir(qw(t lib));
15
use Sereal::TestSet qw(:all);
17
use Test::More tests => 19;
23
my $e = Sereal::Encoder->new({
27
is($e->encode(1), $Header.integer(1), "Encoder works before exception");
28
$ok = eval{$out = $e->encode(bless({}, "Foo")); 1};
29
$err = $@ || 'Zombie error';
31
ok(!$ok, "Object throws exception");
32
ok($err =~ /object/i, 'Exception refers to object');
34
is($e->encode(1), $Header.integer(1), "Encoder works after exception");
36
$ok = eval {$out = $e->encode({}); 1};
37
ok($ok, "Non-blessed hash does not throw exception");
39
# test that code refs throw exception
40
$ok = eval {$out = $e->encode(sub {}); 1};
41
ok(!$ok, "Code ref throws exception");
44
# test that code refs with undef_unknown don't throw exceptions
46
my $e = Sereal::Encoder->new({undef_unknown => 1});
47
$ok = eval {$out = $e->encode(sub{}); 1};
48
$err = $@ || 'Zombie error';
49
ok($ok, "undef_unknown makes CODE encoding not fail");
50
is($out, $Header . chr(SRL_HDR_UNDEF), "output is undef")
52
hobodecode($out) if $ENV{DEBUG_SEREAL};
56
# test that code refs with stringify_unknown don't throw exceptions
58
my $e = Sereal::Encoder->new({stringify_unknown => 1});
60
$ok = eval {$out = $e->encode($sub); 1};
61
$err = $@ || 'Zombie error';
62
ok($ok, "stringify_unknown makes CODE encoding not fail");
64
my $str = $e->encode("$sub");
65
is($out, $str, "output is stringified ref")
67
hobodecode($out), hobodecode($str) if $ENV{DEBUG_SEREAL};
71
# test that code refs with warn_unknown do warn
73
my $e = Sereal::Encoder->new({stringify_unknown => 1, warn_unknown => 1});
77
$ok = eval {$out = $e->encode($sub); 1};
80
"warn_unknown warns about stringified sub";
83
# test that blessed code refs with stringify_unknown don't throw exceptions
85
my $e = Sereal::Encoder->new({stringify_unknown => 1});
86
my $sub = bless(sub {}, "Foo");
87
$ok = eval {$out = $e->encode($sub); 1};
88
$err = $@ || 'Zombie error';
89
ok($ok, "stringify_unknown makes CODE encoding not fail");
91
my $str = $e->encode("$sub");
92
is($out, $str, "output is stringified ref")
94
hobodecode($out), hobodecode($str) if $ENV{DEBUG_SEREAL};
98
# dito for string overloading
101
package BlessedCodeRefOverload;
102
use overload '""' => sub {$_[0]->()};
104
my ($class, $data) = @_;
105
bless sub {return $data} => __PACKAGE__;
109
package BlessedCodeRef;
111
my ($class, $data) = @_;
112
bless sub {return $data} => __PACKAGE__;
115
my $e = Sereal::Encoder->new({stringify_unknown => 1});
116
my $sub = BlessedCodeRefOverload->new("hello");
117
is("$sub", "hello", "BlessedCodeRefOverload stringification actually works as designed");
119
$ok = eval {$out = $e->encode($sub); 1};
120
$err = $@ || 'Zombie error';
121
ok($ok, "stringify_unknown makes CODE encoding not fail");
123
my $str = $e->encode("$sub");
124
is($out, $str, "output is stringified ref")
126
hobodecode($out), hobodecode($str) if $ENV{DEBUG_SEREAL};
129
# test that we get a warning with warn_unknown
130
$e = Sereal::Encoder->new({stringify_unknown => 1, warn_unknown => 1});
133
$ok = eval {$out = $e->encode($sub); 1};
136
"warn_unknown warns about stringified sub despite overloading";
138
# Test that we do NOT get a warning with warn_unknown set to -1
139
# FIXME Test::Warn doesn't have a "no_warnings" function, so let's just
140
# run this for now and hope the user will be spooked by the warning
141
# if there is one. Duh.
142
$e = Sereal::Encoder->new({stringify_unknown => 1, warn_unknown => -1});
143
$out = $e->encode($sub);
144
ok(defined $out && $out !~ /CODE/ && $out !~ "Blessed", "RV of encode makes some sense");
146
# Test that we DO get a warning for non-overloaded unsupported stuff
147
my $sub2 = BlessedCodeRef->new("hello");
150
$ok = eval {$out = $e->encode($sub2); 1};
153
"warn_unknown == -1 warns about stringified sub without overloading";