71acd61a24d8ab98314050518d587c946ea5c641
[dbsrgits/DBM-Deep.git] / t / 20_tie.t
1 ##
2 # DBM::Deep Test
3 ##
4 use strict;
5 use Test::More tests => 11;
6 use Test::Exception;
7 use File::Temp qw( tempfile tempdir );
8 use Fcntl qw( :flock );
9
10 use_ok( 'DBM::Deep' );
11
12 my $dir = tempdir( CLEANUP => 1 );
13
14 ##
15 # testing the various modes of opening a file
16 ##
17 {
18     my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir );
19     flock $fh, LOCK_UN;
20     my %hash;
21     my $db = tie %hash, 'DBM::Deep', $filename;
22
23     ok(1, "Tied an hash with an array for params" );
24 }
25
26 {
27     my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir );
28     flock $fh, LOCK_UN;
29     my %hash;
30     my $db = tie %hash, 'DBM::Deep', {
31         file => $filename,
32     };
33
34     ok(1, "Tied a hash with a hashref for params" );
35 }
36
37 {
38     my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir );
39     flock $fh, LOCK_UN;
40     my @array;
41     my $db = tie @array, 'DBM::Deep', $filename;
42
43     ok(1, "Tied an array with an array for params" );
44
45     is( $db->{type}, DBM::Deep->TYPE_ARRAY, "TIE_ARRAY sets the correct type" );
46 }
47
48 {
49     my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir );
50     flock $fh, LOCK_UN;
51     my @array;
52     my $db = tie @array, 'DBM::Deep', {
53         file => $filename,
54     };
55
56     ok(1, "Tied an array with a hashref for params" );
57
58     is( $db->{type}, DBM::Deep->TYPE_ARRAY, "TIE_ARRAY sets the correct type" );
59 }
60
61 my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir );
62 flock $fh, LOCK_UN;
63 throws_ok {
64     tie my %hash, 'DBM::Deep', [ file => $filename ];
65 } qr/Not a hashref/, "Passing an arrayref to TIEHASH fails";
66
67 throws_ok {
68     tie my @array, 'DBM::Deep', [ file => $filename ];
69 } qr/Not a hashref/, "Passing an arrayref to TIEARRAY fails";
70
71 throws_ok {
72     tie my %hash, 'DBM::Deep', undef, file => $filename;
73 } qr/Odd number of parameters/, "Odd number of params to TIEHASH fails";
74
75 throws_ok {
76     tie my @array, 'DBM::Deep', undef, file => $filename;
77 } qr/Odd number of parameters/, "Odd number of params to TIEARRAY fails";