xref: /openbsd/gnu/usr.bin/perl/cpan/JSON-PP/t/002_error.t (revision 256a93a4)
15759b3d2Safresh1# copied over from JSON::XS and modified to use JSON::PP
2898184e3Ssthen
3898184e3Ssthenuse strict;
4*256a93a4Safresh1use warnings;
5898184e3Ssthenuse Test::More;
6f3efcd01Safresh1BEGIN { plan tests => 35 };
7898184e3Ssthen
8898184e3SsthenBEGIN { $ENV{PERL_JSON_BACKEND} = 0; }
9898184e3Ssthen
10898184e3Ssthenuse utf8;
11898184e3Ssthenuse JSON::PP;
12f3efcd01Safresh1no warnings;
13898184e3Ssthen
14898184e3Ssthen
15898184e3Sstheneval { JSON::PP->new->encode ([\-1]) }; ok $@ =~ /cannot encode reference/;
16898184e3Sstheneval { JSON::PP->new->encode ([\undef]) }; ok $@ =~ /cannot encode reference/;
17898184e3Sstheneval { JSON::PP->new->encode ([\2]) }; ok $@ =~ /cannot encode reference/;
18898184e3Sstheneval { JSON::PP->new->encode ([\{}]) }; ok $@ =~ /cannot encode reference/;
19898184e3Sstheneval { JSON::PP->new->encode ([\[]]) }; ok $@ =~ /cannot encode reference/;
20898184e3Sstheneval { JSON::PP->new->encode ([\\1]) }; ok $@ =~ /cannot encode reference/;
21f3efcd01Safresh1
22898184e3Sstheneval { JSON::PP->new->allow_nonref (1)->decode ('"\u1234\udc00"') }; ok $@ =~ /missing high /;
23898184e3Sstheneval { JSON::PP->new->allow_nonref->decode ('"\ud800"') }; ok $@ =~ /missing low /;
24898184e3Sstheneval { JSON::PP->new->allow_nonref (1)->decode ('"\ud800\u1234"') }; ok $@ =~ /surrogate pair /;
25f3efcd01Safresh1
26f3efcd01Safresh1eval { JSON::PP->new->allow_nonref (0)->decode ('null') }; ok $@ =~ /allow_nonref/;
27898184e3Sstheneval { JSON::PP->new->allow_nonref (1)->decode ('+0') }; ok $@ =~ /malformed/;
28898184e3Sstheneval { JSON::PP->new->allow_nonref->decode ('.2') }; ok $@ =~ /malformed/;
29898184e3Sstheneval { JSON::PP->new->allow_nonref (1)->decode ('bare') }; ok $@ =~ /malformed/;
30898184e3Sstheneval { JSON::PP->new->allow_nonref->decode ('naughty') }; ok $@ =~ /null/;
31898184e3Sstheneval { JSON::PP->new->allow_nonref (1)->decode ('01') }; ok $@ =~ /leading zero/;
32898184e3Sstheneval { JSON::PP->new->allow_nonref->decode ('00') }; ok $@ =~ /leading zero/;
33898184e3Sstheneval { JSON::PP->new->allow_nonref (1)->decode ('-0.') }; ok $@ =~ /decimal point/;
34898184e3Sstheneval { JSON::PP->new->allow_nonref->decode ('-0e') }; ok $@ =~ /exp sign/;
35898184e3Sstheneval { JSON::PP->new->allow_nonref (1)->decode ('-e+1') }; ok $@ =~ /initial minus/;
36898184e3Sstheneval { JSON::PP->new->allow_nonref->decode ("\"\n\"") }; ok $@ =~ /invalid character/;
37898184e3Sstheneval { JSON::PP->new->allow_nonref (1)->decode ("\"\x01\"") }; ok $@ =~ /invalid character/;
38898184e3Sstheneval { JSON::PP->new->decode ('[5') }; ok $@ =~ /parsing array/;
39898184e3Sstheneval { JSON::PP->new->decode ('{"5"') }; ok $@ =~ /':' expected/;
40898184e3Sstheneval { JSON::PP->new->decode ('{"5":null') }; ok $@ =~ /parsing object/;
41898184e3Ssthen
42898184e3Sstheneval { JSON::PP->new->decode (undef) }; ok $@ =~ /malformed/;
43898184e3Sstheneval { JSON::PP->new->decode (\5) }; ok !!$@; # Can't coerce readonly
44898184e3Sstheneval { JSON::PP->new->decode ([]) }; ok $@ =~ /malformed/;
45898184e3Sstheneval { JSON::PP->new->decode (\*STDERR) }; ok $@ =~ /malformed/;
46898184e3Sstheneval { JSON::PP->new->decode (*STDERR) }; ok !!$@; # cannot coerce GLOB
47898184e3Ssthen
48898184e3Sstheneval { decode_json ("\"\xa0") }; ok $@ =~ /malformed.*character/;
49898184e3Sstheneval { decode_json ("\"\xa0\"") }; ok $@ =~ /malformed.*character/;
50f3efcd01Safresh1{ #SKIP_UNLESS_XS4_COMPAT 4
51f3efcd01Safresh1eval { decode_json ("1\x01") }; ok $@ =~ /garbage after/;
52f3efcd01Safresh1eval { decode_json ("1\x00") }; ok $@ =~ /garbage after/;
53f3efcd01Safresh1eval { decode_json ("\"\"\x00") }; ok $@ =~ /garbage after/;
54f3efcd01Safresh1eval { decode_json ("[]\x00") }; ok $@ =~ /garbage after/;
55f3efcd01Safresh1}
56898184e3Ssthen
57