X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F16_circular.t;h=61667367778842e2dc17539432130617280ae9c4;hb=0e3e35555b9d0f781548d4d383f601ba69837310;hp=501435d1c673e0e3c23875e3866a56d244ab8af0;hpb=4f0f6fff0474a896cad8bbb80d8c0ccce3d87bf9;p=dbsrgits%2FDBM-Deep.git diff --git a/t/16_circular.t b/t/16_circular.t index 501435d..6166736 100644 --- a/t/16_circular.t +++ b/t/16_circular.t @@ -1,103 +1,104 @@ -## -# DBM::Deep Test -## use strict; -use Test::More tests => 32; -use t::common qw( new_fh ); +use warnings FATAL => 'all'; -use_ok( 'DBM::Deep' ); +use Test::More; +use t::common qw( new_dbm ); -my ($fh, $filename) = new_fh(); -my $db = DBM::Deep->new( $filename ); - -## -# put/get simple keys -## -$db->{key1} = "value1"; -$db->{key2} = "value2"; - -my @keys_1 = sort keys %$db; - -$db->{key3} = $db->{key1}; - -my @keys_2 = sort keys %$db; -is( @keys_2 + 0, @keys_1 + 1, "Correct number of keys" ); -is_deeply( - [ @keys_1, 'key3' ], - [ @keys_2 ], - "Keys still match after circular reference is added", -); - -$db->{key4} = { 'foo' => 'bar' }; -$db->{key5} = $db->{key4}; -$db->{key6} = $db->{key5}; - -my @keys_3 = sort keys %$db; - -is( @keys_3 + 0, @keys_2 + 3, "Correct number of keys" ); -is_deeply( - [ @keys_2, 'key4', 'key5', 'key6', ], - [ @keys_3 ], - "Keys still match after circular reference is added (@keys_3)", -); - -## -# Insert circular reference -## -$db->{circle} = $db; - -my @keys_4 = sort keys %$db; - -is( @keys_4 + 0, @keys_3 + 1, "Correct number of keys" ); -is_deeply( - [ 'circle', @keys_3 ], - [ @keys_4 ], - "Keys still match after circular reference is added", -); - -## -# Make sure keys exist in both places -## -is( $db->{key1}, 'value1', "The value is there directly" ); -is( $db->{circle}{key1}, 'value1', "The value is there in one loop of the circle" ); -is( $db->{circle}{circle}{key1}, 'value1', "The value is there in two loops of the circle" ); -is( $db->{circle}{circle}{circle}{key1}, 'value1', "The value is there in three loops of the circle" ); - -## -# Make sure changes are reflected in both places -## -$db->{key1} = "another value"; - -isnt( $db->{key3}, 'another value', "Simple scalars are copied by value" ); - -is( $db->{key1}, 'another value', "The value is there directly" ); -is( $db->{circle}{key1}, 'another value', "The value is there in one loop of the circle" ); -is( $db->{circle}{circle}{key1}, 'another value', "The value is there in two loops of the circle" ); -is( $db->{circle}{circle}{circle}{key1}, 'another value', "The value is there in three loops of the circle" ); - -$db->{circle}{circle}{circle}{circle}{key1} = "circles"; - -is( $db->{key1}, 'circles', "The value is there directly" ); -is( $db->{circle}{key1}, 'circles', "The value is there in one loop of the circle" ); -is( $db->{circle}{circle}{key1}, 'circles', "The value is there in two loops of the circle" ); -is( $db->{circle}{circle}{circle}{key1}, 'circles', "The value is there in three loops of the circle" ); - -is( $db->{key4}{foo}, 'bar' ); -is( $db->{key5}{foo}, 'bar' ); -is( $db->{key6}{foo}, 'bar' ); - -$db->{key4}{foo2} = 'bar2'; -is( $db->{key4}{foo2}, 'bar2' ); -is( $db->{key5}{foo2}, 'bar2' ); -is( $db->{key6}{foo2}, 'bar2' ); - -$db->{key4}{foo3} = 'bar3'; -is( $db->{key4}{foo3}, 'bar3' ); -is( $db->{key5}{foo3}, 'bar3' ); -is( $db->{key6}{foo3}, 'bar3' ); - -$db->{key4}{foo4} = 'bar4'; -is( $db->{key4}{foo4}, 'bar4' ); -is( $db->{key5}{foo4}, 'bar4' ); -is( $db->{key6}{foo4}, 'bar4' ); +use_ok( 'DBM::Deep' ); +my $dbm_factory = new_dbm(); +while ( my $dbm_maker = $dbm_factory->() ) { + my $db = $dbm_maker->(); + + ## + # put/get simple keys + ## + $db->{key1} = "value1"; + $db->{key2} = "value2"; + + my @keys_1 = sort keys %$db; + + $db->{key3} = $db->{key1}; + + my @keys_2 = sort keys %$db; + is( @keys_2 + 0, @keys_1 + 1, "Correct number of keys" ); + is_deeply( + [ @keys_1, 'key3' ], + [ @keys_2 ], + "Keys still match after circular reference is added", + ); + + $db->{key4} = { 'foo' => 'bar' }; + $db->{key5} = $db->{key4}; + $db->{key6} = $db->{key5}; + + my @keys_3 = sort keys %$db; + + is( @keys_3 + 0, @keys_2 + 3, "Correct number of keys" ); + is_deeply( + [ @keys_2, 'key4', 'key5', 'key6', ], + [ @keys_3 ], + "Keys still match after circular reference is added (@keys_3)", + ); + + ## + # Insert circular reference + ## + $db->{circle} = $db; + + my @keys_4 = sort keys %$db; + + is( @keys_4 + 0, @keys_3 + 1, "Correct number of keys" ); + is_deeply( + [ 'circle', @keys_3 ], + [ @keys_4 ], + "Keys still match after circular reference is added", + ); + + ## + # Make sure keys exist in both places + ## + is( $db->{key1}, 'value1', "The value is there directly" ); + is( $db->{circle}{key1}, 'value1', "The value is there in one loop of the circle" ); + is( $db->{circle}{circle}{key1}, 'value1', "The value is there in two loops of the circle" ); + is( $db->{circle}{circle}{circle}{key1}, 'value1', "The value is there in three loops of the circle" ); + + ## + # Make sure changes are reflected in both places + ## + $db->{key1} = "another value"; + + isnt( $db->{key3}, 'another value', "Simple scalars are copied by value" ); + + is( $db->{key1}, 'another value', "The value is there directly" ); + is( $db->{circle}{key1}, 'another value', "The value is there in one loop of the circle" ); + is( $db->{circle}{circle}{key1}, 'another value', "The value is there in two loops of the circle" ); + is( $db->{circle}{circle}{circle}{key1}, 'another value', "The value is there in three loops of the circle" ); + + $db->{circle}{circle}{circle}{circle}{key1} = "circles"; + + is( $db->{key1}, 'circles', "The value is there directly" ); + is( $db->{circle}{key1}, 'circles', "The value is there in one loop of the circle" ); + is( $db->{circle}{circle}{key1}, 'circles', "The value is there in two loops of the circle" ); + is( $db->{circle}{circle}{circle}{key1}, 'circles', "The value is there in three loops of the circle" ); + + is( $db->{key4}{foo}, 'bar' ); + is( $db->{key5}{foo}, 'bar' ); + is( $db->{key6}{foo}, 'bar' ); + + $db->{key4}{foo2} = 'bar2'; + is( $db->{key4}{foo2}, 'bar2' ); + is( $db->{key5}{foo2}, 'bar2' ); + is( $db->{key6}{foo2}, 'bar2' ); + + $db->{key4}{foo3} = 'bar3'; + is( $db->{key4}{foo3}, 'bar3' ); + is( $db->{key5}{foo3}, 'bar3' ); + is( $db->{key6}{foo3}, 'bar3' ); + + $db->{key4}{foo4} = 'bar4'; + is( $db->{key4}{foo4}, 'bar4' ); + is( $db->{key5}{foo4}, 'bar4' ); + is( $db->{key6}{foo4}, 'bar4' ); +} +done_testing;