r14213@rob-kinyons-computer (orig r8080): rkinyon | 2006-11-17 20:47:50 -0500
[dbsrgits/DBM-Deep.git] / t / 27_filehandle.t
CommitLineData
714618f0 1##
2# DBM::Deep Test
3##
4use strict;
3e9498a1 5use Test::More tests => 14;
acd4faf2 6use Test::Exception;
fde3db1a 7use t::common qw( new_fh );
714618f0 8
2a81bf9e 9use_ok( 'DBM::Deep' );
10
2a81bf9e 11{
3e9498a1 12 my ($fh, $filename) = new_fh();
714618f0 13
3e9498a1 14 # Create the datafile to be used
15 {
16 my $db = DBM::Deep->new( $filename );
17 $db->{hash} = { foo => [ 'a' .. 'c' ] };
18 }
714618f0 19
3e9498a1 20 {
21 open(my $fh, '<', $filename) || die("Can't open '$filename' for reading: $!\n");
714618f0 22
3e9498a1 23 my $db;
714618f0 24
3e9498a1 25 # test if we can open and read a db using its filehandle
714618f0 26
3e9498a1 27 ok(($db = DBM::Deep->new(fh => $fh)), "open db in filehandle");
28 ok($db->{hash}->{foo}->[1] eq 'b', "and get at stuff in the database");
29 throws_ok {
30 $db->{foo} = 1;
31 } qr/Cannot write to a readonly filehandle/,
32 "Can't write to a read-only filehandle";
33 ok( !$db->exists( 'foo' ), "foo doesn't exist" );
70b55428 34
3e9498a1 35 my $db_obj = $db->_get_self;
83371fe3 36 ok( $db_obj->_storage->{inode}, "The inode has been set" );
3e9498a1 37
38 close($fh);
39 }
70b55428 40}
714618f0 41
42# now the same, but with an offset into the file. Use the database that's
43# embedded in the test for the DATA filehandle. First, find the database ...
3e9498a1 44{
45 my ($fh,$filename) = new_fh();
70b55428 46
3e9498a1 47 print $fh "#!$^X\n";
48 print $fh <<'__END_FH__';
49use strict;
50use Test::More no_plan => 1;
51Test::More->builder->no_ending(1);
52Test::More->builder->{Curr_Test} = 12;
70b55428 53
3e9498a1 54use_ok( 'DBM::Deep' );
70b55428 55
3e9498a1 56my $db = DBM::Deep->new({
57 fh => *DATA,
58});
59is($db->{x}, 'b', "and get at stuff in the database");
60__END_FH__
61 print $fh "__DATA__\n";
62 close $fh;
63
64 my $offset = do {
65 open my $fh, '<', $filename;
66 while(my $line = <$fh>) {
67 last if($line =~ /^__DATA__/);
68 }
69 tell($fh);
70 };
71
72 {
73 my $db = DBM::Deep->new({
74 file => $filename,
75 file_offset => $offset,
2120a181 76#XXX For some reason, this is needed to make the test pass. Figure out why later.
77locking => 0,
3e9498a1 78 });
79
80 $db->{x} = 'b';
81 is( $db->{x}, 'b', 'and it was stored' );
82 }
83
3e9498a1 84 {
85 open my $fh, '<', $filename;
86 my $db = DBM::Deep->new({
87 fh => $fh,
88 file_offset => $offset,
89 });
90
91 is($db->{x}, 'b', "and get at stuff in the database");
92
93 ok( !$db->exists( 'foo' ), "foo doesn't exist yet" );
94 throws_ok {
95 $db->{foo} = 1;
96 } qr/Cannot write to a readonly filehandle/, "Can't write to a read-only filehandle";
97 ok( !$db->exists( 'foo' ), "foo still doesn't exist" );
98
99 is( $db->{x}, 'b' );
100 }
101
019404df 102 exec( "$^X -Iblib/lib $filename" );
70b55428 103}