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