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