6 use Test::More tests => 35;
10 use_ok('MooseX::AttributeHelpers');
18 metaclass => 'Collection::List',
20 isa => 'ArrayRef[Int]',
21 init_arg => 'options',
22 default => sub { [] },
24 'count' => 'num_options',
25 'empty' => 'has_options',
26 'map' => 'map_options',
27 'grep' => 'filter_options',
28 'find' => 'find_option',
29 'elements' => 'options',
30 'join' => 'join_options',
31 'get' => 'get_option_at',
32 'first' => 'get_first_option',
33 'last' => 'get_last_option',
34 'sort' => 'sorted_options',
37 'grep' => {less_than_five => [ sub { $_ < 5 } ]},
38 'map' => {up_by_one => [ sub { $_ + 1 } ]},
39 'join' => {dashify => [ '-' ]},
40 'sort' => {descending => [ sub { $_[1] <=> $_[0] } ]},
46 isa => 'ArrayRef[Str]',
47 metaclass => 'Collection::List',
50 double_length_of => sub {
51 my ($self, $body, $arg) = @_;
53 $body->($self, sub { length($_) == length($arg) * 2 });
60 my $stuff = Stuff->new(options => [ 1 .. 10 ]);
61 isa_ok($stuff, 'Stuff');
63 can_ok($stuff, $_) for qw[
76 is_deeply($stuff->_options, [1 .. 10], '... got options');
78 ok($stuff->has_options, '... we have options');
79 is($stuff->num_options, 10, '... got 2 options');
80 cmp_ok($stuff->get_option_at(0), '==', 1, '... get option 0');
81 cmp_ok($stuff->get_first_option, '==', 1, '... get first');
82 cmp_ok($stuff->get_last_option, '==', 10, '... get last');
85 [ $stuff->filter_options(sub { $_[0] % 2 == 0 }) ],
87 '... got the right filtered values'
91 [ $stuff->map_options(sub { $_[0] * 2 }) ],
92 [ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 ],
93 '... got the right mapped values'
96 is($stuff->find_option(sub { $_[0] % 2 == 0 }), 2, '.. found the right option');
98 is_deeply([ $stuff->options ], [1 .. 10], '... got the list of options');
100 is($stuff->join_options(':'), '1:2:3:4:5:6:7:8:9:10', '... joined the list of options by :');
102 is_deeply([ $stuff->sorted_options ], [sort (1..10)],
103 '... got sorted options (default sort order)');
104 is_deeply([ $stuff->sorted_options( sub { $_[1] <=> $_[0] } ) ], [sort { $b <=> $a } (1..10)],
105 '... got sorted options (descending sort order) ');
107 throws_ok { $stuff->sorted_options('foo') } qr/Argument must be a code reference/,
108 'error when sort receives a non-coderef argument';
111 is_deeply([ $stuff->less_than_five() ], [1 .. 4]);
113 is_deeply([ $stuff->up_by_one() ], [2 .. 11]);
115 is($stuff->dashify, '1-2-3-4-5-6-7-8-9-10');
117 $stuff->animals([ qw/cat duck horse cattle gorilla elephant flamingo kangaroo/ ]);
121 [ sort $stuff->double_length_of('fish') ],
122 [ sort qw/elephant flamingo kangaroo/ ],
123 'returns all elements with double length of string "fish"'
126 is_deeply([$stuff->descending], [reverse 1 .. 10]);
130 my $options = $stuff->meta->get_attribute('_options');
131 isa_ok($options, 'MooseX::AttributeHelpers::Collection::List');
133 is_deeply($options->provides, {
134 'map' => 'map_options',
135 'grep' => 'filter_options',
136 'find' => 'find_option',
137 'count' => 'num_options',
138 'empty' => 'has_options',
139 'elements' => 'options',
140 'join' => 'join_options',
141 'get' => 'get_option_at',
142 'first' => 'get_first_option',
143 'last' => 'get_last_option',
144 'sort' => 'sorted_options',
145 }, '... got the right provides mapping');
147 is($options->type_constraint->type_parameter, 'Int', '... got the right container type');
150 $stuff->sort_in_place_options( undef );
151 } '... sort rejects arg of invalid type';