File: 010_each.t

package info (click to toggle)
libmoose-autobox-perl 0.12-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 308 kB
  • sloc: perl: 2,454; makefile: 2
file content (58 lines) | stat: -rw-r--r-- 1,280 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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");
}