r8199@h460878c2 (orig r10013): rkinyon | 2007-09-28 12:05:34 -0400
[dbsrgits/DBM-Deep.git] / t / 31_references.t
1 use strict;
2
3 use Test::More tests => 16;
4 use Test::Exception;
5 use t::common qw( new_fh );
6
7 use_ok( 'DBM::Deep' );
8
9 my ($fh, $filename) = new_fh();
10 my $db = DBM::Deep->new( $filename );
11
12 my %hash = (
13     foo => 1,
14     bar => [ 1 .. 3 ],
15     baz => { a => 42 },
16 );
17
18 $db->{hash} = \%hash;
19 isa_ok( tied(%hash), 'DBM::Deep::Hash' );
20
21 is( $db->{hash}{foo}, 1 );
22 is_deeply( $db->{hash}{bar}, [ 1 .. 3 ] );
23 is_deeply( $db->{hash}{baz}, { a => 42 } );
24
25 $hash{foo} = 2;
26 is( $db->{hash}{foo}, 2 );
27
28 $hash{bar}[1] = 90;
29 is( $db->{hash}{bar}[1], 90 );
30
31 $hash{baz}{b} = 33;
32 is( $db->{hash}{baz}{b}, 33 );
33
34 my @array = (
35     1, [ 1 .. 3 ], { a => 42 },
36 );
37
38 $db->{array} = \@array;
39 isa_ok( tied(@array), 'DBM::Deep::Array' );
40
41 is( $db->{array}[0], 1 );
42 is_deeply( $db->{array}[1], [ 1 .. 3 ] );
43 is_deeply( $db->{array}[2], { a => 42 } );
44
45 $array[0] = 2;
46 is( $db->{array}[0], 2 );
47
48 $array[1][2] = 9;
49 is( $db->{array}[1][2], 9 );
50
51 $array[2]{b} = 'floober';
52 is( $db->{array}[2]{b}, 'floober' );
53
54 my %hash2 = ( abc => [ 1 .. 3 ] );
55 $array[3] = \%hash2;
56
57 $hash2{ def } = \%hash;
58 is( $array[3]{def}{foo}, 2 );