Children are now tied directly instead of copied. This makes code behave more as...
[dbsrgits/DBM-Deep.git] / t / 31_references.t
1 ##
2 # DBM::Deep Test
3 ##
4 use strict;
5 use Test::More tests => 16;
6 use Test::Exception;
7 use File::Temp qw( tempfile tempdir );
8 use Fcntl qw( :flock );
9
10 use_ok( 'DBM::Deep' );
11
12 my $dir = tempdir( CLEANUP => 1 );
13 my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir );
14 flock $fh, LOCK_UN;
15 my $db = DBM::Deep->new( $filename );
16
17 my %hash = (
18     foo => 1,
19     bar => [ 1 .. 3 ],
20     baz => { a => 42 },
21 );
22
23 $db->{hash} = \%hash;
24 isa_ok( tied(%hash), 'DBM::Deep::Hash' );
25
26 is( $db->{hash}{foo}, 1 );
27 is_deeply( $db->{hash}{bar}, [ 1 .. 3 ] );
28 is_deeply( $db->{hash}{baz}, { a => 42 } );
29
30 $hash{foo} = 2;
31 is( $db->{hash}{foo}, 2 );
32
33 $hash{bar}[1] = 90;
34 is( $db->{hash}{bar}[1], 90 );
35
36 $hash{baz}{b} = 33;
37 is( $db->{hash}{baz}{b}, 33 );
38
39 my @array = (
40     1, [ 1 .. 3 ], { a => 42 },
41 );
42
43 $db->{array} = \@array;
44 isa_ok( tied(@array), 'DBM::Deep::Array' );
45
46 is( $db->{array}[0], 1 );
47 is_deeply( $db->{array}[1], [ 1 .. 3 ] );
48 is_deeply( $db->{array}[2], { a => 42 } );
49
50 $array[0] = 2;
51 is( $db->{array}[0], 2 );
52
53 $array[1][2] = 9;
54 is( $db->{array}[1][2], 9 );
55
56 $array[2]{b} = 'floober';
57 is( $db->{array}[2]{b}, 'floober' );
58
59 my %hash2 = ( abc => [ 1 .. 3 ] );
60 $array[3] = \%hash2;
61 $hash2{ def } = \%hash;
62
63 is( $array[3]{def}{foo}, 2 );