forked from avar/YAML-Syck
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathjson-circular-ref.t
executable file
·42 lines (32 loc) · 1.47 KB
/
json-circular-ref.t
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
use strict;
use warnings;
use Test::More tests => 8;
use JSON::Syck;
{ # Impossible circular blessed references in JSON
my $foo = bless {}, "Foo";
my $bar = bless { foo => $foo }, "Bar";
$foo->{bar} = $bar;
my $result = eval { JSON::Syck::Dump($foo) };
is( $result, undef, "A Structure should come back on a JSON dump with circular blessed references" );
like( $@, qr/^Dumping circular structures is not supported with JSON::Syck/, "Die is thrown when the circular blessed ref happens" );
}
{ # Circular references broken regardless of blessing
my $foo = {};
my $bar = { foo => $foo };
$foo->{bar} = $bar;
my $result = eval { JSON::Syck::Dump($foo) };
is( $result, undef, "A Structure should come back on a JSON dump with duplicate references" );
like( $@, qr/^Dumping circular structures is not supported with JSON::Syck/, "Die is thrown when the circular ref happens" );
}
{
my $foo = {};
my $result = eval { JSON::Syck::Dump( [ $foo, $foo ] ) };
is( $result, '[{},{}]', "A Structure should come back on a JSON dump with duplicate references" );
is( $@, '', "No die is thrown when the circular ref happens" );
}
{
my $foo = { 'a' => [ 1, 2 ] };
my $result = eval { JSON::Syck::Dump( [ $foo, $foo ] ) };
is( $result, '[{"a":[1,2]},{"a":[1,2]}]', "A Complex structure should come back on a JSON dump with duplicate references" );
is( $@, '', "No die is thrown when the circular ref happens" );
}