Debian Codegraph @

t/serializer_helpers.t

raw
1 use strict;
2 use warnings;
3 use Test::More;
4 use Dancer2::Serializer::JSON;
5 use Dancer2::Serializer::YAML;
6
7 # The from_json/to_json/from_yaml/to_yaml helpers are the class-method face of
8 # the serializer engines: they are called as plain functions, outside any
9 # Dancer2 app, so there is no app logger for the engine to log to.
10 #
11 # That used to mean a failed parse raised "Class::XSAccessor: invalid instance
12 # method invocant" from inside the error handler -- which replaced the real
13 # parse error with an unrelated one (GH #1837). The helpers now build an engine
14 # instance with a log_cb that warns, so the diagnostic reaches the caller on
15 # STDERR instead of being lost. These tests pin both halves of that: the real
16 # error is reported, and nothing about the accessor comes back.
17
18 # Collect what the code under test says on STDERR, and give back both the
19 # return value and the warnings, so each subtest can assert on the pair.
20 sub capture_warnings {
21 my $code = shift;
22 my @warnings;
23 local $SIG{'__WARN__'} = sub { push @warnings, @_ };
24 my $result = $code->();
25 return ( $result, join '', @warnings );
26 }
27
28 subtest 'from_json reports a parse failure instead of hiding it' => sub {
29 my ( $result, $warnings )
30 = capture_warnings( sub { Dancer2::Serializer::JSON::from_json('{not json') } );
31
32 is( $result, undef, 'malformed JSON deserializes to undef' );
33 like( $warnings, qr/Failed to deserialize content/,
34 'and the failure is reported on STDERR' );
35 unlike( $warnings, qr/Class::XSAccessor/,
36 'not as an accessor error from inside the error handler' );
37 };
38
39 subtest 'from_yaml reports a parse failure instead of hiding it' => sub {
40 my ( $result, $warnings ) = capture_warnings(
41 sub { Dancer2::Serializer::YAML::from_yaml("\tbad:\n - [unclosed") } );
42
43 is( $result, undef, 'malformed YAML deserializes to undef' );
44 like( $warnings, qr/Failed to deserialize content/,
45 'and the failure is reported on STDERR' );
46 unlike( $warnings, qr/Class::XSAccessor/,
47 'not as an accessor error from inside the error handler' );
48 };
49
50 subtest 'to_json still warns about invalid UTF-8' => sub {
51 # Dancer2::Serializer::JSON::_invalid_utf8 has always let this one out --
52 # it warned directly whenever there was no object to log through. Now that
53 # the helpers do have an object, the notice travels through log_cb, and
54 # this is the test that keeps it visible rather than silently swallowed.
55 my ( $json, $warnings ) = capture_warnings(
56 sub { Dancer2::Serializer::JSON::to_json( { bad => pack( 'C', 0xFF ) } ) } );
57
58 ok( $json, 'serializing bytes that are not UTF-8 still produces JSON' );
59 like( $warnings, qr/Invalid UTF-8/, 'and says so on STDERR' );
60 };
61
62 subtest 'the helpers stay quiet when nothing is wrong' => sub {
63 my ( $data, $json_warnings ) = capture_warnings( sub {
64 Dancer2::Serializer::JSON::from_json(
65 Dancer2::Serializer::JSON::to_json( { a => 1, b => [ 1, 2 ] } ) );
66 } );
67
68 is_deeply( $data, { a => 1, b => [ 1, 2 ] }, 'JSON round trips' );
69 is( $json_warnings, '', 'and warns about nothing' );
70
71 my ( $yaml_data, $yaml_warnings ) = capture_warnings( sub {
72 Dancer2::Serializer::YAML::from_yaml(
73 Dancer2::Serializer::YAML::to_yaml( { a => 1, b => [ 1, 2 ] } ) );
74 } );
75
76 is_deeply( $yaml_data, { a => 1, b => [ 1, 2 ] }, 'YAML round trips' );
77 is( $yaml_warnings, '', 'and warns about nothing either' );
78 };
79
80 subtest 'a bare class-method call fails quietly rather than dying' => sub {
81 # Not just the helpers: any caller can invoke the engines as class methods,
82 # and Role::Serializer's error handler now guards its log_cb call with
83 # blessed(). There is nowhere to report to on this path, so the failure is
84 # silent -- but it must not raise.
85 my ( $outcome, $warnings ) = capture_warnings( sub {
86 my $result = eval { Dancer2::Serializer::JSON->deserialize('{not json') };
87 return { result => $result, error => $@ };
88 } );
89
90 is( $outcome->{'error'}, '',
91 'a class-method deserialize of malformed JSON does not die' );
92 is( $outcome->{'result'}, undef, 'it returns undef' );
93 unlike( $warnings, qr/Class::XSAccessor/, 'and raises no accessor error' );
94 };
95
96 done_testing();