68162d1f211791f013970802ce5e2829850da74d
[dbsrgits/DBM-Deep.git] / t / 47_odd_reference_behaviors.t
1 use strict;
2 use warnings FATAL => 'all';
3
4 use Test::More;
5 use Test::Exception;
6 use Test::Deep;
7
8 use t::common qw( new_dbm );
9
10 use_ok( 'DBM::Deep' );
11
12 # This is bug #34819, reported by EJS
13 {
14     my $dbm_factory = new_dbm();
15     while ( my $dbm_maker = $dbm_factory->() ) {
16         my $db = $dbm_maker->();
17
18         my $bar = bless { foo => 'ope' }, 'Foo';
19
20         eval {
21             $db->{bar} = $bar;
22             $db->{bar} = $bar;
23         };
24
25         ok(!$@, "repeated object assignment");
26         isa_ok($db->{bar}, 'Foo');
27     }
28 }
29
30 # This is bug #29957, reported by HANENKAMP
31 {
32     my $dbm_factory = new_dbm();
33     while ( my $dbm_maker = $dbm_factory->() ) {
34         my $db = $dbm_maker->();
35
36         $db->{foo} = [];
37
38         for my $value ( 1 .. 3 ) {
39             lives_ok {
40                 my $ref = $db->{foo};
41                 push @$ref, $value;
42                 $db->{foo} = $ref;
43             } "Successfully added value $value";
44         }
45
46         cmp_deeply( [1,2,3], noclass($db->{foo}), "Everything looks ok" );
47     }
48 }
49
50 # This is bug #33863, reported by PJS
51 {
52     my $dbm_factory = new_dbm();
53     while ( my $dbm_maker = $dbm_factory->() ) {
54         my $db = $dbm_maker->();
55
56         $db->{foo} = [ 42 ];
57         my $foo = shift @{ $db->{foo} };
58         cmp_ok( @{ $db->{foo} }, '==', 0, "Shifting a scalar leaves no values" );
59         cmp_ok( $foo, '==', 42, "... And the value is correct." );
60
61         $db->{bar} = [ [] ];
62         my $bar = shift @{ $db->{bar} };
63         cmp_ok( @{ $db->{bar} }, '==', 0, "Shifting an arrayref leaves no values" );
64
65         $db->{baz} = { foo => [ 1 .. 3 ] };
66         $db->{baz2} = [ $db->{baz} ];
67         my $baz2 = shift @{ $db->{baz2} };
68         cmp_ok( @{ $db->{baz2} }, '==', 0, "Shifting an arrayref leaves no values" );
69         ok( exists $db->{baz}{foo} );
70         ok( exists $baz2->{foo} );
71     }
72 }
73
74 done_testing;