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