6 use Test::More tests => 68;
8 use Test::Moose 'does_ok';
16 use Moose::AttributeHelpers;
19 traits => ['Collection::Array'],
21 isa => 'ArrayRef[Str]',
22 default => sub { [] },
24 'add_options' => 'push',
25 'remove_last_option' => 'pop',
26 'remove_first_option' => 'shift',
27 'insert_options' => 'unshift',
28 'get_option_at' => 'get',
29 'set_option_at' => 'set',
30 'num_options' => 'count',
31 'has_options' => 'empty',
32 'clear_options' => 'clear',
33 'splice_options' => 'splice',
34 'sort_options_in_place' => 'sort_in_place',
35 'option_accessor' => 'accessor',
36 'add_options_with_speed' =>
37 [ 'push' => [ 'funrolls', 'funbuns' ] ],
38 'prepend_prerequisites_along_with' =>
39 [ 'unshift' => [ 'first', 'second' ] ],
40 'descending_options' =>
41 [ 'sort_in_place' => [ $sort = sub { $_[1] <=> $_[0] } ] ],
46 my $stuff = Stuff->new( options => [ 10, 12 ] );
47 isa_ok( $stuff, 'Stuff' );
49 can_ok( $stuff, $_ ) for qw[
63 is_deeply( $stuff->options, [ 10, 12 ], '... got options' );
65 ok( $stuff->has_options, '... we have options' );
66 is( $stuff->num_options, 2, '... got 2 options' );
68 is( $stuff->remove_last_option, 12, '... removed the last option' );
69 is( $stuff->remove_first_option, 10, '... removed the last option' );
71 is_deeply( $stuff->options, [], '... no options anymore' );
73 ok( !$stuff->has_options, '... no options' );
74 is( $stuff->num_options, 0, '... got no options' );
77 $stuff->add_options( 1, 2, 3 );
79 '... set the option okay';
81 is_deeply( $stuff->options, [ 1, 2, 3 ], '... got options now' );
83 ok( $stuff->has_options, '... no options' );
84 is( $stuff->num_options, 3, '... got 3 options' );
86 is( $stuff->get_option_at(0), 1, '... get option at index 0' );
87 is( $stuff->get_option_at(1), 2, '... get option at index 1' );
88 is( $stuff->get_option_at(2), 3, '... get option at index 2' );
91 $stuff->set_option_at( 1, 100 );
93 '... set the option okay';
95 is( $stuff->get_option_at(1), 100, '... get option at index 1' );
98 $stuff->add_options( 10, 15 );
100 '... set the option okay';
102 is_deeply( $stuff->options, [ 1, 100, 3, 10, 15 ],
103 '... got more options now' );
105 is( $stuff->num_options, 5, '... got 5 options' );
107 is( $stuff->remove_last_option, 15, '... removed the last option' );
109 is( $stuff->num_options, 4, '... got 4 options' );
110 is_deeply( $stuff->options, [ 1, 100, 3, 10 ], '... got diff options now' );
113 $stuff->insert_options( 10, 20 );
115 '... set the option okay';
117 is( $stuff->num_options, 6, '... got 6 options' );
118 is_deeply( $stuff->options, [ 10, 20, 1, 100, 3, 10 ],
119 '... got diff options now' );
121 is( $stuff->get_option_at(0), 10, '... get option at index 0' );
122 is( $stuff->get_option_at(1), 20, '... get option at index 1' );
123 is( $stuff->get_option_at(3), 100, '... get option at index 3' );
125 is( $stuff->remove_first_option, 10, '... getting the first option' );
127 is( $stuff->num_options, 5, '... got 5 options' );
128 is( $stuff->get_option_at(0), 20, '... get option at index 0' );
130 $stuff->clear_options;
131 is_deeply( $stuff->options, [], "... clear options" );
133 $stuff->add_options( 5, 1, 2, 3 );
134 $stuff->sort_options_in_place;
135 is_deeply( $stuff->options, [ 1, 2, 3, 5 ],
136 "... sort options in place (default sort order)" );
138 $stuff->sort_options_in_place( sub { $_[1] <=> $_[0] } );
139 is_deeply( $stuff->options, [ 5, 3, 2, 1 ],
140 "... sort options in place (descending order)" );
142 $stuff->clear_options();
143 $stuff->add_options( 5, 1, 2, 3 );
145 $stuff->descending_options();
147 '... curried sort in place lives ok';
149 is_deeply( $stuff->options, [ 5, 3, 2, 1 ], "... sort currying" );
151 throws_ok { $stuff->sort_options_in_place('foo') }
152 qr/Argument must be a code reference/,
153 'error when sort_in_place receives a non-coderef argument';
155 $stuff->clear_options;
158 $stuff->add_options('tree');
160 '... set the options okay';
163 $stuff->add_options_with_speed( 'compatible', 'safe' );
165 '... add options with speed okay';
168 $stuff->options, [qw/tree funrolls funbuns compatible safe/],
169 'check options after add_options_with_speed'
173 $stuff->prepend_prerequisites_along_with();
175 '... add prerequisite options okay';
177 $stuff->clear_options;
178 $stuff->add_options( 1, 2 );
181 $stuff->splice_options( 1, 0, 'foo' );
183 '... splice_options works';
186 $stuff->options, [ 1, 'foo', 2 ],
187 'splice added expected option'
190 is( $stuff->option_accessor( 1 => 'foo++' ), 'foo++' );
191 is( $stuff->option_accessor(1), 'foo++' );
196 # $stuff->insert_options(undef);
197 #} '... could not add an undef where a string is expected';
200 # $stuff->set_option(5, {});
201 #} '... could not add a hash ref where a string is expected';
204 Stuff->new( options => [ undef, 10, undef, 20 ] );
206 '... bad constructor params';
209 my $stuff = Stuff->new();
210 $stuff->add_options(undef);
212 '... rejects push of an invalid type';
215 my $stuff = Stuff->new();
216 $stuff->insert_options(undef);
218 '... rejects unshift of an invalid type';
221 my $stuff = Stuff->new();
222 $stuff->set_option_at( 0, undef );
224 '... rejects set of an invalid type';
227 my $stuff = Stuff->new();
228 $stuff->sort_in_place_options(undef);
230 '... sort rejects arg of invalid type';
233 my $stuff = Stuff->new();
234 $stuff->option_accessor();
236 '... accessor rejects 0 args';
239 my $stuff = Stuff->new();
240 $stuff->option_accessor( 1, 2, 3 );
242 '... accessor rejects 3 args';
246 my $options = $stuff->meta->get_attribute('options');
247 does_ok( $options, 'Moose::AttributeHelpers::Trait::Collection::Array' );
252 'add_options' => 'push',
253 'remove_last_option' => 'pop',
254 'remove_first_option' => 'shift',
255 'insert_options' => 'unshift',
256 'get_option_at' => 'get',
257 'set_option_at' => 'set',
258 'num_options' => 'count',
259 'has_options' => 'empty',
260 'clear_options' => 'clear',
261 'splice_options' => 'splice',
262 'sort_options_in_place' => 'sort_in_place',
263 'option_accessor' => 'accessor',
264 'add_options_with_speed' =>
265 [ 'push' => [ 'funrolls', 'funbuns' ] ],
266 'prepend_prerequisites_along_with' =>
267 [ 'unshift' => [ 'first', 'second' ] ],
268 'descending_options' =>
269 [ 'sort_in_place' => [$sort] ],
271 '... got the right handles mapping'
274 is( $options->type_constraint->type_parameter, 'Str',
275 '... got the right container type' );