14 use_ok('MooseX::AttributeHelpers');
22 metaclass => 'Collection::List',
24 isa => 'ArrayRef[Int]',
25 init_arg => 'options',
26 default => sub { [] },
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 'sort' => 'sort_options',
41 'grep' => {less_than_five => [ sub { $_ < 5 } ]},
42 'map' => {up_by_one => [ sub { $_ + 1 } ]},
43 'join' => {dashify => [ '-' ]},
44 'sort' => {ascending => [ sub { $_[0] <=> $_[1] } ]},
50 isa => 'ArrayRef[Str]',
51 metaclass => 'Collection::List',
54 double_length_of => sub {
55 my ($self, $body, $arg) = @_;
57 $body->($self, sub { length($_) == length($arg) * 2 });
64 my $stuff = Stuff->new(options => [ 1 .. 10 ]);
65 isa_ok($stuff, 'Stuff');
67 can_ok($stuff, $_) for qw[
80 is_deeply($stuff->_options, [1 .. 10], '... got options');
82 ok($stuff->has_options, '... we have options');
83 is($stuff->num_options, 10, '... got 2 options');
84 cmp_ok($stuff->get_option_at(0), '==', 1, '... get option 0');
85 cmp_ok($stuff->get_first_option, '==', 1, '... get first');
86 cmp_ok($stuff->get_last_option, '==', 10, '... get last');
89 [ $stuff->filter_options(sub { $_[0] % 2 == 0 }) ],
91 '... got the right filtered values'
95 [ $stuff->map_options(sub { $_[0] * 2 }) ],
96 [ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 ],
97 '... got the right mapped values'
100 is($stuff->find_option(sub { $_[0] % 2 == 0 }), 2, '.. found the right option');
102 is_deeply([ $stuff->options ], [1 .. 10], '... got the list of options');
104 is($stuff->join_options(':'), '1:2:3:4:5:6:7:8:9:10', '... joined the list of options by :');
106 is_deeply([ $stuff->sort_options( sub { $_[1] <=> $_[0] } ) ], [sort { $b <=> $a } (1..10)], '... got sorted options');
109 is_deeply([ $stuff->less_than_five() ], [1 .. 4]);
111 is_deeply([ $stuff->up_by_one() ], [2 .. 11]);
113 is($stuff->dashify, '1-2-3-4-5-6-7-8-9-10');
115 $stuff->animals([ qw/cat duck horse cattle gorilla elephant flamingo kangaroo/ ]);
119 [ sort $stuff->double_length_of('fish') ],
120 [ sort qw/elephant flamingo kangaroo/ ],
121 'returns all elements with double length of string "fish"'
124 is_deeply([$stuff->ascending], [1 .. 10]);
128 my $options = $stuff->meta->get_attribute('_options');
129 isa_ok($options, 'MooseX::AttributeHelpers::Collection::List');
131 is_deeply($options->provides, {
132 'map' => 'map_options',
133 'grep' => 'filter_options',
134 'find' => 'find_option',
135 'count' => 'num_options',
136 'empty' => 'has_options',
137 'elements' => 'options',
138 'join' => 'join_options',
139 'get' => 'get_option_at',
140 'first' => 'get_first_option',
141 'last' => 'get_last_option',
142 'sort' => 'sort_options',
143 }, '... got the right provies mapping');
145 is($options->type_constraint->type_parameter, 'Int', '... got the right container type');
148 $stuff->sort_in_place_options( undef );
149 } '... sort rejects arg of invalid type';