3676b48dd98eecf1f468bed87a69168f36fc9bf8
[dbsrgits/DBM-Deep.git] / t / 39_singletons.t
1 use strict;
2 use Test::More tests => 11;
3 use Test::Deep;
4 use t::common qw( new_fh );
5
6 use_ok( 'DBM::Deep' );
7
8 {
9     my ($fh, $filename) = new_fh();
10     my $db = DBM::Deep->new(
11         file => $filename,
12         locking => 1,
13         autoflush => 1,
14     );
15
16     $db->{a} = 1;
17     $db->{foo} = { a => 'b' };
18     my $x = $db->{foo};
19     my $y = $db->{foo};
20
21     is( $x, $y, "The references are the same" );
22
23     delete $db->{foo};
24     is( $x, undef, "After deleting the DB location, external references are also undef (\$x)" );
25     is( $y, undef, "After deleting the DB location, external references are also undef (\$y)" );
26     is( $x + 0, undef, "DBM::Deep::Null can be added to." );
27     is( $y + 0, undef, "DBM::Deep::Null can be added to." );
28     is( $db->{foo}, undef, "The {foo} location is also undef." );
29
30     # These shenanigans work to get another hashref
31     # into the same data location as $db->{foo} was.
32     $db->{foo} = {};
33     delete $db->{foo};
34     $db->{foo} = {};
35     $db->{bar} = {};
36
37     is( $x, undef, "After re-assigning to {foo}, external references to old values are still undef (\$x)" );
38     is( $y, undef, "After re-assigning to {foo}, external references to old values are still undef (\$y)" );
39 }
40
41 SKIP: {
42     skip "What do we do with external references and txns?", 2;
43     my ($fh, $filename) = new_fh();
44     my $db = DBM::Deep->new(
45         file => $filename,
46         locking => 1,
47         autoflush => 1,
48         num_txns => 2,
49     );
50
51     $db->{foo} = { a => 'b' };
52     my $x = $db->{foo};
53
54     $db->begin_work;
55     
56         $db->{foo} = { c => 'd' };
57         my $y = $db->{foo};
58
59         # XXX What should happen here with $x and $y?
60         is( $x, $y );
61         is( $x->{c}, 'd' );
62
63     $db->rollback;
64 }