6 use Test::More tests => 43;
8 use Test::Moose 'does_ok';
21 isa => 'ArrayRef[Int]',
22 init_arg => 'options',
23 default => sub { [] },
25 'num_options' => 'count',
26 'has_no_options' => 'is_empty',
27 'map_options', => 'map',
28 'filter_options' => 'grep',
29 'find_option' => 'first',
30 'options' => 'elements',
31 'join_options' => 'join',
32 'get_option_at' => 'get',
33 'sorted_options' => 'sort',
34 'randomized_options' => 'shuffle',
35 'unique_options' => 'uniq',
36 'less_than_five' => [ grep => ($less = sub { $_ < 5 }) ],
37 'up_by_one' => [ map => ($up = sub { $_ + 1 }) ],
38 'pairwise_options' => [ natatime => 2 ],
39 'dashify' => [ join => '-' ],
40 'descending' => [ sort => ($sort = sub { $_[1] <=> $_[0] }) ],
41 'product' => [ reduce => ($prod = sub { $_[0] * $_[1] }) ],
47 my $stuff = Stuff->new( options => [ 1 .. 10 ] );
48 isa_ok( $stuff, 'Stuff' );
50 can_ok( $stuff, $_ ) for qw[
71 is_deeply( $stuff->_options, [ 1 .. 10 ], '... got options' );
73 ok( !$stuff->has_no_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' );
78 [ $stuff->filter_options( sub { $_ % 2 == 0 } ) ],
80 '... got the right filtered values'
84 [ $stuff->map_options( sub { $_ * 2 } ) ],
85 [ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 ],
86 '... got the right mapped values'
89 is( $stuff->find_option( sub { $_ % 2 == 0 } ), 2,
90 '.. found the right option' );
92 is_deeply( [ $stuff->options ], [ 1 .. 10 ], '... got the list of options' );
94 is( $stuff->join_options(':'), '1:2:3:4:5:6:7:8:9:10',
95 '... joined the list of options by :' );
98 [ $stuff->sorted_options ], [ sort ( 1 .. 10 ) ],
99 '... got sorted options (default sort order)'
102 [ $stuff->sorted_options( sub { $_[1] <=> $_[0] } ) ],
103 [ sort { $b <=> $a } ( 1 .. 10 ) ],
104 '... got sorted options (descending sort order) '
107 throws_ok { $stuff->sorted_options('foo') }
108 qr/Argument must be a code reference/,
109 'error when sort receives a non-coderef argument';
111 is_deeply( [ sort { $a <=> $b } $stuff->randomized_options ], [ 1 .. 10 ] );
114 $stuff->pairwise_options(sub { push @pairs, [@_] });
115 is_deeply( \@pairs, [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ], [ 7, 8 ], [ 9, 10 ] ] );
118 is_deeply( [ $stuff->less_than_five() ], [ 1 .. 4 ] );
120 is_deeply( [ $stuff->up_by_one() ], [ 2 .. 11 ] );
122 is( $stuff->dashify, '1-2-3-4-5-6-7-8-9-10' );
124 is_deeply( [ $stuff->descending ], [ reverse 1 .. 10 ] );
126 is( $stuff->product, 3628800 );
128 my $other_stuff = Stuff->new( options => [ 1, 1, 2, 3, 5 ] );
129 is_deeply( [ $other_stuff->unique_options ], [1, 2, 3, 5] );
133 my $options = $stuff->meta->get_attribute('_options');
134 does_ok( $options, 'Moose::Meta::Attribute::Native::Trait::Array' );
139 'num_options' => 'count',
140 'has_no_options' => 'is_empty',
141 'map_options', => 'map',
142 'filter_options' => 'grep',
143 'find_option' => 'first',
144 'options' => 'elements',
145 'join_options' => 'join',
146 'get_option_at' => 'get',
147 'sorted_options' => 'sort',
148 'randomized_options' => 'shuffle',
149 'unique_options' => 'uniq',
150 'less_than_five' => [ grep => $less ],
151 'up_by_one' => [ map => $up ],
152 'pairwise_options' => [ natatime => 2 ],
153 'dashify' => [ join => '-' ],
154 'descending' => [ sort => $sort ],
155 'product' => [ reduce => $prod ],
157 '... got the right handles mapping'
160 is( $options->type_constraint->type_parameter, 'Int',
161 '... got the right container type' );
164 $stuff->sort_in_place_options(undef);
166 '... sort rejects arg of invalid type';