"Fossies" - the Fresh Open Source Software Archive

Member "perl-5.32.1/cpan/JSON-PP/t/008_pc_base.t" (18 Dec 2020, 2139 Bytes) of package /linux/misc/perl-5.32.1.tar.xz:


As a special service "Fossies" has tried to format the requested text file into HTML format (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 use Test::More;
    2 
    3 # copied over from JSON::PC and modified to use JSON::PP
    4 # copied over from JSON::XS and modified to use JSON::PP
    5 
    6 use strict;
    7 BEGIN { plan tests => 20 };
    8 BEGIN { $ENV{PERL_JSON_BACKEND} = 0; }
    9 
   10 use JSON::PP;
   11 
   12 my ($js,$obj);
   13 
   14 my $pc = new JSON::PP;
   15 
   16 $js  = q|{}|;
   17 
   18 $obj = $pc->decode($js);
   19 $js  = $pc->encode($obj);
   20 is($js,'{}', '{}');
   21 
   22 $js  = q|[]|;
   23 $obj = $pc->decode($js);
   24 $js  = $pc->encode($obj);
   25 is($js,'[]', '[]');
   26 
   27 
   28 $js  = q|{"foo":"bar"}|;
   29 $obj = $pc->decode($js);
   30 is($obj->{foo},'bar');
   31 $js  = $pc->encode($obj);
   32 is($js,'{"foo":"bar"}', '{"foo":"bar"}');
   33 
   34 $js  = q|{"foo":""}|;
   35 $obj = $pc->decode($js);
   36 $js  = $pc->encode($obj);
   37 is($js,'{"foo":""}', '{"foo":""}');
   38 
   39 $js  = q|{"foo":" "}|;
   40 $obj = $pc->decode($js);
   41 $js  = $pc->encode($obj);
   42 is($js,'{"foo":" "}' ,'{"foo":" "}');
   43 
   44 $js  = q|{"foo":"0"}|;
   45 $obj = $pc->decode($js);
   46 $js  = $pc->encode($obj);
   47 is($js,'{"foo":"0"}',q|{"foo":"0"} - autoencode (default)|);
   48 
   49 
   50 $js  = q|{"foo":"0 0"}|;
   51 $obj = $pc->decode($js);
   52 $js  = $pc->encode($obj);
   53 is($js,'{"foo":"0 0"}','{"foo":"0 0"}');
   54 
   55 $js  = q|[1,2,3]|;
   56 $obj = $pc->decode($js);
   57 is($obj->[1],2);
   58 $js  = $pc->encode($obj);
   59 is($js,'[1,2,3]');
   60 
   61 $js = q|{"foo":{"bar":"hoge"}}|;
   62 $obj = $pc->decode($js);
   63 is($obj->{foo}->{bar},'hoge');
   64 $js  = $pc->encode($obj);
   65 is($js,q|{"foo":{"bar":"hoge"}}|);
   66 
   67 $js = q|[{"foo":[1,2,3]},-0.12,{"a":"b"}]|;
   68 $obj = $pc->decode($js);
   69 $js  = $pc->encode($obj);
   70 is($js,q|[{"foo":[1,2,3]},-0.12,{"a":"b"}]|);
   71 
   72 
   73 $obj = ["\x01"];
   74 is($js = $pc->encode($obj),'["\\u0001"]');
   75 $obj = $pc->decode($js);
   76 is($obj->[0],"\x01");
   77 
   78 $obj = ["\e"];
   79 is($js = $pc->encode($obj),'["\\u001b"]');
   80 $obj = $pc->decode($js);
   81 is($obj->[0],"\e");
   82 
   83 $js = '{"id":"}';
   84 eval q{ $pc->decode($js) };
   85 like($@, qr/unexpected end/i);
   86 
   87 $obj = { foo => sub { "bar" } };
   88 eval q{ $js = $pc->encode($obj) };
   89 like($@, qr/JSON can only/i, 'invalid value (coderef)');
   90 
   91 #$obj = { foo => bless {}, "Hoge" };
   92 #eval q{ $js = $pc->encode($obj) };
   93 #like($@, qr/JSON can only/i, 'invalid value (blessd object)');
   94 
   95 $obj = { foo => \$js };
   96 eval q{ $js = $pc->encode($obj) };
   97 like($@, qr/cannot encode reference/i, 'invalid value (ref)');
   98