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