Debian Codegraph @

t/22_comment_at_eof.t

raw
1 # copied over from JSON::XS and modified to use JSON
2
3 # the original test case was provided by [email protected]
4
5 use strict;
6 use warnings;
7
8 use Test::More tests => 13;
9
10 BEGIN { $ENV{PERL_JSON_BACKEND} ||= "JSON::backportPP"; }
11
12 use JSON;
13
14 use Data::Dumper qw( Dumper );
15
16 sub decoder {
17 my ($str) = @_;
18
19 my $json = JSON->new->relaxed;
20
21 $json->incr_parse($_[0]);
22
23 my $rv;
24 if (!eval { $rv = $json->incr_parse(); 1 }) {
25 $rv = "died with $@";
26 }
27
28 local $Data::Dumper::Useqq = 1;
29 local $Data::Dumper::Terse = 1;
30 local $Data::Dumper::Indent = 0;
31
32 return Dumper($rv);
33 }
34
35 is( decoder( "[]" ), '[]', 'array baseline' );
36 is( decoder( " []" ), '[]', 'space ignored before array' );
37 is( decoder( "\n[]" ), '[]', 'newline ignored before array' );
38 is( decoder( "# foo\n[]" ), '[]', 'comment ignored before array' );
39 is( decoder( "# fo[o\n[]"), '[]', 'comment ignored before array' );
40 is( decoder( "# fo]o\n[]"), '[]', 'comment ignored before array' );
41 is( decoder( "[# fo]o\n]"), '[]', 'comment ignored inside array' );
42
43 is( decoder( "" ), 'undef', 'eof baseline' );
44 is( decoder( " " ), 'undef', 'space ignored before eof' );
45 is( decoder( "\n" ), 'undef', 'newline ignored before eof' );
46 is( decoder( "#,foo\n" ), 'undef', 'comment ignored before eof' );
47 is( decoder( "# []o\n" ), 'undef', 'comment ignored before eof' );
48
49 is( decoder(qq/#\n[#foo\n"#\\n"#\n]/), '["#\n"]', 'array and string in multiple lines' );
50