6 use Test::More tests => 69;
10 use_ok('Moose::AttributeHelpers');
18 metaclass => 'Collection::Array',
20 isa => 'ArrayRef[Str]',
21 default => sub { [] },
23 'push' => 'add_options',
24 'pop' => 'remove_last_option',
25 'shift' => 'remove_first_option',
26 'unshift' => 'insert_options',
27 'get' => 'get_option_at',
28 'set' => 'set_option_at',
29 'count' => 'num_options',
30 'empty' => 'has_options',
31 'clear' => 'clear_options',
32 'splice' => 'splice_options',
33 'sort_in_place' => 'sort_options_in_place',
34 'accessor' => 'option_accessor',
38 add_options_with_speed => ['funrolls', 'funbuns']
41 prepend_prerequisites_along_with => ['first', 'second']
43 'sort_in_place' => { descending_options => [ sub { $_[1] <=> $_[0] } ],
49 my $stuff = Stuff->new(options => [ 10, 12 ]);
50 isa_ok($stuff, 'Stuff');
52 can_ok($stuff, $_) for qw[
66 is_deeply($stuff->options, [10, 12], '... got options');
68 ok($stuff->has_options, '... we have options');
69 is($stuff->num_options, 2, '... got 2 options');
71 is($stuff->remove_last_option, 12, '... removed the last option');
72 is($stuff->remove_first_option, 10, '... removed the last option');
74 is_deeply($stuff->options, [], '... no options anymore');
76 ok(!$stuff->has_options, '... no options');
77 is($stuff->num_options, 0, '... got no options');
80 $stuff->add_options(1, 2, 3);
81 } '... set the option okay';
83 is_deeply($stuff->options, [1, 2, 3], '... got options now');
85 ok($stuff->has_options, '... no options');
86 is($stuff->num_options, 3, '... got 3 options');
88 is($stuff->get_option_at(0), 1, '... get option at index 0');
89 is($stuff->get_option_at(1), 2, '... get option at index 1');
90 is($stuff->get_option_at(2), 3, '... get option at index 2');
93 $stuff->set_option_at(1, 100);
94 } '... set the option okay';
96 is($stuff->get_option_at(1), 100, '... get option at index 1');
99 $stuff->add_options(10, 15);
100 } '... set the option okay';
102 is_deeply($stuff->options, [1, 100, 3, 10, 15], '... 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);
113 } '... set the option okay';
115 is($stuff->num_options, 6, '... got 6 options');
116 is_deeply($stuff->options, [10, 20, 1, 100, 3, 10], '... got diff options now');
118 is($stuff->get_option_at(0), 10, '... get option at index 0');
119 is($stuff->get_option_at(1), 20, '... get option at index 1');
120 is($stuff->get_option_at(3), 100, '... get option at index 3');
122 is($stuff->remove_first_option, 10, '... getting the first option');
124 is($stuff->num_options, 5, '... got 5 options');
125 is($stuff->get_option_at(0), 20, '... get option at index 0');
127 $stuff->clear_options;
128 is_deeply( $stuff->options, [], "... clear options" );
130 $stuff->add_options(5, 1, 2, 3);
131 $stuff->sort_options_in_place;
132 is_deeply( $stuff->options, [1, 2, 3, 5], "... sort options in place (default sort order)" );
134 $stuff->sort_options_in_place( sub { $_[1] <=> $_[0] } );
135 is_deeply( $stuff->options, [5, 3, 2, 1], "... sort options in place (descending order)" );
137 $stuff->clear_options();
138 $stuff->add_options(5, 1, 2, 3);
140 $stuff->descending_options();
141 } '... curried sort in place lives ok';
143 is_deeply( $stuff->options, [5, 3, 2, 1], "... sort currying" );
145 throws_ok { $stuff->sort_options_in_place('foo') } qr/Argument must be a code reference/,
146 'error when sort_in_place receives a non-coderef argument';
148 $stuff->clear_options;
151 $stuff->add_options('tree');
152 } '... set the options okay';
155 $stuff->add_options_with_speed('compatible', 'safe');
156 } '... add options with speed okay';
158 is_deeply($stuff->options, [qw/tree funrolls funbuns compatible safe/],
159 'check options after add_options_with_speed');
162 $stuff->prepend_prerequisites_along_with();
163 } '... add prerequisite options okay';
165 $stuff->clear_options;
166 $stuff->add_options( 1, 2 );
169 $stuff->splice_options( 1, 0, 'foo' );
170 } '... splice_options works';
173 $stuff->options, [ 1, 'foo', 2 ],
174 'splice added expected option'
177 is($stuff->option_accessor(1 => 'foo++'), 'foo++');
178 is($stuff->option_accessor(1), 'foo++');
183 # $stuff->insert_options(undef);
184 #} '... could not add an undef where a string is expected';
187 # $stuff->set_option(5, {});
188 #} '... could not add a hash ref where a string is expected';
191 Stuff->new(options => [ undef, 10, undef, 20 ]);
192 } '... bad constructor params';
195 my $stuff = Stuff->new();
196 $stuff->add_options(undef);
197 } '... rejects push of an invalid type';
200 my $stuff = Stuff->new();
201 $stuff->insert_options(undef);
202 } '... rejects unshift of an invalid type';
205 my $stuff = Stuff->new();
206 $stuff->set_option_at( 0, undef );
207 } '... rejects set of an invalid type';
210 my $stuff = Stuff->new();
211 $stuff->sort_in_place_options( undef );
212 } '... sort rejects arg of invalid type';
215 my $stuff = Stuff->new();
216 $stuff->option_accessor();
217 } '... accessor rejects 0 args';
220 my $stuff = Stuff->new();
221 $stuff->option_accessor(1, 2, 3);
222 } '... accessor rejects 3 args';
226 my $options = $stuff->meta->get_attribute('options');
227 isa_ok($options, 'Moose::AttributeHelpers::Collection::Array');
229 is_deeply($options->provides, {
230 'push' => 'add_options',
231 'pop' => 'remove_last_option',
232 'shift' => 'remove_first_option',
233 'unshift' => 'insert_options',
234 'get' => 'get_option_at',
235 'set' => 'set_option_at',
236 'count' => 'num_options',
237 'empty' => 'has_options',
238 'clear' => 'clear_options',
239 'splice' => 'splice_options',
240 'sort_in_place' => 'sort_options_in_place',
241 'accessor' => 'option_accessor',
242 }, '... got the right provides mapping');
244 is($options->type_constraint->type_parameter, 'Str', '... got the right container type');