New testing feature that allows specification of the workdir for the tests
[dbsrgits/DBM-Deep.git] / t / 27_filehandle.t
CommitLineData
714618f0 1##
2# DBM::Deep Test
3##
4use strict;
2a81bf9e 5use Test::More tests => 11;
acd4faf2 6use Test::Exception;
fde3db1a 7use t::common qw( new_fh );
714618f0 8
2a81bf9e 9use_ok( 'DBM::Deep' );
10
fde3db1a 11my ($fh, $filename) = new_fh();
2a81bf9e 12
13# Create the datafile to be used
14{
15 my $db = DBM::Deep->new( $filename );
16 $db->{hash} = { foo => [ 'a' .. 'c' ] };
17}
714618f0 18
70b55428 19{
2a81bf9e 20 open(FILE, $filename) || die("Can't open '$filename' for reading: $!\n");
714618f0 21
70b55428 22 my $db;
714618f0 23
70b55428 24 # test if we can open and read a db using its filehandle
714618f0 25
70b55428 26 ok(($db = DBM::Deep->new(fh => *FILE)), "open db in filehandle");
27 ok($db->{hash}->{foo}->[1] eq 'b', "and get at stuff in the database");
28 throws_ok {
29 $db->{foo} = 1;
e06824f8 30 } qr/Cannot write to a readonly filehandle/,
31 "Can't write to a read-only filehandle";
70b55428 32 ok( !$db->exists( 'foo' ), "foo doesn't exist" );
714618f0 33
70b55428 34 my $db_obj = $db->_get_self;
35 ok( $db_obj->_root->{inode}, "The inode has been set" );
36
37 close(FILE);
38}
714618f0 39
40# now the same, but with an offset into the file. Use the database that's
41# embedded in the test for the DATA filehandle. First, find the database ...
42open(FILE, "t/28_DATA.t") || die("Can't open t/28_DATA.t\n");
43while(my $line = <FILE>) {
44 last if($line =~ /^__DATA__/);
45}
46my $offset = tell(FILE);
47close(FILE);
48
6ed2f3df 49SKIP: {
96041a25 50 skip "File header and format changed ... gah!", 5;
70b55428 51 open(FILE, "t/28_DATA.t");
52
53 my $db;
714618f0 54
70b55428 55 ok(($db = DBM::Deep->new(fh => *FILE, file_offset => $offset)), "open db in filehandle with offset");
56
57 ok($db->{hash}->{foo}->[1] eq 'b', "and get at stuff in the database");
58
59 ok( !$db->exists( 'foo' ), "foo doesn't exist yet" );
60 throws_ok {
61 $db->{foo} = 1;
62 } qr/Cannot write to a readonly filehandle/, "Can't write to a read-only filehandle";
63 ok( !$db->exists( 'foo' ), "foo doesn't exist" );
64
65 close FILE;
66}