7e2e665f816d5194cff4e7989d99f11ab7a35021
[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
9 use_ok( 'DBM::Deep' );
10
11 my $dir = tempdir( CLEANUP => 1 );
12
13 ##
14 # testing the various modes of opening a file
15 ##
16 {
17     my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir );
18     my %hash;
19     my $db = tie %hash, 'DBM::Deep', $filename;
20
21     ok(1, "Tied an hash with an array for params" );
22 }
23
24 {
25     my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir );
26     my %hash;
27     my $db = tie %hash, 'DBM::Deep', {
28         file => $filename,
29     };
30
31     ok(1, "Tied a hash with a hashref for params" );
32 }
33
34 {
35     my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir );
36     my @array;
37     my $db = tie @array, 'DBM::Deep', $filename;
38
39     ok(1, "Tied an array with an array for params" );
40
41     is( $db->{type}, DBM::Deep->TYPE_ARRAY, "TIE_ARRAY sets the correct type" );
42 }
43
44 {
45     my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir );
46     my @array;
47     my $db = tie @array, 'DBM::Deep', {
48         file => $filename,
49     };
50
51     ok(1, "Tied an array with a hashref for params" );
52
53     is( $db->{type}, DBM::Deep->TYPE_ARRAY, "TIE_ARRAY sets the correct type" );
54 }
55
56 my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir );
57 throws_ok {
58     tie my %hash, 'DBM::Deep', [ file => $filename ];
59 } qr/Not a hashref/, "Passing an arrayref to TIEHASH fails";
60
61 throws_ok {
62     tie my @array, 'DBM::Deep', [ file => $filename ];
63 } qr/Not a hashref/, "Passing an arrayref to TIEARRAY fails";
64
65 throws_ok {
66     tie my %hash, 'DBM::Deep', undef, file => $filename;
67 } qr/Odd number of parameters/, "Odd number of params to TIEHASH fails";
68
69 throws_ok {
70     tie my @array, 'DBM::Deep', undef, file => $filename;
71 } qr/Odd number of parameters/, "Odd number of params to TIEARRAY fails";