1use strict; 2use Test::More; 3BEGIN { $ENV{PERL_JSON_BACKEND} = 0; } 4use JSON::PP; 5 6BEGIN { plan skip_all => "requires Perl 5.008 or later" if $] < 5.008 } 7 8#SKIP_ALL_UNLESS_XS4_COMPAT 9 10package # 11 Dummy::True; 12*Dummy::True:: = *JSON::PP::Boolean::; 13 14package # 15 Dummy::False; 16*Dummy::False:: = *JSON::PP::Boolean::; 17 18package main; 19 20my $dummy_true = bless \(my $dt = 1), 'Dummy::True'; 21my $dummy_false = bless \(my $df = 0), 'Dummy::False'; 22 23my @tests = ([$dummy_true, $dummy_false, 'Dummy::True', 'Dummy::False']); 24 25# extra boolean classes 26if (eval "require boolean; 1") { 27 push @tests, [boolean::true(), boolean::false(), 'boolean', 'boolean', 1]; 28} 29if (eval "require JSON::PP; 1") { 30 push @tests, [JSON::PP::true(), JSON::PP::false(), 'JSON::PP::Boolean', 'JSON::PP::Boolean']; 31} 32if (eval "require Data::Bool; 1") { 33 push @tests, [Data::Bool::true(), Data::Bool::false(), 'Data::Bool::Impl', 'Data::Bool::Impl']; 34} 35if (eval "require Types::Serialiser; 1") { 36 push @tests, [Types::Serialiser::true(), Types::Serialiser::false(), 'Types::Serialiser::BooleanBase', 'Types::Serialiser::BooleanBase']; 37} 38 39plan tests => 13 * @tests; 40 41my $json = JSON::PP->new; 42for my $test (@tests) { 43 my ($true, $false, $true_class, $false_class, $incompat) = @$test; 44 45 $json->boolean_values($false, $true); 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 $json->boolean_values(); 73 ok !$json->get_boolean_values, "reset boolean values"; 74 75 $should_true = $json->allow_nonref(1)->decode('true'); 76 ok $should_true->isa('JSON::PP::Boolean'), "JSON true turns into a JSON::PP::Boolean object"; 77 78 $should_false = $json->allow_nonref(1)->decode('false'); 79 ok $should_false->isa('JSON::PP::Boolean'), "JSON false turns into a JSON::PP::Boolean object"; 80} 81