6 use Test::More tests => 68;
8 use Test::Moose 'does_ok';
20 isa => 'ArrayRef[Str]',
21 default => sub { [] },
23 'add_options' => 'push',
24 'remove_last_option' => 'pop',
25 'remove_first_option' => 'shift',
26 'insert_options' => 'unshift',
27 'get_option_at' => 'get',
28 'set_option_at' => 'set',
29 'num_options' => 'count',
30 'has_no_options' => 'is_empty',
31 'clear_options' => 'clear',
32 'splice_options' => 'splice',
33 'sort_options_in_place' => 'sort_in_place',
34 'option_accessor' => 'accessor',
35 'add_options_with_speed' =>
36 [ 'push' => 'funrolls', 'funbuns' ],
37 'prepend_prerequisites_along_with' =>
38 [ 'unshift' => 'first', 'second' ],
39 'descending_options' =>
40 [ 'sort_in_place' => ($sort = sub { $_[1] <=> $_[0] }) ],
45 my $stuff = Stuff->new( options => [ 10, 12 ] );
46 isa_ok( $stuff, 'Stuff' );
48 can_ok( $stuff, $_ ) for qw[
62 is_deeply( $stuff->options, [ 10, 12 ], '... got options' );
64 ok( !$stuff->has_no_options, '... we have options' );
65 is( $stuff->num_options, 2, '... got 2 options' );
67 is( $stuff->remove_last_option, 12, '... removed the last option' );
68 is( $stuff->remove_first_option, 10, '... removed the last option' );
70 is_deeply( $stuff->options, [], '... no options anymore' );
72 ok( $stuff->has_no_options, '... no options' );
73 is( $stuff->num_options, 0, '... got no options' );
76 $stuff->add_options( 1, 2, 3 );
78 '... set the option okay';
80 is_deeply( $stuff->options, [ 1, 2, 3 ], '... got options now' );
82 ok( !$stuff->has_no_options, '... has options' );
83 is( $stuff->num_options, 3, '... got 3 options' );
85 is( $stuff->get_option_at(0), 1, '... get option at index 0' );
86 is( $stuff->get_option_at(1), 2, '... get option at index 1' );
87 is( $stuff->get_option_at(2), 3, '... get option at index 2' );
90 $stuff->set_option_at( 1, 100 );
92 '... set the option okay';
94 is( $stuff->get_option_at(1), 100, '... get option at index 1' );
97 $stuff->add_options( 10, 15 );
99 '... set the option okay';
101 is_deeply( $stuff->options, [ 1, 100, 3, 10, 15 ],
102 '... got more options now' );
104 is( $stuff->num_options, 5, '... got 5 options' );
106 is( $stuff->remove_last_option, 15, '... removed the last option' );
108 is( $stuff->num_options, 4, '... got 4 options' );
109 is_deeply( $stuff->options, [ 1, 100, 3, 10 ], '... got diff options now' );
112 $stuff->insert_options( 10, 20 );
114 '... set the option okay';
116 is( $stuff->num_options, 6, '... got 6 options' );
117 is_deeply( $stuff->options, [ 10, 20, 1, 100, 3, 10 ],
118 '... got diff options now' );
120 is( $stuff->get_option_at(0), 10, '... get option at index 0' );
121 is( $stuff->get_option_at(1), 20, '... get option at index 1' );
122 is( $stuff->get_option_at(3), 100, '... get option at index 3' );
124 is( $stuff->remove_first_option, 10, '... getting the first option' );
126 is( $stuff->num_options, 5, '... got 5 options' );
127 is( $stuff->get_option_at(0), 20, '... get option at index 0' );
129 $stuff->clear_options;
130 is_deeply( $stuff->options, [], "... clear options" );
132 $stuff->add_options( 5, 1, 2, 3 );
133 $stuff->sort_options_in_place;
134 is_deeply( $stuff->options, [ 1, 2, 3, 5 ],
135 "... sort options in place (default sort order)" );
137 $stuff->sort_options_in_place( sub { $_[1] <=> $_[0] } );
138 is_deeply( $stuff->options, [ 5, 3, 2, 1 ],
139 "... sort options in place (descending order)" );
141 $stuff->clear_options();
142 $stuff->add_options( 5, 1, 2, 3 );
144 $stuff->descending_options();
146 '... curried sort in place lives ok';
148 is_deeply( $stuff->options, [ 5, 3, 2, 1 ], "... sort currying" );
150 throws_ok { $stuff->sort_options_in_place('foo') }
151 qr/Argument must be a code reference/,
152 'error when sort_in_place receives a non-coderef argument';
154 $stuff->clear_options;
157 $stuff->add_options('tree');
159 '... set the options okay';
162 $stuff->add_options_with_speed( 'compatible', 'safe' );
164 '... add options with speed okay';
167 $stuff->options, [qw/tree funrolls funbuns compatible safe/],
168 'check options after add_options_with_speed'
172 $stuff->prepend_prerequisites_along_with();
174 '... add prerequisite options okay';
176 $stuff->clear_options;
177 $stuff->add_options( 1, 2 );
180 $stuff->splice_options( 1, 0, 'foo' );
182 '... splice_options works';
185 $stuff->options, [ 1, 'foo', 2 ],
186 'splice added expected option'
189 is( $stuff->option_accessor( 1 => 'foo++' ), 'foo++' );
190 is( $stuff->option_accessor(1), 'foo++' );
195 # $stuff->insert_options(undef);
196 #} '... could not add an undef where a string is expected';
199 # $stuff->set_option(5, {});
200 #} '... could not add a hash ref where a string is expected';
203 Stuff->new( options => [ undef, 10, undef, 20 ] );
205 '... bad constructor params';
208 my $stuff = Stuff->new();
209 $stuff->add_options(undef);
211 '... rejects push of an invalid type';
214 my $stuff = Stuff->new();
215 $stuff->insert_options(undef);
217 '... rejects unshift of an invalid type';
220 my $stuff = Stuff->new();
221 $stuff->set_option_at( 0, undef );
223 '... rejects set of an invalid type';
226 my $stuff = Stuff->new();
227 $stuff->sort_in_place_options(undef);
229 '... sort rejects arg of invalid type';
232 my $stuff = Stuff->new();
233 $stuff->option_accessor();
235 '... accessor rejects 0 args';
238 my $stuff = Stuff->new();
239 $stuff->option_accessor( 1, 2, 3 );
241 '... accessor rejects 3 args';
245 my $options = $stuff->meta->get_attribute('options');
246 does_ok( $options, 'Moose::Meta::Attribute::Native::Trait::Array' );
251 'add_options' => 'push',
252 'remove_last_option' => 'pop',
253 'remove_first_option' => 'shift',
254 'insert_options' => 'unshift',
255 'get_option_at' => 'get',
256 'set_option_at' => 'set',
257 'num_options' => 'count',
258 'has_no_options' => 'is_empty',
259 'clear_options' => 'clear',
260 'splice_options' => 'splice',
261 'sort_options_in_place' => 'sort_in_place',
262 'option_accessor' => 'accessor',
263 'add_options_with_speed' => [ 'push' => 'funrolls', 'funbuns' ],
264 'prepend_prerequisites_along_with' =>
265 [ 'unshift' => 'first', 'second' ],
266 'descending_options' => [ 'sort_in_place' => $sort ],
268 '... got the right handles mapping'
271 is( $options->type_constraint->type_parameter, 'Str',
272 '... got the right container type' );