"Fossies" - the Fresh Open Source Software Archive

Member "perl-5.32.1/cpan/JSON-PP/t/003_types.t" (18 Dec 2020, 2409 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 # copied over from JSON::XS and modified to use JSON::PP
    2 
    3 use strict;
    4 use Test::More;
    5 BEGIN { plan tests => 76 + 2 };
    6 
    7 BEGIN { $ENV{PERL_JSON_BACKEND} = 0; }
    8 
    9 use utf8;
   10 use JSON::PP;
   11 
   12 
   13 ok (!defined JSON::PP->new->allow_nonref (1)->decode ('null'));
   14 ok (JSON::PP->new->allow_nonref (1)->decode ('true') == 1);
   15 ok (JSON::PP->new->allow_nonref (1)->decode ('false') == 0);
   16 
   17 my $true  = JSON::PP->new->allow_nonref (1)->decode ('true');
   18 ok ($true eq 1);
   19 ok (JSON::PP::is_bool $true);
   20 my $false = JSON::PP->new->allow_nonref (1)->decode ('false');
   21 ok ($false == !$true);
   22 ok (JSON::PP::is_bool $false);
   23 ok (++$false == 1);
   24 ok (!JSON::PP::is_bool $false);
   25 ok (!JSON::PP::is_bool "JSON::PP::Boolean");
   26 ok (!JSON::PP::is_bool {}); # GH-34
   27 
   28 ok (JSON::PP->new->allow_nonref (1)->decode ('5') == 5);
   29 ok (JSON::PP->new->allow_nonref (1)->decode ('-5') == -5);
   30 ok (JSON::PP->new->allow_nonref (1)->decode ('5e1') == 50);
   31 ok (JSON::PP->new->allow_nonref (1)->decode ('-333e+0') == -333);
   32 ok (JSON::PP->new->allow_nonref (1)->decode ('2.5') == 2.5);
   33 
   34 ok (JSON::PP->new->allow_nonref (1)->decode ('""') eq "");
   35 ok ('[1,2,3,4]' eq encode_json decode_json ('[1,2, 3,4]'));
   36 ok ('[{},[],[],{}]' eq encode_json decode_json ('[{},[], [ ] ,{ }]'));
   37 ok ('[{"1":[5]}]' eq encode_json [{1 => [5]}]);
   38 ok ('{"1":2,"3":4}' eq JSON::PP->new->canonical (1)->encode (decode_json '{ "1" : 2, "3" : 4 }'));
   39 ok ('{"1":2,"3":1.2}' eq JSON::PP->new->canonical (1)->encode (decode_json '{ "1" : 2, "3" : 1.2 }'));
   40 
   41 ok ('[true]'  eq encode_json [JSON::PP::true]);
   42 ok ('[false]' eq encode_json [JSON::PP::false]);
   43 ok ('[true]'  eq encode_json [\1]);
   44 ok ('[false]' eq encode_json [\0]);
   45 ok ('[null]'  eq encode_json [undef]);
   46 ok ('[true]'  eq encode_json [JSON::PP::true]);
   47 ok ('[false]' eq encode_json [JSON::PP::false]);
   48 
   49 for my $v (1, 2, 3, 5, -1, -2, -3, -4, 100, 1000, 10000, -999, -88, -7, 7, 88, 999, -1e5, 1e6, 1e7, 1e8) {
   50    ok ($v == ((decode_json "[$v]")->[0]));
   51    ok ($v == ((decode_json encode_json [$v])->[0]));
   52 }
   53 
   54 ok (30123 == ((decode_json encode_json [30123])->[0]));
   55 ok (32123 == ((decode_json encode_json [32123])->[0]));
   56 ok (32456 == ((decode_json encode_json [32456])->[0]));
   57 ok (32789 == ((decode_json encode_json [32789])->[0]));
   58 ok (32767 == ((decode_json encode_json [32767])->[0]));
   59 ok (32768 == ((decode_json encode_json [32768])->[0]));
   60 
   61 my @sparse; @sparse[0,3] = (1, 4);
   62 ok ("[1,null,null,4]" eq encode_json \@sparse);
   63