Added better tests of existence around failure to write to readonly handle
[dbsrgits/DBM-Deep.git] / t / 27_filehandle.t
1 ##
2 # DBM::Deep Test
3 ##
4 use strict;
5 use Test::More tests => 9;
6 use Test::Exception;
7
8 use DBM::Deep;
9
10 open(FILE, "t/27_filehandle.t.db") || die("Can't open t/27_filehandle.t.db\n");
11
12 my $db;
13
14 # test if we can open and read a db using its filehandle
15
16 ok(($db = DBM::Deep->new(fh => *FILE)), "open db in filehandle");
17 ok($db->{hash}->{foo}->[1] eq 'b', "and get at stuff in the database");
18 throws_ok {
19     $db->{foo} = 1;
20 } qr/Cannot write to a readonly filehandle/, "Can't write to a read-only filehandle";
21 ok( !$db->exists( 'foo' ), "foo doesn't exist" );
22
23 undef $db;
24 close(FILE);
25
26 # now the same, but with an offset into the file.  Use the database that's
27 # embedded in the test for the DATA filehandle.  First, find the database ...
28 open(FILE, "t/28_DATA.t") || die("Can't open t/28_DATA.t\n");
29 while(my $line = <FILE>) {
30     last if($line =~ /^__DATA__/);
31 }
32 my $offset = tell(FILE);
33 close(FILE);
34
35 open(FILE, "t/28_DATA.t");
36 ok(($db = DBM::Deep->new(fh => *FILE, file_offset => $offset)), "open db in filehandle with offset");
37 ok($db->{hash}->{foo}->[1] eq 'b', "and get at stuff in the database");
38
39 ok( !$db->exists( 'foo' ), "foo doesn't exist yet" );
40 throws_ok {
41     $db->{foo} = 1;
42 } qr/Cannot write to a readonly filehandle/, "Can't write to a read-only filehandle";
43 ok( !$db->exists( 'foo' ), "foo doesn't exist" );