diff options
author | TizenOpenSource <tizenopensrc@samsung.com> | 2023-12-28 17:46:21 +0900 |
---|---|---|
committer | TizenOpenSource <tizenopensrc@samsung.com> | 2023-12-28 17:46:21 +0900 |
commit | 782d8bd355c14b4617bde530e43d31b89ec298f5 (patch) | |
tree | 8c4fb6771f3e13e1e98543e5bf179c989659d5b9 /t | |
parent | 486774dd555cba55fb31657143ea9cc9ad64a561 (diff) | |
download | perl-json-782d8bd355c14b4617bde530e43d31b89ec298f5.tar.gz perl-json-782d8bd355c14b4617bde530e43d31b89ec298f5.tar.bz2 perl-json-782d8bd355c14b4617bde530e43d31b89ec298f5.zip |
Imported Upstream version 4.10upstream/4.10upstream
Diffstat (limited to 't')
-rw-r--r-- | t/03_types.t | 10 | ||||
-rw-r--r-- | t/core_bools.t | 86 | ||||
-rw-r--r-- | t/e02_bool.t | 10 |
3 files changed, 101 insertions, 5 deletions
diff --git a/t/03_types.t b/t/03_types.t index 1037a7c..83eff94 100644 --- a/t/03_types.t +++ b/t/03_types.t @@ -3,7 +3,7 @@ use strict; use warnings; use Test::More; -BEGIN { plan tests => 76 + 2 }; +BEGIN { plan tests => 78 + 2 }; BEGIN { $ENV{PERL_JSON_BACKEND} ||= "JSON::backportPP"; } @@ -47,6 +47,14 @@ ok ('[null]' eq encode_json [undef]); ok ('[true]' eq encode_json [JSON::true]); ok ('[false]' eq encode_json [JSON::false]); +SKIP: { + skip "core booleans not supported", 2 + unless JSON->backend->can("CORE_BOOL") && JSON->backend->CORE_BOOL; + + ok ('[true]' eq encode_json [!!1]); + ok ('[false]' eq encode_json [!!0]); +} + for my $v (1, 2, 3, 5, -1, -2, -3, -4, 100, 1000, 10000, -999, -88, -7, 7, 88, 999, -1e5, 1e6, 1e7, 1e8) { ok ($v == ((decode_json "[$v]")->[0])); ok ($v == ((decode_json encode_json [$v])->[0])); diff --git a/t/core_bools.t b/t/core_bools.t new file mode 100644 index 0000000..8c714a1 --- /dev/null +++ b/t/core_bools.t @@ -0,0 +1,86 @@ +use strict; +use warnings; +BEGIN { $ENV{PERL_JSON_BACKEND} ||= "JSON::backportPP"; } + +use JSON; +use Test::More; +BEGIN { + # this is only for JSON.pm + plan skip_all => 'no support for core boolean options' + unless JSON->backend->can('CORE_BOOL'); +} + +plan tests => 24; + +my $json = JSON->new; + +is $json->get_core_bools, !!0, 'core_bools initially false'; + +$json->boolean_values(!!0, !!1); +SKIP: { + skip "core_bools option doesn't register as true for core bools without core boolean support", 1 + unless JSON->backend->CORE_BOOL; + + is $json->get_core_bools, !!1, 'core_bools true when setting bools to core bools'; +} + +$json->boolean_values(!!1, !!0); +is $json->get_core_bools, !!0, 'core_bools false when setting bools to anything other than correct core bools'; + +my $ret = $json->core_bools; +is $ret, $json, + "returns the same object"; + +my ($new_false, $new_true) = $json->get_boolean_values; + +# ensure this registers as true on older perls where the boolean values +# themselves can't be tracked. +is $json->get_core_bools, !!1, 'core_bools true when setting core_bools'; + +ok defined $new_true, "core true value is defined"; +ok defined $new_false, "core false value is defined"; + +ok !ref $new_true, "core true value is not blessed"; +ok !ref $new_false, "core falase value is not blessed"; + +{ + my @warnings; + local $SIG{__WARN__} = sub { + push @warnings, @_; + warn @_; + }; + + cmp_ok $new_true, 'eq', '1', 'core true value is "1"'; + cmp_ok $new_true, '==', 1, 'core true value is 1'; + + cmp_ok $new_false, 'eq', '', 'core false value is ""'; + cmp_ok $new_false, '==', 0, 'core false value is 0'; + + is scalar @warnings, 0, 'no warnings'; +} + +SKIP: { + skip "core boolean support needed to detect core booleans", 4 + unless JSON->backend->CORE_BOOL; + ok JSON::is_bool($new_true), 'core true is a boolean'; + ok JSON::is_bool($new_false), 'core false is a boolean'; + + ok builtin::is_bool($new_true), 'core true is a core boolean'; + ok builtin::is_bool($new_false), 'core false is a core boolean'; +} + +my $should_true = $json->allow_nonref(1)->decode('true'); +my $should_false = $json->allow_nonref(1)->decode('false'); + +ok !ref $should_true && $should_true, "JSON true turns into an unblessed true value"; +ok !ref $should_false && !$should_false, "JSON false turns into an unblessed false value"; + +SKIP: { + skip "core boolean support needed to detect core booleans", 4 + unless JSON->backend->CORE_BOOL; + ok JSON::is_bool($should_true), 'decoded true is a boolean'; + ok JSON::is_bool($should_false), 'decoded false is a boolean'; + + ok JSON::is_bool($should_true), 'decoded true is a core boolean'; + ok JSON::is_bool($should_false), 'decoded false is a core boolean'; +} diff --git a/t/e02_bool.t b/t/e02_bool.t index 06e8dd6..bbfd9fb 100644 --- a/t/e02_bool.t +++ b/t/e02_bool.t @@ -17,16 +17,18 @@ my $not_not_a_number_is_a_number = ( ($json->backend->isa('JSON::PP') && ($JSON::PP::Boolean::VERSION || $JSON::backportPP::Boolean::VERSION)) ) ? 1 : 0; -is($json->encode([!1]), '[""]'); +my $core_bool_support = JSON->backend->can("CORE_BOOL") && JSON->backend->CORE_BOOL ? 1 : 0; + +is($json->encode([!1]), $core_bool_support ? '[false]' : '[""]'); if ($not_not_a_number_is_a_number) { -is($json->encode([!!2]), '[1]'); +is($json->encode([!!2]), $core_bool_support ? '[true]' : '[1]'); } else { is($json->encode([!!2]), '["1"]'); } -is($json->encode([ 'a' eq 'b' ]), '[""]'); +is($json->encode([ 'a' eq 'b' ]), $core_bool_support ? '[false]' : '[""]'); if ($not_not_a_number_is_a_number) { -is($json->encode([ 'a' eq 'a' ]), '[1]'); +is($json->encode([ 'a' eq 'a' ]), $core_bool_support ? '[true]' : '[1]'); } else { is($json->encode([ 'a' eq 'a' ]), '["1"]'); } |