File: collision.t

package info (click to toggle)
libjson-parse-perl 0.62-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 572 kB
  • sloc: ansic: 3,614; perl: 475; makefile: 12
file content (43 lines) | stat: -rw-r--r-- 1,143 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
# This tests what happens when a JSON object has two keys with the
# same string, as in {"a":1,"a":2}. These are called "collisions"
# because the entries for the two bits of the JSON in the storing
# object "collide".

use FindBin '$Bin';
use lib "$Bin";
use JPT;

# We need to do some work with Unicode. This is a core module so it's
# always available.

use Encode 'decode_utf8';

my $j = '{"a":1, "a":2}';
my $p = parse_json ($j);
cmp_ok ($p->{a}, '==', 2, "Test documented hash key collision behaviour");

my $j2 = '{"a":1, "a":2, "a":3, "a":4, "a":5, "a":6, "a":7, "a":8, "a":9, "a":10}';
my $p2 = parse_json ($j2);
cmp_ok ($p2->{a}, '==', 10, "Test documented hash key collision behaviour");

my $focus = '{"hocus":10,"pocus":20,"hocus":30,"focus":40}';
my $jp = JSON::Parse->new ();
$jp->detect_collisions (1);
eval {
    $jp->run ($focus);
};
ok ($@);
like ($@, qr/"hocus"/);

# Test functioning with Unicode strings.

my $yodi = '{"ほかす":10,"ぽかす":20,"ほかす":30,"ふぉかす":40}';
eval {
    $jp->run ($yodi);
};
ok ($@);
my $error = decode_utf8 ($@);
like ($error, qr/"ほかす"/);

note ($error);
done_testing ();