Debian Codegraph @

t/118_boolean_values.t

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