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