6 use Test::More tests => 35;
10 use_ok('Moose::AttributeHelpers');
18 metaclass => 'Collection::List',
20 isa => 'ArrayRef[Int]',
21 init_arg => 'options',
22 default => sub { [] },
24 'num_options' => 'count',
25 'has_options' => 'empty',
26 'map_options', => 'map',
27 'filter_options' => 'grep',
28 'find_option' => 'find',
29 'options' => 'elements',
30 'join_options' => 'join',
31 'get_option_at' => 'get',
32 'get_first_option' => 'first',
33 'get_last_option' => 'last',
34 'sorted_options' => 'sort',
35 'less_than_five' => [ grep => [ sub { $_ < 5 } ] ],
36 'up_by_one' => [ map => [ sub { $_ + 1 } ] ],
37 'dashify' => [ join => [ '-' ] ],
38 'descending' => [ sort => [ sub { $_[1] <=> $_[0] ] ],
44 isa => 'ArrayRef[Str]',
45 metaclass => 'Collection::List',
47 double_length_of => [ grep => [ sub {
48 my ($self, $body, $arg) = @_;
49 $body->($self, sub { length($_) == length($arg) * 2 });
55 my $stuff = Stuff->new(options => [ 1 .. 10 ]);
56 isa_ok($stuff, 'Stuff');
58 can_ok($stuff, $_) for qw[
71 is_deeply($stuff->_options, [1 .. 10], '... got options');
73 ok($stuff->has_options, '... we have options');
74 is($stuff->num_options, 10, '... got 2 options');
75 cmp_ok($stuff->get_option_at(0), '==', 1, '... get option 0');
76 cmp_ok($stuff->get_first_option, '==', 1, '... get first');
77 cmp_ok($stuff->get_last_option, '==', 10, '... get last');
80 [ $stuff->filter_options(sub { $_[0] % 2 == 0 }) ],
82 '... got the right filtered values'
86 [ $stuff->map_options(sub { $_[0] * 2 }) ],
87 [ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 ],
88 '... got the right mapped values'
91 is($stuff->find_option(sub { $_[0] % 2 == 0 }), 2, '.. found the right option');
93 is_deeply([ $stuff->options ], [1 .. 10], '... got the list of options');
95 is($stuff->join_options(':'), '1:2:3:4:5:6:7:8:9:10', '... joined the list of options by :');
97 is_deeply([ $stuff->sorted_options ], [sort (1..10)],
98 '... got sorted options (default sort order)');
99 is_deeply([ $stuff->sorted_options( sub { $_[1] <=> $_[0] } ) ], [sort { $b <=> $a } (1..10)],
100 '... got sorted options (descending sort order) ');
102 throws_ok { $stuff->sorted_options('foo') } qr/Argument must be a code reference/,
103 'error when sort receives a non-coderef argument';
106 is_deeply([ $stuff->less_than_five() ], [1 .. 4]);
108 is_deeply([ $stuff->up_by_one() ], [2 .. 11]);
110 is($stuff->dashify, '1-2-3-4-5-6-7-8-9-10');
112 $stuff->animals([ qw/cat duck horse cattle gorilla elephant flamingo kangaroo/ ]);
116 [ sort $stuff->double_length_of('fish') ],
117 [ sort qw/elephant flamingo kangaroo/ ],
118 'returns all elements with double length of string "fish"'
121 is_deeply([$stuff->descending], [reverse 1 .. 10]);
125 my $options = $stuff->meta->get_attribute('_options');
126 isa_ok($options, 'Moose::AttributeHelpers::Collection::List');
128 is_deeply($options->handles, {
129 'num_options' => 'count',
130 'has_options' => 'empty',
131 'map_options', => 'map',
132 'filter_options' => 'grep',
133 'find_option' => 'find',
134 'options' => 'elements',
135 'join_options' => 'join',
136 'get_option_at' => 'get',
137 'get_first_option' => 'first',
138 'get_last_option' => 'last',
139 'sorted_options' => 'sort',
140 'less_than_five' => [ grep ],
141 'up_by_one' => [ map ],
142 'dashify' => [ join ],
143 'descending' => [ sort ],
144 }, '... got the right handles mapping');
146 is($options->type_constraint->type_parameter, 'Int', '... got the right container type');
149 $stuff->sort_in_place_options( undef );
150 } '... sort rejects arg of invalid type';