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