1use strict; 2use warnings; 3use Test::More; 4BEGIN { $ENV{PERL_JSON_BACKEND} = 0; } 5use JSON::PP; 6 7#SKIP_ALL_UNLESS_XS4_COMPAT 8 9package # 10 Dummy::True; 11*Dummy::True:: = *JSON::PP::Boolean::; 12 13package # 14 Dummy::False; 15*Dummy::False:: = *JSON::PP::Boolean::; 16 17package main; 18 19my $dummy_true = bless \(my $dt = 1), 'Dummy::True'; 20my $dummy_false = bless \(my $df = 0), 'Dummy::False'; 21 22my @tests = ([$dummy_true, $dummy_false, 'Dummy::True', 'Dummy::False']); 23 24# extra boolean classes 25if (eval "require boolean; 1") { 26 push @tests, [boolean::true(), boolean::false(), 'boolean', 'boolean', 1]; 27} 28if (eval "require JSON::PP; 1") { 29 push @tests, [JSON::PP::true(), JSON::PP::false(), 'JSON::PP::Boolean', 'JSON::PP::Boolean']; 30} 31if (eval "require Data::Bool; 1") { 32 push @tests, [Data::Bool::true(), Data::Bool::false(), 'Data::Bool::Impl', 'Data::Bool::Impl']; 33} 34if (eval "require Types::Serialiser; 1") { 35 push @tests, [Types::Serialiser::true(), Types::Serialiser::false(), 'Types::Serialiser::BooleanBase', 'Types::Serialiser::BooleanBase']; 36} 37 38plan tests => 15 * @tests; 39 40my $json = JSON::PP->new; 41for my $test (@tests) { 42 my ($true, $false, $true_class, $false_class, $incompat) = @$test; 43 44 my $ret = $json->boolean_values($false, $true); 45 is $ret => $json, "returns the same object"; 46 my ($new_false, $new_true) = $json->get_boolean_values; 47 ok defined $new_true, "new true class is defined"; 48 ok defined $new_false, "new false class is defined"; 49 ok $new_true->isa($true_class), "new true class is $true_class"; 50 ok $new_false->isa($false_class), "new false class is $false_class"; 51 SKIP: { 52 skip "$true_class is not compatible with JSON::PP::Boolean", 2 if $incompat; 53 ok $new_true->isa('JSON::PP::Boolean'), "new true class is also JSON::PP::Boolean"; 54 ok $new_false->isa('JSON::PP::Boolean'), "new false class is also JSON::PP::Boolean"; 55 } 56 57 my $should_true = $json->allow_nonref(1)->decode('true'); 58 ok $should_true->isa($true_class), "JSON true turns into a $true_class object"; 59 60 my $should_false = $json->allow_nonref(1)->decode('false'); 61 ok $should_false->isa($false_class), "JSON false turns into a $false_class object"; 62 63 SKIP: { 64 skip "$true_class is not compatible with JSON::PP::Boolean", 2 if $incompat; 65 my $should_true_json = eval { $json->allow_nonref(1)->encode($new_true); }; 66 is $should_true_json => 'true', "A $true_class object turns into JSON true"; 67 68 my $should_false_json = eval { $json->allow_nonref(1)->encode($new_false); }; 69 is $should_false_json => 'false', "A $false_class object turns into JSON false"; 70 } 71 72 $ret = $json->boolean_values(); 73 is $ret => $json, "returns the same object"; 74 ok !$json->get_boolean_values, "reset boolean values"; 75 76 $should_true = $json->allow_nonref(1)->decode('true'); 77 ok $should_true->isa('JSON::PP::Boolean'), "JSON true turns into a JSON::PP::Boolean object"; 78 79 $should_false = $json->allow_nonref(1)->decode('false'); 80 ok $should_false->isa('JSON::PP::Boolean'), "JSON false turns into a JSON::PP::Boolean object"; 81} 82