xref: /openbsd-src/gnu/usr.bin/perl/cpan/JSON-PP/t/core_bools.t (revision f2a19305cfc49ea4d1a5feb55cd6c283c6f1e031)
1use strict;
2use warnings;
3use JSON::PP;
4use Test::More;
5BEGIN {
6  # this is only for JSON.pm
7  plan skip_all => 'no support for core boolean options'
8    unless JSON::PP->can('CORE_BOOL');
9}
10
11plan tests => 24;
12
13my $json = JSON::PP->new;
14
15is $json->get_core_bools, !!0, 'core_bools initially false';
16
17$json->boolean_values(!!0, !!1);
18SKIP: {
19    skip "core_bools option doesn't register as true for core bools without core boolean support", 1
20        unless JSON::PP::CORE_BOOL;
21
22    is $json->get_core_bools, !!1, 'core_bools true when setting bools to core bools';
23}
24
25$json->boolean_values(!!1, !!0);
26is $json->get_core_bools, !!0, 'core_bools false when setting bools to anything other than correct core bools';
27
28my $ret = $json->core_bools;
29is $ret, $json,
30  "returns the same object";
31
32my ($new_false, $new_true) = $json->get_boolean_values;
33
34# ensure this registers as true on older perls where the boolean values
35# themselves can't be tracked.
36is $json->get_core_bools, !!1, 'core_bools true when setting core_bools';
37
38ok defined $new_true, "core true value is defined";
39ok defined $new_false, "core false value is defined";
40
41ok !ref $new_true, "core true value is not blessed";
42ok !ref $new_false, "core falase value is not blessed";
43
44{
45    my @warnings;
46    local $SIG{__WARN__} = sub {
47        push @warnings, @_;
48        warn @_;
49    };
50
51    cmp_ok $new_true, 'eq', '1', 'core true value is "1"';
52    cmp_ok $new_true, '==', 1, 'core true value is 1';
53
54    cmp_ok $new_false, 'eq', '', 'core false value is ""';
55    cmp_ok $new_false, '==', 0, 'core false value is 0';
56
57    is scalar @warnings, 0, 'no warnings';
58}
59
60SKIP: {
61    skip "core boolean support needed to detect core booleans", 4
62        unless JSON::PP::CORE_BOOL;
63    BEGIN { JSON::PP::CORE_BOOL and warnings->unimport(qw(experimental::builtin)) }
64    ok JSON::PP::is_bool($new_true), 'core true is a boolean';
65    ok JSON::PP::is_bool($new_false), 'core false is a boolean';
66
67    ok builtin::is_bool($new_true), 'core true is a core boolean';
68    ok builtin::is_bool($new_false), 'core false is a core boolean';
69}
70
71my $should_true = $json->allow_nonref(1)->decode('true');
72my $should_false = $json->allow_nonref(1)->decode('false');
73
74ok !ref $should_true && $should_true, "JSON true turns into an unblessed true value";
75ok !ref $should_false && !$should_false, "JSON false turns into an unblessed false value";
76
77SKIP: {
78    skip "core boolean support needed to detect core booleans", 4
79        unless JSON::PP::CORE_BOOL;
80    ok JSON::PP::is_bool($should_true), 'decoded true is a boolean';
81    ok JSON::PP::is_bool($should_false), 'decoded false is a boolean';
82
83    ok JSON::PP::is_bool($should_true), 'decoded true is a core boolean';
84    ok JSON::PP::is_bool($should_false), 'decoded false is a core boolean';
85}
86