f8a16c96a364099331f8aa6ae3dcf01970791cde
[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;
7 use Test::Exception;
8
9 BEGIN {
10     plan tests => 36;
11 }
12
13 BEGIN {
14     use_ok('MooseX::AttributeHelpers');   
15 }
16
17 {
18     package Stuff;
19     use Moose;
20
21     has '_options' => (
22         metaclass => 'Collection::List',
23         is        => 'ro',
24         isa       => 'ArrayRef[Int]',
25         init_arg  => 'options',
26         default   => sub { [] },
27         provides  => {
28             'count'    => 'num_options',
29             'empty'    => 'has_options',        
30             'map'      => 'map_options',
31             'grep'     => 'filter_options',
32             'find'     => 'find_option',
33             'elements' => 'options',
34             'join'     => 'join_options',
35             'get'      => 'get_option_at',
36             'first'    => 'get_first_option',
37             'last'     => 'get_last_option',
38             'contains' => 'options_contains',
39         },
40         curries   => {
41             'grep'     => {less_than_five => [ sub { $_ < 5 } ]},
42             'map'      => {up_by_one      => [ sub { $_ + 1 } ]},
43             'join'     => {dashify        => [ '-' ]}
44         }
45     );
46
47     has animals => (
48         is       => 'rw',
49         isa      => 'ArrayRef[Str]',
50         metaclass => 'Collection::List',
51         curries => {
52             grep =>  {
53                 double_length_of => sub {
54                     my ($self, $body, $arg) = @_;
55
56                     $body->($self, sub { length($_) == length($arg) * 2 });
57                 }
58             }
59         }
60     )
61 }
62
63 my $stuff = Stuff->new(options => [ 1 .. 10 ]);
64 isa_ok($stuff, 'Stuff');
65
66 can_ok($stuff, $_) for qw[
67     _options
68     num_options
69     has_options
70     map_options
71     filter_options
72     find_option
73     options
74     join_options
75     get_option_at
76     options_contains
77 ];
78
79 is_deeply($stuff->_options, [1 .. 10], '... got options');
80
81 ok($stuff->has_options, '... we have options');
82 is($stuff->num_options, 10, '... got 2 options');
83 cmp_ok($stuff->get_option_at(0), '==', 1, '... get option 0');
84 cmp_ok($stuff->get_first_option, '==', 1, '... get first');
85 cmp_ok($stuff->get_last_option, '==', 10, '... get first');
86
87 is_deeply(
88 [ $stuff->filter_options(sub { $_[0] % 2 == 0 }) ],
89 [ 2, 4, 6, 8, 10 ],
90 '... got the right filtered values'
91 );
92
93 is_deeply(
94 [ $stuff->map_options(sub { $_[0] * 2 }) ],
95 [ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 ],
96 '... got the right mapped values'
97 );
98
99 is($stuff->find_option(sub { $_[0] % 2 == 0 }), 2, '.. found the right option');
100
101 is_deeply([ $stuff->options ], [1 .. 10], '... got the list of options');
102
103 is($stuff->join_options(':'), '1:2:3:4:5:6:7:8:9:10', '... joined the list of options by :');
104
105 is($stuff->options_contains(),0,'... does not contain undef');
106 is($stuff->options_contains(5),1,'... contains "5"');
107 is($stuff->options_contains(11),0,'... does not contain "11"');
108 push @{$stuff->_options}, undef;
109 is($stuff->options_contains(undef),1,'... does contain undef');
110 push @{$stuff->_options}, 5;
111 is($stuff->options_contains(5),2,'... contains returns count');
112 splice @{$stuff->_options}, -2;
113 is_deeply($stuff->_options, [1 .. 10], '... reset list for regression');
114
115 # test the currying
116 is_deeply([ $stuff->less_than_five() ], [1 .. 4]);
117
118 is_deeply([ $stuff->up_by_one() ], [2 .. 11]);
119
120 is($stuff->dashify, '1-2-3-4-5-6-7-8-9-10');
121
122 $stuff->animals([ qw/cat duck horse cattle gorilla elephant flamingo kangaroo/ ]);
123
124 # 4 * 2 = 8
125 is_deeply(
126         [ sort $stuff->double_length_of('fish') ],
127         [ sort qw/elephant flamingo kangaroo/ ],
128         'returns all elements with double length of string "fish"'
129 );
130
131 ## test the meta
132
133 my $options = $stuff->meta->get_attribute('_options');
134 isa_ok($options, 'MooseX::AttributeHelpers::Collection::List');
135
136 is_deeply($options->provides, {
137     'map'      => 'map_options',
138     'grep'     => 'filter_options',
139     'find'     => 'find_option',
140     'count'    => 'num_options',
141     'empty'    => 'has_options',
142     'elements' => 'options',
143     'join'     => 'join_options',
144     'get'      => 'get_option_at',
145     'first'    => 'get_first_option',
146     'last'     => 'get_last_option',
147     'contains' => 'options_contains',
148 }, '... got the right provies mapping');
149
150 is($options->type_constraint->type_parameter, 'Int', '... got the right container type');