Refactored to _descend to fix the recursion bug
[dbsrgits/DBM-Deep.git] / t / 31_references.t
1 use strict;
2 use warnings FATAL => 'all';
3
4 use Test::More;
5 use Test::Deep;
6 use Test::Exception;
7 use t::common qw( new_dbm );
8
9 use_ok( 'DBM::Deep' );
10 my $dbm_factory = new_dbm();
11 while ( my $dbm_maker = $dbm_factory->() ) {
12     my $db = $dbm_maker->();
13
14     my %hash = (
15         foo => 1,
16         bar => [ 1 .. 3 ],
17         baz => { a => 42 },
18     );
19
20     $db->{hash} = \%hash;
21     isa_ok( tied(%hash), 'DBM::Deep::Hash' );
22
23     is( $db->{hash}{foo}, 1 );
24     cmp_deeply( $db->{hash}{bar}, noclass([ 1 .. 3 ]) );
25     cmp_deeply( $db->{hash}{baz}, noclass({ a => 42 }) );
26
27     $hash{foo} = 2;
28     is( $db->{hash}{foo}, 2 );
29
30     $hash{bar}[1] = 90;
31     is( $db->{hash}{bar}[1], 90 );
32
33     $hash{baz}{b} = 33;
34     is( $db->{hash}{baz}{b}, 33 );
35
36     my @array = (
37         1, [ 1 .. 3 ], { a => 42 },
38     );
39
40     $db->{array} = \@array;
41     isa_ok( tied(@array), 'DBM::Deep::Array' );
42
43     is( $db->{array}[0], 1 );
44     cmp_deeply( $db->{array}[1], noclass([ 1 .. 3 ]) );
45     cmp_deeply( $db->{array}[2], noclass({ a => 42 }) );
46
47     $array[0] = 2;
48     is( $db->{array}[0], 2 );
49
50     $array[1][2] = 9;
51     is( $db->{array}[1][2], 9 );
52
53     $array[2]{b} = 'floober';
54     is( $db->{array}[2]{b}, 'floober' );
55
56     my %hash2 = ( abc => [ 1 .. 3 ] );
57     $array[3] = \%hash2;
58
59     $hash2{ def } = \%hash;
60     is( $array[3]{def}{foo}, 2 );
61 }
62
63 done_testing;