Tagged 0.98
[dbsrgits/DBM-Deep.git] / t / 27_filehandle.t
CommitLineData
714618f0 1##
2# DBM::Deep Test
3##
4use strict;
753e18da 5use Test::More tests => 7;
6use Test::Exception;
714618f0 7
8use DBM::Deep;
9
10open(FILE, "t/27_filehandle.t.db") || die("Can't open t/27_filehandle.t.db\n");
11
12my $db;
13
14# test if we can open and read a db using its filehandle
15
16ok(($db = DBM::Deep->new(fh => *FILE)), "open db in filehandle");
17ok($db->{hash}->{foo}->[1] eq 'b', "and get at stuff in the database");
18
19undef $db;
20close(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 ...
24open(FILE, "t/28_DATA.t") || die("Can't open t/28_DATA.t\n");
25while(my $line = <FILE>) {
26 last if($line =~ /^__DATA__/);
27}
28my $offset = tell(FILE);
29close(FILE);
30
753e18da 31open(FILE, '<', "t/28_DATA.t");
714618f0 32ok(($db = DBM::Deep->new(fh => *FILE, file_offset => $offset)), "open db in filehandle with offset");
33ok($db->{hash}->{foo}->[1] eq 'b', "and get at stuff in the database");
34
753e18da 35ok( !$db->{foo}, "foo doesn't exist yet" );
36throws_ok {
37 $db->{foo} = 1;
38} qr/Cannot write to a readonly filehandle/, "Can't write to a read-only filehandle";
39ok( !$db->{foo}, "foo doesn't exist yet" );