From: Ricardo Signes Date: Mon, 12 Oct 2009 15:19:47 +0000 (-0400) Subject: tests for each_* X-Git-Tag: 0_10~4 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=2224d5b445f96b33b6b8aa1ea9455b585f515cf5;hp=2dcdd7d70c44b88f199af083cc1ee68c072b5a16;p=gitmo%2FMoose-Autobox.git tests for each_* --- diff --git a/lib/Moose/Autobox/Array.pm b/lib/Moose/Autobox/Array.pm index 21ce08a..ae96b49 100644 --- a/lib/Moose/Autobox/Array.pm +++ b/lib/Moose/Autobox/Array.pm @@ -126,9 +126,7 @@ sub each { sub each_key { my ($array, $sub) = @_; - for my $i (0 .. $#$array) { - $sub->($i); - } + $sub->($_) for (0 .. $#$array); } sub each_value { diff --git a/t/010_each.t b/t/010_each.t new file mode 100644 index 0000000..5c1f02d --- /dev/null +++ b/t/010_each.t @@ -0,0 +1,58 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 11; + +require_ok('Moose::Autobox'); + +use Moose::Autobox; + +{ + my %hash = ( + a => 1, + b => 2, + c => 3, + ); + + my $href = { %hash }; + + is_deeply($href, \%hash, "sanity check to start"); + + my @keys; + $href->each_key(sub { push @keys, $_ }); + is_deeply([ sort @keys ], [ sort keys %hash ], "keys found via each_key"); + + my @values; + $href->each_value(sub { push @values, $_ }); + is_deeply([ sort @values ], [ sort values %hash ], "values via each_values"); + + $href->each_value(sub { $_++ }); + is($href->{a}, 2, "we can ++ values directly"); + + $href->each_key(sub { $_ = "$_$_" }); + ok(! exists $href->{aa}, "we cannot alter keys directly"); +} + +{ + my @array = qw(foo bar baz); + + my $aref = [ @array ]; + + is_deeply($aref, \@array, "sanity check to start"); + + my @keys; + $aref->each_key(sub { push @keys, $_ }); + is_deeply([ @keys ], [ 0, 1, 2 ], "keys found via each_key"); + + my @values; + $aref->each_value(sub { push @values, $_ }); + is_deeply([ @values ], [ @array ], "values via each_values"); + + $aref->each_value(sub { $_ = uc }); + is($aref->[0], 'FOO', "we can alter values directly"); + + $aref->each_key(sub { $_ = $_ + 1 }); + ok(! $aref->[3], "we cannot alter keys directly"); +}