Added guard to make sure values that cannot be read correctly aren't stored, plus...
[dbsrgits/DBM-Deep.git] / t / 26_scalar_ref.t
1 use strict;
2
3 use Test::More tests => 10;
4 use Test::Exception;
5 use File::Temp qw( tempfile tempdir );
6
7 use_ok( 'DBM::Deep' );
8
9 my $dir = tempdir( CLEANUP => 1 );
10 my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir );
11
12 my $x = 25;
13 {
14     my $db = DBM::Deep->new( $filename );
15
16     throws_ok {
17         $db->{scalarref} = \$x;
18     } qr/Storage of variables of type 'SCALAR' is not supported/,
19     'Storage of scalar refs not supported';
20
21     throws_ok {
22         $db->{scalarref} = \\$x;
23     } qr/Storage of variables of type 'REF' is not supported/,
24     'Storage of ref refs not supported';
25
26     throws_ok {
27         $db->{scalarref} = sub { 1 };
28     } qr/Storage of variables of type 'CODE' is not supported/,
29     'Storage of code refs not supported';
30
31     throws_ok {
32         $db->{scalarref} = $db->_get_self->_fh;
33     } qr/Storage of variables of type 'GLOB' is not supported/,
34     'Storage of glob refs not supported';
35
36     $db->{scalar} = $x;
37     TODO: {
38         todo_skip "Refs to DBM::Deep objects aren't implemented yet", 2;
39         lives_ok {
40             $db->{selfref} = \$db->{scalar};
41         } "Refs to DBM::Deep objects are ok";
42
43         is( ${$db->{selfref}}, $x, "A ref to a DBM::Deep object is ok" );
44     }
45 }
46
47 {
48     my $db = DBM::Deep->new( $filename );
49
50     is( $db->{scalar}, $x, "Scalar retrieved ok" );
51     TODO: {
52         todo_skip "Refs to DBM::Deep objects aren't implemented yet", 2;
53         is( ${$db->{scalarref}}, 30, "Scalarref retrieved ok" );
54         is( ${$db->{selfref}}, 26, "Scalarref to stored scalar retrieved ok" );
55     }
56 }