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