Further changes
[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
8 use_ok( 'DBM::Deep' );
9
10 ##
11 # testing the various modes of opening a file
12 ##
13 {
14     unlink "t/test.db";
15     my %hash;
16     my $db = tie %hash, 'DBM::Deep', 't/test.db';
17
18     if ($db->error()) {
19         print "ERROR: " . $db->error();
20         ok(0);
21         exit(0);
22     }
23     else { ok(1, "Tied an hash with an array for params" ); }
24 }
25
26 {
27     unlink "t/test.db";
28     my %hash;
29     my $db = tie %hash, 'DBM::Deep', {
30         file => 't/test.db',
31     };
32
33     if ($db->error()) {
34         print "ERROR: " . $db->error();
35         ok(0);
36         exit(0);
37     }
38     else { ok(1, "Tied a hash with a hashref for params" ); }
39 }
40
41 {
42     unlink "t/test.db";
43     my @array;
44     my $db = tie @array, 'DBM::Deep', 't/test.db';
45
46     if ($db->error()) {
47         print "ERROR: " . $db->error();
48         ok(0);
49         exit(0);
50     }
51     else { ok(1, "Tied an array with an array for params" ); }
52
53     is( $db->{type}, DBM::Deep->TYPE_ARRAY, "TIE_ARRAY sets the correct type" );
54 }
55
56 {
57     unlink "t/test.db";
58     my @array;
59     my $db = tie @array, 'DBM::Deep', {
60         file => 't/test.db',
61     };
62
63     if ($db->error()) {
64         print "ERROR: " . $db->error();
65         ok(0);
66         exit(0);
67     }
68     else { ok(1, "Tied an array with a hashref for params" ); }
69
70     is( $db->{type}, DBM::Deep->TYPE_ARRAY, "TIE_ARRAY sets the correct type" );
71 }
72
73 unlink "t/test.db";
74 throws_ok {
75     tie my %hash, 'DBM::Deep', [ file => 't/test.db' ];
76 } qr/Not a hashref/, "Passing an arrayref to TIEHASH fails";
77
78 unlink "t/test.db";
79 throws_ok {
80     tie my @array, 'DBM::Deep', [ file => 't/test.db' ];
81 } qr/Not a hashref/, "Passing an arrayref to TIEARRAY fails";
82
83 unlink "t/test.db";
84 throws_ok {
85     tie my %hash, 'DBM::Deep', undef, file => 't/test.db';
86 } qr/Odd number of parameters/, "Odd number of params to TIEHASH fails";
87
88 unlink "t/test.db";
89 throws_ok {
90     tie my @array, 'DBM::Deep', undef, file => 't/test.db';
91 } qr/Odd number of parameters/, "Odd number of params to TIEARRAY fails";