Converted all relevant tests to use new_dbm instead of new_fh and all tests (except...
[dbsrgits/DBM-Deep.git] / t / 21_tie_access.t
1 use strict;
2 use warnings FATAL => 'all';
3
4 use Test::More;
5 use Test::Exception;
6 use t::common qw( new_fh );
7
8 use_ok( 'DBM::Deep' );
9
10 my ($fh, $filename) = new_fh();
11
12 {
13     my %hash;
14     tie %hash, 'DBM::Deep', $filename;
15
16     $hash{key1} = 'value';
17     is( $hash{key1}, 'value', 'Set and retrieved key1' );
18     tied( %hash )->_get_self->_engine->storage->close( tied( %hash )->_get_self );
19 }
20
21 {
22     my %hash;
23     tie %hash, 'DBM::Deep', $filename;
24
25     is( $hash{key1}, 'value', 'Set and retrieved key1' );
26
27     is( keys %hash, 1, "There's one key so far" );
28     ok( exists $hash{key1}, "... and it's key1" );
29     tied( %hash )->_get_self->_engine->storage->close( tied( %hash )->_get_self );
30 }
31
32 {
33     throws_ok {
34         tie my @array, 'DBM::Deep', {
35             file => $filename,
36             type => DBM::Deep->TYPE_ARRAY,
37         };
38         tied( @array )->_get_self->_engine->storage->close( tied( @array )->_get_self );
39     } qr/DBM::Deep: File type mismatch/, "\$SIG_TYPE doesn't match file's type";
40 }
41
42 {
43     my ($fh, $filename) = new_fh();
44     my $db = DBM::Deep->new( file => $filename, type => DBM::Deep->TYPE_ARRAY );
45
46     throws_ok {
47         tie my %hash, 'DBM::Deep', {
48             file => $filename,
49             type => DBM::Deep->TYPE_HASH,
50         };
51     } qr/DBM::Deep: File type mismatch/, "\$SIG_TYPE doesn't match file's type";
52     $db->_get_self->_engine->storage->close( $db->_get_self );
53 }
54
55 done_testing;