83e6324d37a76847ff0aaf174dbc96865360c034
[gitmo/MooseX-AttributeHelpers.git] / t / 005_basic_list.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More no_plan => 1;
7 use Test::Exception;
8
9 BEGIN {
10     use_ok('MooseX::AttributeHelpers');   
11 }
12
13 {
14     package Stuff;
15     use Moose;
16
17     has 'options' => (
18         metaclass => 'Collection::List',
19         is        => 'ro',
20         isa       => 'ArrayRef[Int]',
21         default   => sub { [] },
22         provides  => {
23             'count'   => 'num_options',
24             'empty'   => 'has_options',        
25             'map'     => 'map_options',
26             'grep'    => 'filter_options',
27             'find'    => 'find_option',
28         }
29     );
30 }
31
32 my $stuff = Stuff->new(options => [ 1 .. 10 ]);
33 isa_ok($stuff, 'Stuff');
34
35 can_ok($stuff, $_) for qw[
36     num_options
37     has_options
38     map_options
39     filter_options
40     find_option
41 ];
42
43 is_deeply($stuff->options, [1 .. 10], '... got options');
44
45 ok($stuff->has_options, '... we have options');
46 is($stuff->num_options, 10, '... got 2 options');
47
48 is_deeply(
49 [ $stuff->filter_options(sub { $_[0] % 2 == 0 }) ],
50 [ 2, 4, 6, 8, 10 ],
51 '... got the right filtered values'
52 );
53
54 is_deeply(
55 [ $stuff->map_options(sub { $_[0] * 2 }) ],
56 [ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 ],
57 '... got the right mapped values'
58 );
59
60 is($stuff->find_option(sub { $_[0] % 2 == 0 }), 2, '.. found the right option');
61
62 ## test the meta
63
64 my $options = $stuff->meta->get_attribute('options');
65 isa_ok($options, 'MooseX::AttributeHelpers::Collection::List');
66
67 is_deeply($options->provides, {
68     'map'     => 'map_options',
69     'grep'    => 'filter_options',
70     'find'    => 'find_option',
71     'count'   => 'num_options',
72     'empty'   => 'has_options',    
73 }, '... got the right provies mapping');
74
75 is($options->container_type, 'Int', '... got the right container type');