"Fossies" - the Fresh Open Source Software Archive

Member "perl-5.32.1/cpan/JSON-PP/t/012_blessed.t" (18 Dec 2020, 1444 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 => 16 };
    6 
    7 BEGIN { $ENV{PERL_JSON_BACKEND} = 0; }
    8 
    9 use JSON::PP;
   10 
   11 
   12 my $o1 = bless { a => 3 }, "XX";
   13 my $o2 = bless \(my $dummy = 1), "YY";
   14 
   15 sub XX::TO_JSON {
   16    {'__',""}
   17 }
   18 
   19 my $js = JSON::PP->new;
   20 
   21 eval { $js->encode ($o1) }; ok ($@ =~ /allow_blessed/);
   22 eval { $js->encode ($o2) }; ok ($@ =~ /allow_blessed/);
   23 $js->allow_blessed;
   24 ok ($js->encode ($o1) eq "null");
   25 ok ($js->encode ($o2) eq "null");
   26 $js->convert_blessed;
   27 ok ($js->encode ($o1) eq '{"__":""}');
   28 ok ($js->encode ($o2) eq "null");
   29 
   30 $js->filter_json_object (sub { 5 });
   31 $js->filter_json_single_key_object (a => sub { shift });
   32 $js->filter_json_single_key_object (b => sub { 7 });
   33 
   34 ok ("ARRAY" eq ref $js->decode ("[]"));
   35 ok (5 eq join ":", @{ $js->decode ('[{}]') });
   36 ok (6 eq join ":", @{ $js->decode ('[{"a":6}]') });
   37 ok (5 eq join ":", @{ $js->decode ('[{"a":4,"b":7}]') });
   38 
   39 $js->filter_json_object;
   40 ok (7 == $js->decode ('[{"a":4,"b":7}]')->[0]{b});
   41 ok (3 eq join ":", @{ $js->decode ('[{"a":3}]') });
   42 
   43 $js->filter_json_object (sub { });
   44 ok (7 == $js->decode ('[{"a":4,"b":7}]')->[0]{b});
   45 ok (9 eq join ":", @{ $js->decode ('[{"a":9}]') });
   46 
   47 $js->filter_json_single_key_object ("a");
   48 ok (4 == $js->decode ('[{"a":4}]')->[0]{a});
   49 
   50 $js->filter_json_single_key_object (a => sub { return; }); # sub {} is not suitable for Perl 5.6
   51 ok (4 == $js->decode ('[{"a":4}]')->[0]{a});
   52