From: rkinyon Date: Mon, 15 Jan 2007 05:44:06 +0000 (+0000) Subject: Added test for importing an array X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=be12c94187bd98d16a2fce73c61339a7ef6d72e8;p=dbsrgits%2FDBM-Deep.git Added test for importing an array --- diff --git a/lib/DBM/Deep.pm b/lib/DBM/Deep.pm index 03dd371..d93d268 100644 --- a/lib/DBM/Deep.pm +++ b/lib/DBM/Deep.pm @@ -199,13 +199,13 @@ sub _copy_value { return 1; } -sub _copy_node { - die "Must be implemented in a child class\n"; -} - -sub _repr { - die "Must be implemented in a child class\n"; -} +#sub _copy_node { +# die "Must be implemented in a child class\n"; +#} +# +#sub _repr { +# die "Must be implemented in a child class\n"; +#} sub export { ## @@ -244,11 +244,11 @@ sub import { #XXX This isn't the best solution. Better would be to use Data::Walker, #XXX but that's a lot more thinking than I want to do right now. eval { - #$self->begin_work; + $self->begin_work; $self->_import( _clone_data( $struct ) ); - #$self->commit; + $self->commit; }; if ( $@ ) { - #$self->rollback; + $self->rollback; die $@; } diff --git a/lib/DBM/Deep/File.pm b/lib/DBM/Deep/File.pm index bbb87f8..454fa93 100644 --- a/lib/DBM/Deep/File.pm +++ b/lib/DBM/Deep/File.pm @@ -125,17 +125,6 @@ sub read_at { return $buffer; } -sub increment_pointer { - my $self = shift; - my ($size) = @_; - - if ( defined $size ) { - seek( $self->{fh}, $size, SEEK_CUR ); - } - - return 1; -} - sub DESTROY { my $self = shift; return unless $self; diff --git a/t/17_import.t b/t/17_import.t index eeb8688..8bb4c28 100644 --- a/t/17_import.t +++ b/t/17_import.t @@ -2,59 +2,96 @@ # DBM::Deep Test ## use strict; -use Test::More tests => 6; +use Test::More tests => 9; use Test::Deep; use t::common qw( new_fh ); use_ok( 'DBM::Deep' ); -my ($fh, $filename) = new_fh(); -my $db = DBM::Deep->new({ - file => $filename, - autobless => 1, -}); +{ + my ($fh, $filename) = new_fh(); + my $db = DBM::Deep->new({ + file => $filename, + autobless => 1, + }); ## # Create structure in memory ## -my $struct = { - key1 => "value1", - key2 => "value2", - array1 => [ "elem0", "elem1", "elem2" ], - hash1 => { - subkey1 => "subvalue1", - subkey2 => "subvalue2", - subkey3 => bless( {}, 'Foo' ), - } -}; + my $struct = { + key1 => "value1", + key2 => "value2", + array1 => [ "elem0", "elem1", "elem2" ], + hash1 => { + subkey1 => "subvalue1", + subkey2 => "subvalue2", + subkey3 => bless( {}, 'Foo' ), + } + }; ## # Import entire thing ## -$db->import( $struct ); + $db->import( $struct ); -cmp_deeply( - $db, - noclass({ - key1 => 'value1', - key2 => 'value2', - array1 => [ 'elem0', 'elem1', 'elem2', ], - hash1 => { - subkey1 => "subvalue1", - subkey2 => "subvalue2", - subkey3 => useclass( bless {}, 'Foo' ), - }, - }), - "Everything matches", -); - -$struct->{foo} = 'bar'; -is( $struct->{foo}, 'bar', "\$struct has foo and it's 'bar'" ); -ok( !exists $db->{foo}, "\$db doesn't have the 'foo' key, so \$struct is not tied" ); - -$struct->{hash1}->{foo} = 'bar'; -is( $struct->{hash1}->{foo}, 'bar', "\$struct->{hash1} has foo and it's 'bar'" ); -ok( !exists $db->{hash1}->{foo}, "\$db->{hash1} doesn't have the 'foo' key, so \$struct->{hash1} is not tied" ); + cmp_deeply( + $db, + noclass({ + key1 => 'value1', + key2 => 'value2', + array1 => [ 'elem0', 'elem1', 'elem2', ], + hash1 => { + subkey1 => "subvalue1", + subkey2 => "subvalue2", + subkey3 => useclass( bless {}, 'Foo' ), + }, + }), + "Everything matches", + ); + + $struct->{foo} = 'bar'; + is( $struct->{foo}, 'bar', "\$struct has foo and it's 'bar'" ); + ok( !exists $db->{foo}, "\$db doesn't have the 'foo' key, so \$struct is not tied" ); + + $struct->{hash1}->{foo} = 'bar'; + is( $struct->{hash1}->{foo}, 'bar', "\$struct->{hash1} has foo and it's 'bar'" ); + ok( !exists $db->{hash1}->{foo}, "\$db->{hash1} doesn't have the 'foo' key, so \$struct->{hash1} is not tied" ); +} + +{ + my ($fh, $filename) = new_fh(); + my $db = DBM::Deep->new({ + file => $filename, + type => DBM::Deep->TYPE_ARRAY, + }); + + my $struct = [ + 1 .. 3, + [ 2, 4, 6 ], + bless( [], 'Bar' ), + { foo => [ 2 .. 4 ] }, + ]; + +## +# Import entire thing +## + $db->import( $struct ); + + cmp_deeply( + $db, + noclass([ + 1 .. 3, + [ 2, 4, 6 ], + useclass( bless( [], 'Bar' ) ), + { foo => [ 2 .. 4 ] }, + ]), + "Everything matches", + ); + + push @$struct, 'bar'; + is( $struct->[-1], 'bar', "\$struct has 'bar' at the end" ); + ok( $db->[-1], "\$db doesn't have the 'bar' value at the end, so \$struct is not tied" ); +} __END__