01297e337e46451efa22a7bb71755df1ed17d747
[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
9 use_ok( 'DBM::Deep' );
10
11 ##
12 # make sure you can clear the error state
13 ##
14 ##
15 # test a corrupted file
16 ##
17 open FH, '>t/test.db';
18 print FH 'DPDB';
19 close FH;
20 throws_ok {
21     DBM::Deep->new( "t/test.db" );
22 } qr/DBM::Deep: Corrupted file, no master index record/, "Fail if there's no master index record";
23
24 {
25     unlink "t/test.db";
26     my %hash;
27     tie %hash, 'DBM::Deep', 't/test.db';
28     undef %hash;
29
30     my @array;
31     throws_ok {
32         tie @array, 'DBM::Deep', 't/test.db';
33     } qr/DBM::Deep: File type mismatch/, "Fail if we try and tie a hash file with an array";
34
35     throws_ok {
36         DBM::Deep->new( file => 't/test.db', type => DBM::Deep->TYPE_ARRAY )
37     } qr/DBM::Deep: File type mismatch/, "Fail if we try and open a hash file with an array";
38 }
39
40 {
41     unlink "t/test.db";
42     my @array;
43     tie @array, 'DBM::Deep', 't/test.db';
44     undef @array;
45
46     my %hash;
47     throws_ok {
48         tie %hash, 'DBM::Deep', 't/test.db';
49     } qr/DBM::Deep: File type mismatch/, "Fail if we try and tie an array file with a hash";
50
51     throws_ok {
52         DBM::Deep->new( file => 't/test.db', type => DBM::Deep->TYPE_HASH )
53     } qr/DBM::Deep: File type mismatch/, "Fail if we try and open an array file with a hash";
54 }