Commit | Line | Data |
ffed8b01 |
1 | ## |
2 | # DBM::Deep Test |
3 | ## |
467f373b |
4 | $|++; |
ffed8b01 |
5 | use strict; |
f2641a61 |
6 | use Test::More tests => 6; |
7 | use Test::Exception; |
2a81bf9e |
8 | use File::Temp qw( tempfile tempdir ); |
58910373 |
9 | use Fcntl qw( :flock ); |
ffed8b01 |
10 | |
11 | use_ok( 'DBM::Deep' ); |
12 | |
2a81bf9e |
13 | my $dir = tempdir( CLEANUP => 1 ); |
14 | my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir ); |
58910373 |
15 | flock $fh, LOCK_UN; |
2a81bf9e |
16 | |
1e9d4578 |
17 | ## |
1e9d4578 |
18 | # test a corrupted file |
19 | ## |
2a81bf9e |
20 | open FH, ">$filename"; |
ffed8b01 |
21 | print FH 'DPDB'; |
22 | close FH; |
f2641a61 |
23 | throws_ok { |
2a81bf9e |
24 | DBM::Deep->new( $filename ); |
f2641a61 |
25 | } qr/DBM::Deep: Corrupted file, no master index record/, "Fail if there's no master index record"; |
1e9d4578 |
26 | |
f2641a61 |
27 | { |
2a81bf9e |
28 | my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir ); |
58910373 |
29 | flock $fh, LOCK_UN; |
f2641a61 |
30 | my %hash; |
2a81bf9e |
31 | tie %hash, 'DBM::Deep', $filename; |
f2641a61 |
32 | undef %hash; |
33 | |
34 | my @array; |
35 | throws_ok { |
2a81bf9e |
36 | tie @array, 'DBM::Deep', $filename; |
f2641a61 |
37 | } qr/DBM::Deep: File type mismatch/, "Fail if we try and tie a hash file with an array"; |
38 | |
39 | throws_ok { |
2a81bf9e |
40 | DBM::Deep->new( file => $filename, type => DBM::Deep->TYPE_ARRAY ) |
f2641a61 |
41 | } qr/DBM::Deep: File type mismatch/, "Fail if we try and open a hash file with an array"; |
42 | } |
43 | |
44 | { |
2a81bf9e |
45 | my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir ); |
58910373 |
46 | flock $fh, LOCK_UN; |
f2641a61 |
47 | my @array; |
2a81bf9e |
48 | tie @array, 'DBM::Deep', $filename; |
f2641a61 |
49 | undef @array; |
50 | |
51 | my %hash; |
52 | throws_ok { |
2a81bf9e |
53 | tie %hash, 'DBM::Deep', $filename; |
f2641a61 |
54 | } qr/DBM::Deep: File type mismatch/, "Fail if we try and tie an array file with a hash"; |
55 | |
56 | throws_ok { |
2a81bf9e |
57 | DBM::Deep->new( file => $filename, type => DBM::Deep->TYPE_HASH ) |
f2641a61 |
58 | } qr/DBM::Deep: File type mismatch/, "Fail if we try and open an array file with a hash"; |
59 | } |