Added more tests on SPLICE
[dbsrgits/DBM-Deep.git] / t / 04_array.t
index d3fd6c9..7398265 100644 (file)
@@ -2,7 +2,7 @@
 # DBM::Deep Test
 ##
 use strict;
-use Test::More tests => 93;
+use Test::More tests => 107;
 use Test::Exception;
 
 use_ok( 'DBM::Deep' );
@@ -53,24 +53,31 @@ is( $db->fetch(4), 'elem4', "fetch() for store() works" );
 
 is( $db->length, 5, "... and we have five elements" );
 
-is( $db->[-1], $db->[4], "-1st index is 4th value" );
-is( $db->[-2], $db->[3], "-2nd index is 3rd value" );
-is( $db->[-3], $db->[2], "-3rd index is 2nd value" );
-is( $db->[-4], $db->[1], "-4th index is 1st value" );
-is( $db->[-5], $db->[0], "-5th index is 0th value" );
-TODO: {
-    local $TODO = "Going off the end of the array from the back is legal";
-    eval { is( $db->[-6], undef, "-6th index is undef" ); };
-}
+is( $db->[-1], $db->[4], "-1st index is 4th index" );
+is( $db->[-2], $db->[3], "-2nd index is 3rd index" );
+is( $db->[-3], $db->[2], "-3rd index is 2nd index" );
+is( $db->[-4], $db->[1], "-4th index is 1st index" );
+is( $db->[-5], $db->[0], "-5th index is 0th index" );
+is( $db->[-6], undef, "-6th index is undef" );
 is( $db->length, 5, "... and we have five elements after abortive -6 index lookup" );
 
+$db->[-1] = 'elem4.1';
+is( $db->[-1], 'elem4.1' );
+is( $db->[4], 'elem4.1' );
+is( $db->get(4), 'elem4.1' );
+is( $db->fetch(4), 'elem4.1' );
+
+throws_ok {
+    $db->[-6] = 'whoops!';
+} qr/Modification of non-creatable array value attempted, subscript -6/, "Correct error thrown"; 
+
 my $popped = $db->pop;
 is( $db->length, 4, "... and we have four after popping" );
 is( $db->[0], 'elem0', "0th element still there after popping" );
 is( $db->[1], 'elem1', "1st element still there after popping" );
 is( $db->[2], 'elem2', "2nd element still there after popping" );
 is( $db->[3], 'elem3', "3rd element still there after popping" );
-is( $popped, 'elem4', "Popped value is correct" );
+is( $popped, 'elem4.1', "Popped value is correct" );
 
 my $shifted = $db->shift;
 is( $db->length, 3, "... and we have three after shifting" );
@@ -87,10 +94,7 @@ is( $db->length, 3, "... and we still have three after deleting" );
 is( $db->[0], undef, "0th element now undef" );
 is( $db->[1], 'elem2', "1st element still there after deleting" );
 is( $db->[2], 'elem3', "2nd element still there after deleting" );
-TODO: {
-    local $TODO = "delete on an array element should return the deleted value";
-    is( $deleted, 'elem1', "Deleted value is correct" );
-}
+is( $deleted, 'elem1', "Deleted value is correct" );
 
 is( $db->delete(99), undef, 'delete on an element not in the array returns undef' );
 is( $db->length, 3, "... and we still have three after a delete on an out-of-range index" );
@@ -107,15 +111,9 @@ is( $db->length, 3, "... and we still have three after a DELETE on an out-of-ran
 $deleted = $db->delete(-2);
 is( $db->length, 3, "... and we still have three after deleting" );
 is( $db->[0], undef, "0th element still undef" );
-TODO: {
-    local $TODO = "delete on a negative array element should work";
-    is( $db->[1], undef, "1st element now undef" );
-}
+is( $db->[1], undef, "1st element now undef" );
 is( $db->[2], 'elem3', "2nd element still there after deleting" );
-TODO: {
-    local $TODO = "delete on an array element should return the deleted value";
-    is( $deleted, 'elem2', "Deleted value is correct" );
-}
+is( $deleted, 'elem2', "Deleted value is correct" );
 
 $db->[1] = 'elem2';
 
@@ -125,10 +123,7 @@ $db->[1] = 'elem2';
 ok( $db->exists(1), "The 1st value exists" );
 ok( !$db->exists(0), "The 0th value doesn't exists" );
 ok( !$db->exists(22), "The 22nd value doesn't exists" );
-TODO: {
-    local $TODO = "exists on negative values should work";
-    ok( $db->exists(-1), "The -1st value does exists" );
-}
+ok( $db->exists(-1), "The -1st value does exists" );
 ok( !$db->exists(-22), "The -22nd value doesn't exists" );
 
 ##
@@ -143,12 +138,10 @@ is( $db->length(), 0, "After pop() on empty array, length is still 0" );
 is( $db->shift, undef, "shift on an empty array returns undef" );
 is( $db->length(), 0, "After shift() on empty array, length is still 0" );
 
-TODO: {
-    local $TODO = "unshift returns the number of elements in the array";
-    is( $db->unshift( 1, 2, 3 ), 3, "unshift returns the number of elements in the array" );
-    is( $db->unshift( 1, 2, 3 ), 6, "unshift returns the number of elements in the array" );
-    is( $db->push( 1, 2, 3 ), 9, "unshift returns the number of elements in the array" );
-}
+is( $db->unshift( 1, 2, 3 ), 3, "unshift returns the number of elements in the array" );
+is( $db->unshift( 1, 2, 3 ), 6, "unshift returns the number of elements in the array" );
+is( $db->push( 1, 2, 3 ), 9, "push returns the number of elements in the array" );
+
 is( $db->length(), 9, "After unshift and push on empty array, length is now 9" );
 
 $db->clear;
@@ -165,7 +158,9 @@ is($db->[2], "elem last", "Third element is 'elem last'");
 ##
 # splice with length 1
 ##
-$db->splice( 1, 1, "middle A", "middle B" );
+my @returned = $db->splice( 1, 1, "middle A", "middle B" );
+is( scalar(@returned), 1, "One element was removed" );
+is( $returned[0], 'elem middle', "... and it was correctly removed" );
 is($db->length(), 4);
 is($db->[0], "elem first");
 is($db->[1], "middle A");
@@ -175,7 +170,8 @@ is($db->[3], "elem last");
 ##
 # splice with length of 0
 ##
-$db->splice( -1, 0, "middle C" );
+@returned = $db->splice( -1, 0, "middle C" );
+is( scalar(@returned), 0, "No elements were removed" );
 is($db->length(), 5);
 is($db->[0], "elem first");
 is($db->[1], "middle A");
@@ -186,12 +182,27 @@ is($db->[4], "elem last");
 ##
 # splice with length of 3
 ##
-$db->splice( 1, 3, "middle ABC" );
+my $returned = $db->splice( 1, 3, "middle ABC" );
+is( $returned, 'middle C', "Just the last element was returned" );
 is($db->length(), 3);
 is($db->[0], "elem first");
 is($db->[1], "middle ABC");
 is($db->[2], "elem last");
 
+@returned = $db->splice( 1 );
+is($db->length(), 1);
+is($db->[0], "elem first");
+is($returned[0], "middle ABC");
+is($returned[1], "elem last");
+
+$db->push( @returned );
+
+@returned = $db->splice( 1, -1 );
+is($db->length(), 2);
+is($db->[0], "elem first");
+is($db->[1], "elem last");
+is($returned[0], "middle ABC");
+
 # These tests verify that the hash methods cannot be called on arraytypes.
 # They will be removed once the ARRAY and HASH types are refactored into their own classes.
 
@@ -199,19 +210,3 @@ $db->[0] = [ 1 .. 3 ];
 $db->[1] = { a => 'foo' };
 is( $db->[0]->length, 3, "Reuse of same space with array successful" );
 is( $db->[1]->fetch('a'), 'foo', "Reuse of same space with hash successful" );
-
-throws_ok {
-    $db->FIRSTKEY();
-} qr/FIRSTKEY method only supported for hashes/, "Cannot call FIRSTKEY on an array type";
-
-throws_ok {
-    $db->first_key();
-} qr/FIRSTKEY method only supported for hashes/, "Cannot call first_key on an array type";
-
-throws_ok {
-    $db->NEXTKEY();
-} qr/NEXTKEY method only supported for hashes/, "Cannot call NEXTKEY on an array type";
-
-throws_ok {
-    $db->next_key();
-} qr/NEXTKEY method only supported for hashes/, "Cannot call next_key on an array type";