File: 26_duplicate.t

package info (click to toggle)
libcpanel-json-xs-perl 4.39-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,872 kB
  • sloc: perl: 1,165; makefile: 8
file content (49 lines) | stat: -rw-r--r-- 1,890 bytes parent folder | download | duplicates (2)
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
use strict;
use Test::More tests => 12;
use Cpanel::JSON::XS;

my $json = Cpanel::JSON::XS->new;

# disallow dupkeys
ok (!eval { $json->decode ('{"a":"b","a":"c"}') }); # y_object_duplicated_key.json
ok (!eval { $json->decode ('{"a":"b","a":"b"}') }); # y_object_duplicated_key_and_value.json

# relaxed allows dupkeys
$json->relaxed;
# y_object_duplicated_key.json
is (encode_json ($json->decode ('{"a":"b","a":"c"}')), '{"a":"c"}', 'relaxed');
# y_object_duplicated_key_and_value.json
is (encode_json ($json->decode ('{"a":"b","a":"b"}')), '{"a":"b"}', 'relaxed');

# turning off relaxed disallows dupkeys
$json->relaxed(0);
$json->allow_dupkeys; # but turn it on
is (encode_json ($json->decode ('{"a":"b","a":"c"}')), '{"a":"c"}', 'allow_dupkeys');
is (encode_json ($json->decode ('{"a":"b","a":"b"}')), '{"a":"b"}', 'allow_dupkeys');

# disallow dupkeys explicitly
$json->allow_dupkeys(0);
eval { $json->decode ('{"a":"b","a":"c"}') };
like ($@, qr/^Duplicate keys not allowed/, 'allow_dupkeys(0)');

# disallow dupkeys explicitly with relaxed
$json->relaxed;
$json->allow_dupkeys(0);
eval { $json->decode ('{"a":"b","a":"c"}') }; # the XS slow path
like ($@, qr/^Duplicate keys not allowed/, 'relaxed and allow_dupkeys(0)');

# allow dupkeys
$json->allow_dupkeys;
$json->relaxed(0); # tuning off relaxed needs to turn off dupkeys
eval { $json->decode ('{"a":"b","a":"c"}') };
like ($@, qr/^Duplicate keys not allowed/, 'relaxed(0)');

# a private extension (GH #193)
$json->allow_dupkeys(0);
$json->dupkeys_as_arrayref; # should turn on dupkeys
is (encode_json ($json->decode ('{"a":"b","a":"c"}')), '{"a":["b","c"]}',
    'dupkeys_as_arrayref');
is (encode_json ($json->decode ('{"a":["b"],"a":"c"}')), '{"a":[["b"],"c"]}',
    'dupkeys_as_arrayref to []');
is (encode_json ($json->decode ('{"a":["b","c"],"a":["c"]}')), '{"a":[["b","c"],["c"]]}',
    'dupkeys_as_arrayref to [[]]');