8 use Test::Moose 'does_ok';
16 add_options => 'push',
17 add_options_with_speed =>
18 [ push => 'funrolls', 'funbuns' ],
19 remove_last_option => 'pop',
20 remove_first_option => 'shift',
21 insert_options => 'unshift',
22 prepend_prerequisites_along_with =>
23 [ unshift => 'first', 'second' ],
24 get_option_at => 'get',
25 set_option_at => 'set',
26 num_options => 'count',
27 options => 'elements',
28 has_no_options => 'is_empty',
29 clear_options => 'clear',
30 splice_options => 'splice',
31 sort_options_in_place => 'sort_in_place',
32 option_accessor => 'accessor',
34 [ sort_in_place => ( $sort = sub { $_[1] <=> $_[0] } ) ],
36 up_by_one => [ map => ( $up = sub { $_ + 1 } ) ],
37 filter_options => 'grep',
38 less_than_five => [ grep => ( $less = sub { $_ < 5 } ) ],
39 find_option => 'first',
40 join_options => 'join',
41 dashify => [ join => '-' ],
42 sorted_options => 'sort',
43 randomized_options => 'shuffle',
44 unique_options => 'uniq',
45 pairwise_options => [ natatime => 2 ],
47 product => [ reduce => ( $prod = sub { $_[0] * $_[1] } ) ],
58 isa => 'ArrayRef[Str]',
59 default => sub { [] },
65 my $stuff = Stuff->new( _options => [ 10, 12 ] );
66 isa_ok( $stuff, 'Stuff' );
68 can_ok( $stuff, $_ ) for sort keys %handles;
70 is_deeply( $stuff->_options, [ 10, 12 ], '... got options' );
72 ok( !$stuff->has_no_options, '... we have options' );
73 is( $stuff->num_options, 2, '... got 2 options' );
75 is( $stuff->remove_last_option, 12, '... removed the last option' );
76 is( $stuff->remove_first_option, 10, '... removed the last option' );
78 is_deeply( $stuff->_options, [], '... no options anymore' );
80 ok( $stuff->has_no_options, '... no options' );
81 is( $stuff->num_options, 0, '... got no options' );
84 $stuff->add_options( 1, 2, 3 );
86 '... set the option okay';
88 is_deeply( $stuff->_options, [ 1, 2, 3 ], '... got options now' );
90 [ $stuff->options ], [ 1, 2, 3 ],
91 '... got options now (with elements method)'
94 ok( !$stuff->has_no_options, '... has options' );
95 is( $stuff->num_options, 3, '... got 3 options' );
97 is( $stuff->get_option_at(0), 1, '... get option at index 0' );
98 is( $stuff->get_option_at(1), 2, '... get option at index 1' );
99 is( $stuff->get_option_at(2), 3, '... get option at index 2' );
101 throws_ok { $stuff->get_option_at() }
102 qr/Must provide a valid index number as an argument/,
103 'throws an error when get_option_at is called without any arguments';
105 throws_ok { $stuff->get_option_at( {} ) }
106 qr/Must provide a valid index number as an argument/,
107 'throws an error when get_option_at is called with an invalid argument';
109 throws_ok { $stuff->get_option_at(2.2) }
110 qr/Must provide a valid index number as an argument/,
111 'throws an error when get_option_at is called with an invalid argument';
113 throws_ok { $stuff->get_option_at('foo') }
114 qr/Must provide a valid index number as an argument/,
115 'throws an error when get_option_at is called with an invalid argument';
118 $stuff->set_option_at( 1, 100 );
120 '... set the option okay';
122 is( $stuff->get_option_at(1), 100, '... get option at index 1' );
125 $stuff->add_options( 10, 15 );
127 '... set the option okay';
130 $stuff->_options, [ 1, 100, 3, 10, 15 ],
131 '... got more options now'
134 is( $stuff->num_options, 5, '... got 5 options' );
136 is( $stuff->remove_last_option, 15, '... removed the last option' );
138 is( $stuff->num_options, 4, '... got 4 options' );
140 $stuff->_options, [ 1, 100, 3, 10 ],
141 '... got diff options now'
145 $stuff->insert_options( 10, 20 );
147 '... set the option okay';
149 is( $stuff->num_options, 6, '... got 6 options' );
151 $stuff->_options, [ 10, 20, 1, 100, 3, 10 ],
152 '... got diff options now'
155 is( $stuff->get_option_at(0), 10, '... get option at index 0' );
156 is( $stuff->get_option_at(1), 20, '... get option at index 1' );
157 is( $stuff->get_option_at(3), 100, '... get option at index 3' );
159 is( $stuff->remove_first_option, 10, '... getting the first option' );
161 is( $stuff->num_options, 5, '... got 5 options' );
162 is( $stuff->get_option_at(0), 20, '... get option at index 0' );
164 $stuff->clear_options;
165 is_deeply( $stuff->_options, [], "... clear options" );
167 $stuff->add_options( 5, 1, 2, 3 );
168 $stuff->sort_options_in_place;
170 $stuff->_options, [ 1, 2, 3, 5 ],
171 "... sort options in place (default sort order)"
174 $stuff->sort_options_in_place( sub { $_[1] <=> $_[0] } );
176 $stuff->_options, [ 5, 3, 2, 1 ],
177 "... sort options in place (descending order)"
180 $stuff->clear_options();
181 $stuff->add_options( 5, 1, 2, 3 );
183 $stuff->descending_options();
185 '... curried sort in place lives ok';
187 is_deeply( $stuff->_options, [ 5, 3, 2, 1 ], "... sort currying" );
189 throws_ok { $stuff->sort_options_in_place('foo') }
190 qr/Argument must be a code reference/,
191 'error when sort_in_place receives a non-coderef argument';
193 $stuff->clear_options;
196 $stuff->add_options('tree');
198 '... set the options okay';
201 $stuff->add_options_with_speed( 'compatible', 'safe' );
203 '... add options with speed okay';
206 $stuff->_options, [qw/tree funrolls funbuns compatible safe/],
207 'check options after add_options_with_speed'
211 $stuff->prepend_prerequisites_along_with();
213 '... add prerequisite options okay';
215 $stuff->clear_options;
216 $stuff->add_options( 1, 2 );
219 $stuff->splice_options( 1, 0, 'foo' );
221 '... splice_options works';
224 $stuff->_options, [ 1, 'foo', 2 ],
225 'splice added expected option'
229 $stuff->option_accessor( 1 => 'foo++' ), 'foo++',
230 'set using accessor method'
232 is( $stuff->option_accessor(1), 'foo++', 'get using accessor method' );
235 $stuff->insert_options(undef);
237 '... could not add an undef where a string is expected';
240 $stuff->set_option( 5, {} );
242 '... could not add a hash ref where a string is expected';
245 Stuff->new( _options => [ undef, 10, undef, 20 ] );
247 '... bad constructor params';
250 my $stuff = Stuff->new();
251 $stuff->add_options(undef);
253 '... rejects push of an invalid type';
256 my $stuff = Stuff->new();
257 $stuff->insert_options(undef);
259 '... rejects unshift of an invalid type';
262 my $stuff = Stuff->new();
263 $stuff->set_option_at( 0, undef );
265 '... rejects set of an invalid type';
268 my $stuff = Stuff->new();
269 $stuff->sort_in_place_options(undef);
271 '... sort rejects arg of invalid type';
274 my $stuff = Stuff->new();
275 $stuff->option_accessor();
277 '... accessor rejects 0 args';
280 my $stuff = Stuff->new();
281 $stuff->option_accessor( 1, 2, 3 );
283 '... accessor rejects 3 args';
287 my $stuff = Stuff->new( _options => [ 1 .. 10 ] );
289 is_deeply( $stuff->_options, [ 1 .. 10 ], '... got options' );
291 ok( !$stuff->has_no_options, '... we have options' );
292 is( $stuff->num_options, 10, '... got 2 options' );
293 cmp_ok( $stuff->get_option_at(0), '==', 1, '... get option 0' );
296 [ $stuff->filter_options( sub { $_ % 2 == 0 } ) ],
298 '... got the right filtered values'
301 throws_ok { $stuff->filter_options() }
302 qr/Must provide a code reference as an argument/,
303 'throws an error when filter_options is called without any arguments';
305 throws_ok { $stuff->filter_options( {} ) }
306 qr/Must provide a code reference as an argument/,
307 'throws an error when filter_options is called with an invalid argument';
310 [ $stuff->map_options( sub { $_ * 2 } ) ],
311 [ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 ],
312 '... got the right mapped values'
315 throws_ok { $stuff->map_options() }
316 qr/Must provide a code reference as an argument/,
317 'throws an error when map_options is called without any arguments';
319 throws_ok { $stuff->map_options( {} ) }
320 qr/Must provide a code reference as an argument/,
321 'throws an error when map_options is called with an invalid argument';
324 $stuff->find_option( sub { $_ % 2 == 0 } ), 2,
325 '.. found the right option'
328 throws_ok { $stuff->find_option() }
329 qr/Must provide a code reference as an argument/,
330 'throws an error when find_option is called without any arguments';
332 throws_ok { $stuff->find_option( {} ) }
333 qr/Must provide a code reference as an argument/,
334 'throws an error when find_option is called with an invalid argument';
337 [ $stuff->options ], [ 1 .. 10 ],
338 '... got the list of options'
342 $stuff->join_options(':'), '1:2:3:4:5:6:7:8:9:10',
343 '... joined the list of options by :'
346 throws_ok { $stuff->join_options() }
347 qr/Must provide a string as an argument/,
348 'throws an error when join_options is called without any arguments';
350 throws_ok { $stuff->join_options( {} ) }
351 qr/Must provide a string as an argument/,
352 'throws an error when join_options is called with an invalid argument';
355 [ $stuff->sorted_options ], [ sort ( 1 .. 10 ) ],
356 '... got sorted options (default sort order)'
359 [ $stuff->sorted_options( sub { $_[1] <=> $_[0] } ) ],
360 [ sort { $b <=> $a } ( 1 .. 10 ) ],
361 '... got sorted options (descending sort order) '
364 throws_ok { $stuff->sorted_options('foo') }
365 qr/Argument must be a code reference/,
366 'error when sort receives a non-coderef argument';
369 [ sort { $a <=> $b } $stuff->randomized_options ],
371 'randomized_options returns all options'
375 $stuff->pairwise_options( sub { push @pairs, [@_] } );
378 [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ], [ 7, 8 ], [ 9, 10 ] ],
379 'pairwise returns pairs as expected'
383 [ $stuff->less_than_five() ], [ 1 .. 4 ],
384 'less_than_five returns 1..4'
388 [ $stuff->up_by_one() ], [ 2 .. 11 ],
389 'up_by_one returns 2..11'
393 $stuff->dashify, '1-2-3-4-5-6-7-8-9-10',
394 'dashify returns options joined by dashes'
398 $stuff->reduce( sub { $_[0] * $_[1] } ),
400 'call reducing to generate a product returns expected value'
403 throws_ok { $stuff->reduce() }
404 qr/Must provide a code reference as an argument/,
405 'throws an error when reduce is called without any arguments';
407 throws_ok { $stuff->reduce( {} ) }
408 qr/Must provide a code reference as an argument/,
409 'throws an error when reduce is called with an invalid argument';
412 $stuff->product, 3628800,
413 'product returns expected value'
416 my $other_stuff = Stuff->new( _options => [ 1, 1, 2, 3, 5 ] );
418 [ $other_stuff->unique_options ], [ 1, 2, 3, 5 ],
419 'unique_options returns unique options'
424 my $options = Stuff->meta->get_attribute('_options');
425 does_ok( $options, 'Moose::Meta::Attribute::Native::Trait::Array' );
428 $options->handles, \%handles,
429 '... got the right handles mapping'
433 $options->type_constraint->type_parameter, 'Str',
434 '... got the right container type'