5 use Test::More tests => 11;
7 use File::Temp qw( tempfile tempdir );
8 use Fcntl qw( :flock );
10 use_ok( 'DBM::Deep' );
12 my $dir = tempdir( CLEANUP => 1 );
13 my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir );
16 # Create the datafile to be used
18 my $db = DBM::Deep->new( $filename );
19 $db->{hash} = { foo => [ 'a' .. 'c' ] };
23 open(FILE, $filename) || die("Can't open '$filename' for reading: $!\n");
27 # test if we can open and read a db using its filehandle
29 ok(($db = DBM::Deep->new(fh => *FILE)), "open db in filehandle");
30 ok($db->{hash}->{foo}->[1] eq 'b', "and get at stuff in the database");
33 } qr/Cannot write to a readonly filehandle/,
34 "Can't write to a read-only filehandle";
35 ok( !$db->exists( 'foo' ), "foo doesn't exist" );
37 my $db_obj = $db->_get_self;
38 ok( $db_obj->_root->{inode}, "The inode has been set" );
43 # now the same, but with an offset into the file. Use the database that's
44 # embedded in the test for the DATA filehandle. First, find the database ...
45 open(FILE, "t/28_DATA.t") || die("Can't open t/28_DATA.t\n");
46 while(my $line = <FILE>) {
47 last if($line =~ /^__DATA__/);
49 my $offset = tell(FILE);
53 skip "File format changed ... gah!", 5;
54 open(FILE, "t/28_DATA.t");
58 ok(($db = DBM::Deep->new(fh => *FILE, file_offset => $offset)), "open db in filehandle with offset");
60 ok($db->{hash}->{foo}->[1] eq 'b', "and get at stuff in the database");
62 ok( !$db->exists( 'foo' ), "foo doesn't exist yet" );
65 } qr/Cannot write to a readonly filehandle/, "Can't write to a read-only filehandle";
66 ok( !$db->exists( 'foo' ), "foo doesn't exist" );