Fixed t/13 on Leopard and a couple issues with no_plan on newer Test::More installati...
[dbsrgits/DBM-Deep.git] / t / 27_filehandle.t
CommitLineData
09dd8113 1use 5.006_000;
2
714618f0 3use strict;
09dd8113 4use warnings FATAL => 'all';
5
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,
2120a181 79#XXX For some reason, this is needed to make the test pass. Figure out why later.
80locking => 0,
3e9498a1 81 });
82
83 $db->{x} = 'b';
84 is( $db->{x}, 'b', 'and it was stored' );
85 }
86
3e9498a1 87 {
88 open my $fh, '<', $filename;
89 my $db = DBM::Deep->new({
90 fh => $fh,
91 file_offset => $offset,
92 });
93
94 is($db->{x}, 'b', "and get at stuff in the database");
95
96 ok( !$db->exists( 'foo' ), "foo doesn't exist yet" );
97 throws_ok {
98 $db->{foo} = 1;
99 } qr/Cannot write to a readonly filehandle/, "Can't write to a read-only filehandle";
100 ok( !$db->exists( 'foo' ), "foo still doesn't exist" );
101
9c87a079 102 is( $db->{x}, 'b' );
3e9498a1 103 }
104
019404df 105 exec( "$^X -Iblib/lib $filename" );
70b55428 106}