Added test to verify no writing to a readonly file
[dbsrgits/DBM-Deep.git] / t / 27_filehandle.t
1 ##
2 # DBM::Deep Test
3 ##
4 use strict;
5 use Test::More tests => 7;
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
19 undef $db;
20 close(FILE);
21
22 # now the same, but with an offset into the file.  Use the database that's
23 # embedded in the test for the DATA filehandle.  First, find the database ...
24 open(FILE, "t/28_DATA.t") || die("Can't open t/28_DATA.t\n");
25 while(my $line = <FILE>) {
26     last if($line =~ /^__DATA__/);
27 }
28 my $offset = tell(FILE);
29 close(FILE);
30
31 open(FILE, '<', "t/28_DATA.t");
32 ok(($db = DBM::Deep->new(fh => *FILE, file_offset => $offset)), "open db in filehandle with offset");
33 ok($db->{hash}->{foo}->[1] eq 'b', "and get at stuff in the database");
34
35 ok( !$db->{foo}, "foo doesn't exist yet" );
36 throws_ok {
37     $db->{foo} = 1;
38 } qr/Cannot write to a readonly filehandle/, "Can't write to a read-only filehandle";
39 ok( !$db->{foo}, "foo doesn't exist yet" );