Debian Codegraph @

t/114_decode_prefix.t

raw
1 use strict;
2 use warnings;
3 use Test::More tests => 8;
4
5 BEGIN {
6 $ENV{ PERL_JSON_BACKEND } = 0;
7 }
8
9 BEGIN { $ENV{PERL_JSON_BACKEND} ||= "JSON::backportPP"; }
10
11 use JSON;
12
13 my $json = JSON->new;
14
15 my $complete_text = qq/{"foo":"bar"}/;
16 my $garbaged_text = qq/{"foo":"bar"}\n/;
17 my $garbaged_text2 = qq/{"foo":"bar"}\n\n/;
18 my $garbaged_text3 = qq/{"foo":"bar"}\n----/;
19
20 is( ( $json->decode_prefix( $complete_text ) ) [1], 13 );
21 is( ( $json->decode_prefix( $garbaged_text ) ) [1], 13 );
22 is( ( $json->decode_prefix( $garbaged_text2 ) ) [1], 13 );
23 is( ( $json->decode_prefix( $garbaged_text3 ) ) [1], 13 );
24
25 eval { $json->decode( "\n" ) }; ok( $@ =~ /malformed JSON/ );
26 eval { $json->allow_nonref(0)->decode('null') }; ok $@ =~ /allow_nonref/;
27
28 eval { $json->decode_prefix( "\n" ) }; ok( $@ =~ /malformed JSON/ );
29 eval { $json->allow_nonref(0)->decode_prefix('null') }; ok $@ =~ /allow_nonref/;
30