6 use Test::More tests => 35;
8 use Test::Moose 'does_ok';
11 use_ok('MooseX::AttributeHelpers');
19 traits => [qw/MooseX::AttributeHelpers::Trait::Collection::List/],
21 isa => 'ArrayRef[Int]',
22 init_arg => 'options',
23 default => sub { [] },
25 'count' => 'num_options',
26 'empty' => 'has_options',
27 'map' => 'map_options',
28 'grep' => 'filter_options',
29 'find' => 'find_option',
30 'elements' => 'options',
31 'join' => 'join_options',
32 'get' => 'get_option_at',
33 'first' => 'get_first_option',
34 'last' => 'get_last_option',
35 'sort' => 'sorted_options',
38 'grep' => {less_than_five => [ sub { $_ < 5 } ]},
39 'map' => {up_by_one => [ sub { $_ + 1 } ]},
40 'join' => {dashify => [ '-' ]},
41 'sort' => {descending => [ sub { $_[1] <=> $_[0] } ]},
47 isa => 'ArrayRef[Str]',
48 metaclass => 'Collection::List',
51 double_length_of => sub {
52 my ($self, $body, $arg) = @_;
54 $body->($self, sub { length($_) == length($arg) * 2 });
61 my $stuff = Stuff->new(options => [ 1 .. 10 ]);
62 isa_ok($stuff, 'Stuff');
64 can_ok($stuff, $_) for qw[
77 is_deeply($stuff->_options, [1 .. 10], '... got options');
79 ok($stuff->has_options, '... we have options');
80 is($stuff->num_options, 10, '... got 2 options');
81 cmp_ok($stuff->get_option_at(0), '==', 1, '... get option 0');
82 cmp_ok($stuff->get_first_option, '==', 1, '... get first');
83 cmp_ok($stuff->get_last_option, '==', 10, '... get last');
86 [ $stuff->filter_options(sub { $_[0] % 2 == 0 }) ],
88 '... got the right filtered values'
92 [ $stuff->map_options(sub { $_[0] * 2 }) ],
93 [ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 ],
94 '... got the right mapped values'
97 is($stuff->find_option(sub { $_[0] % 2 == 0 }), 2, '.. found the right option');
99 is_deeply([ $stuff->options ], [1 .. 10], '... got the list of options');
101 is($stuff->join_options(':'), '1:2:3:4:5:6:7:8:9:10', '... joined the list of options by :');
103 is_deeply([ $stuff->sorted_options ], [sort (1..10)],
104 '... got sorted options (default sort order)');
105 is_deeply([ $stuff->sorted_options( sub { $_[1] <=> $_[0] } ) ], [sort { $b <=> $a } (1..10)],
106 '... got sorted options (descending sort order) ');
108 throws_ok { $stuff->sorted_options('foo') } qr/Argument must be a code reference/,
109 'error when sort receives a non-coderef argument';
112 is_deeply([ $stuff->less_than_five() ], [1 .. 4]);
114 is_deeply([ $stuff->up_by_one() ], [2 .. 11]);
116 is($stuff->dashify, '1-2-3-4-5-6-7-8-9-10');
118 $stuff->animals([ qw/cat duck horse cattle gorilla elephant flamingo kangaroo/ ]);
122 [ sort $stuff->double_length_of('fish') ],
123 [ sort qw/elephant flamingo kangaroo/ ],
124 'returns all elements with double length of string "fish"'
127 is_deeply([$stuff->descending], [reverse 1 .. 10]);
131 my $options = $stuff->meta->get_attribute('_options');
132 does_ok($options, 'MooseX::AttributeHelpers::Trait::Collection::List');
134 is_deeply($options->provides, {
135 'map' => 'map_options',
136 'grep' => 'filter_options',
137 'find' => 'find_option',
138 'count' => 'num_options',
139 'empty' => 'has_options',
140 'elements' => 'options',
141 'join' => 'join_options',
142 'get' => 'get_option_at',
143 'first' => 'get_first_option',
144 'last' => 'get_last_option',
145 'sort' => 'sorted_options',
146 }, '... got the right provides mapping');
148 is($options->type_constraint->type_parameter, 'Int', '... got the right container type');
151 $stuff->sort_in_place_options( undef );
152 } '... sort rejects arg of invalid type';