Add each_value to scalar. Also add tests for the behaviour of setting both $_ and...
[gitmo/Moose-Autobox.git] / t / 010_each.t
index 5c1f02d..19b4b2a 100644 (file)
@@ -3,7 +3,7 @@
 use strict;
 use warnings;
 
-use Test::More tests => 11;
+use Test::More tests => 17;
 
 require_ok('Moose::Autobox');
 
@@ -20,13 +20,17 @@ use Moose::Autobox;
 
   is_deeply($href, \%hash, "sanity check to start");
 
-  my @keys;
+  my (@keys, @keys2);
   $href->each_key(sub { push @keys, $_ });
-  is_deeply([ sort @keys ], [ sort keys %hash ], "keys found via each_key");
+  is_deeply([ sort @keys ], [ sort keys %hash ], 'keys found via each_key in $_');
+  $href->each_key(sub { push @keys2, $_[0] });
+  is_deeply([ sort @keys2 ], [ sort keys %hash ], 'keys found via each_key in $_[0]');
 
-  my @values;
+  my (@values, @values2);
   $href->each_value(sub { push @values, $_ });
-  is_deeply([ sort @values ], [ sort values %hash ], "values via each_values");
+  is_deeply([ sort @values ], [ sort values %hash ], 'values via each_values in $_');
+  $href->each_value(sub { push @values2, $_[0] });
+  is_deeply([ sort @values2 ], [ sort values %hash ], 'values via each_values in $_[0]');
 
   $href->each_value(sub { $_++ });
   is($href->{a}, 2, "we can ++ values directly");
@@ -42,13 +46,17 @@ use Moose::Autobox;
 
   is_deeply($aref, \@array, "sanity check to start");
 
-  my @keys;
+  my (@keys, @keys2);
   $aref->each_key(sub { push @keys, $_ });
-  is_deeply([ @keys ], [ 0, 1, 2 ], "keys found via each_key");
+  is_deeply([ @keys ], [ 0, 1, 2 ], 'keys found via each_key in $_');
+  $aref->each_key(sub { push @keys2, $_[0] });
+  is_deeply([ @keys2 ], [ 0, 1, 2 ], 'keys found via each_key in $_[0]');
 
-  my @values;
+  my (@values, @values2);
   $aref->each_value(sub { push @values, $_ });
-  is_deeply([ @values ], [ @array ], "values via each_values");
+  is_deeply([ @values ], [ @array ], 'values via each_values in $_');
+  $aref->each_value(sub { push @values2, $_[0] });
+  is_deeply([ @values2 ], [ @array ], 'values via each_values in $_[0]');
 
   $aref->each_value(sub { $_ = uc });
   is($aref->[0], 'FOO', "we can alter values directly");
@@ -56,3 +64,16 @@ use Moose::Autobox;
   $aref->each_key(sub { $_ = $_ + 1 });
   ok(! $aref->[3], "we cannot alter keys directly");
 }
+
+{
+    my $string = 'foo';
+    my @values;
+    $string->each_value(sub { push @values, $_ });
+    is_deeply( [@values], [$string], 'each_values for scalar using $_' );
+}
+{
+    my $string = 'foo';
+    my @values;
+    $string->each_value(sub { push @values, $_[0] });
+    is_deeply( [@values], [$string], 'each_values for scalar using $_[0]' );
+}