Add Array->each_n.
[gitmo/Moose-Autobox.git] / t / 010_each.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 11;
7
8 require_ok('Moose::Autobox');
9
10 use Moose::Autobox;
11
12 {
13   my %hash = (
14     a => 1,
15     b => 2,
16     c => 3,
17   );
18
19   my $href = { %hash };
20
21   is_deeply($href, \%hash, "sanity check to start");
22
23   my @keys;
24   $href->each_key(sub { push @keys, $_ });
25   is_deeply([ sort @keys ], [ sort keys %hash ], "keys found via each_key");
26
27   my @values;
28   $href->each_value(sub { push @values, $_ });
29   is_deeply([ sort @values ], [ sort values %hash ], "values via each_values");
30
31   $href->each_value(sub { $_++ });
32   is($href->{a}, 2, "we can ++ values directly");
33
34   $href->each_key(sub { $_ = "$_$_" });
35   ok(! exists $href->{aa}, "we cannot alter keys directly");
36 }
37
38 {
39   my @array = qw(foo bar baz);
40
41   my $aref = [ @array ];
42
43   is_deeply($aref, \@array, "sanity check to start");
44
45   my @keys;
46   $aref->each_key(sub { push @keys, $_ });
47   is_deeply([ @keys ], [ 0, 1, 2 ], "keys found via each_key");
48
49   my @values;
50   $aref->each_value(sub { push @values, $_ });
51   is_deeply([ @values ], [ @array ], "values via each_values");
52
53   $aref->each_value(sub { $_ = uc });
54   is($aref->[0], 'FOO', "we can alter values directly");
55
56   $aref->each_key(sub { $_ = $_ + 1 });
57   ok(! $aref->[3], "we cannot alter keys directly");
58 }