summaryrefslogtreecommitdiff
path: root/cpan/JSON-PP/t/118_boolean_values.t
blob: 1a5d175254c0b6f6639891a162c2305fb1face41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use strict;
use Test::More;
BEGIN { $ENV{PERL_JSON_BACKEND} = 0; }
use JSON::PP;

BEGIN { plan skip_all => "requires Perl 5.008 or later" if $] < 5.008 }

#SKIP_ALL_UNLESS_XS4_COMPAT

package #
    Dummy::True;
*Dummy::True:: = *JSON::PP::Boolean::;

package #
    Dummy::False;
*Dummy::False:: = *JSON::PP::Boolean::;

package main;

my $dummy_true = bless \(my $dt = 1), 'Dummy::True';
my $dummy_false = bless \(my $df = 0), 'Dummy::False';

my @tests = ([$dummy_true, $dummy_false, 'Dummy::True', 'Dummy::False']);

# extra boolean classes
if (eval "require boolean; 1") {
    push @tests, [boolean::true(), boolean::false(), 'boolean', 'boolean', 1];
}
if (eval "require JSON::PP; 1") {
    push @tests, [JSON::PP::true(), JSON::PP::false(), 'JSON::PP::Boolean', 'JSON::PP::Boolean'];
}
if (eval "require Data::Bool; 1") {
    push @tests, [Data::Bool::true(), Data::Bool::false(), 'Data::Bool::Impl', 'Data::Bool::Impl'];
}
if (eval "require Types::Serialiser; 1") {
    push @tests, [Types::Serialiser::true(), Types::Serialiser::false(), 'Types::Serialiser::BooleanBase', 'Types::Serialiser::BooleanBase'];
}

plan tests => 13 * @tests;

my $json = JSON::PP->new;
for my $test (@tests) {
    my ($true, $false, $true_class, $false_class, $incompat) = @$test;

    $json->boolean_values($false, $true);
    my ($new_false, $new_true) = $json->get_boolean_values;
    ok defined $new_true, "new true class is defined";
    ok defined $new_false, "new false class is defined";
    ok $new_true->isa($true_class), "new true class is $true_class";
    ok $new_false->isa($false_class), "new false class is $false_class";
    SKIP: {
        skip "$true_class is not compatible with JSON::PP::Boolean", 2 if $incompat;
        ok $new_true->isa('JSON::PP::Boolean'), "new true class is also JSON::PP::Boolean";
        ok $new_false->isa('JSON::PP::Boolean'), "new false class is also JSON::PP::Boolean";
    }

    my $should_true = $json->allow_nonref(1)->decode('true');
    ok $should_true->isa($true_class), "JSON true turns into a $true_class object";

    my $should_false = $json->allow_nonref(1)->decode('false');
    ok $should_false->isa($false_class), "JSON false turns into a $false_class object";

    SKIP: {
        skip "$true_class is not compatible with JSON::PP::Boolean", 2 if $incompat;
        my $should_true_json = eval { $json->allow_nonref(1)->encode($new_true); };
        is $should_true_json => 'true', "A $true_class object turns into JSON true";

        my $should_false_json = eval { $json->allow_nonref(1)->encode($new_false); };
        is $should_false_json => 'false', "A $false_class object turns into JSON false";
    }

    $json->boolean_values();
    ok !$json->get_boolean_values, "reset boolean values";

    $should_true = $json->allow_nonref(1)->decode('true');
    ok $should_true->isa('JSON::PP::Boolean'), "JSON true turns into a JSON::PP::Boolean object";

    $should_false = $json->allow_nonref(1)->decode('false');
    ok $should_false->isa('JSON::PP::Boolean'), "JSON false turns into a JSON::PP::Boolean object";
}