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