File: 04_tree.t

package info (click to toggle)
libdata-clone-perl 0.006-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 208 kB
  • sloc: perl: 669; makefile: 8; ansic: 5
file content (53 lines) | stat: -rw-r--r-- 1,095 bytes parent folder | download | duplicates (5)
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
50
51
52
53
#!perl -w

use strict;
use warnings FATAL => 'all';

use Test::More;

use Data::Clone;

use Scalar::Util qw(isweak weaken);
use Data::Dumper;
$Data::Dumper::Indent   = 0;
$Data::Dumper::Sortkeys = 1;

# hash tree

my $parent = {};
my $child  = {};

$child->{parent} = $parent;
$parent->{child} = $child;

weaken $child->{parent};

my $cloned = clone($child);
is Dumper($cloned), Dumper({ parent => undef }), 'tree structure (child)';

$cloned = clone($parent);
is Dumper($cloned), Dumper($parent), 'tree structure (parent)';

cmp_ok $cloned, '==', $cloned->{child}{parent}, 'as circular refs';
ok isweak($cloned->{child}{parent}), 'correctly weaken';

# array tree

$parent = ['is_parent'];
$child  = ['is_child'];

push @{$child},  $parent;
push @{$parent}, $child;

weaken $child->[1];

$cloned = clone($child);
is Dumper($cloned), Dumper(['is_child', undef]), 'array tree (child)';

$cloned = clone($parent);
is Dumper($cloned), Dumper($parent), 'array tree (parent)';

cmp_ok $cloned, '==', $cloned->[1][1], 'as sircular refs';
ok isweak($cloned->[1][1]), 'correctly weaken';

done_testing;