6 use Test::More tests => 69;
8 use Test::Moose 'does_ok';
11 use_ok('MooseX::AttributeHelpers');
19 traits => [qw/MooseX::AttributeHelpers::Trait::Collection::Array/],
21 isa => 'ArrayRef[Str]',
22 default => sub { [] },
24 'push' => 'add_options',
25 'pop' => 'remove_last_option',
26 'shift' => 'remove_first_option',
27 'unshift' => 'insert_options',
28 'get' => 'get_option_at',
29 'set' => 'set_option_at',
30 'count' => 'num_options',
31 'empty' => 'has_options',
32 'clear' => 'clear_options',
33 'splice' => 'splice_options',
34 'sort_in_place' => 'sort_options_in_place',
35 'accessor' => 'option_accessor',
39 add_options_with_speed => ['funrolls', 'funbuns']
42 prepend_prerequisites_along_with => ['first', 'second']
44 'sort_in_place' => { descending_options => [ sub { $_[1] <=> $_[0] } ],
50 my $stuff = Stuff->new(options => [ 10, 12 ]);
51 isa_ok($stuff, 'Stuff');
53 can_ok($stuff, $_) for qw[
67 is_deeply($stuff->options, [10, 12], '... got options');
69 ok($stuff->has_options, '... we have options');
70 is($stuff->num_options, 2, '... got 2 options');
72 is($stuff->remove_last_option, 12, '... removed the last option');
73 is($stuff->remove_first_option, 10, '... removed the last option');
75 is_deeply($stuff->options, [], '... no options anymore');
77 ok(!$stuff->has_options, '... no options');
78 is($stuff->num_options, 0, '... got no options');
81 $stuff->add_options(1, 2, 3);
82 } '... set the option okay';
84 is_deeply($stuff->options, [1, 2, 3], '... got options now');
86 ok($stuff->has_options, '... no options');
87 is($stuff->num_options, 3, '... got 3 options');
89 is($stuff->get_option_at(0), 1, '... get option at index 0');
90 is($stuff->get_option_at(1), 2, '... get option at index 1');
91 is($stuff->get_option_at(2), 3, '... get option at index 2');
94 $stuff->set_option_at(1, 100);
95 } '... set the option okay';
97 is($stuff->get_option_at(1), 100, '... get option at index 1');
100 $stuff->add_options(10, 15);
101 } '... set the option okay';
103 is_deeply($stuff->options, [1, 100, 3, 10, 15], '... 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);
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], '... got diff options now');
119 is($stuff->get_option_at(0), 10, '... get option at index 0');
120 is($stuff->get_option_at(1), 20, '... get option at index 1');
121 is($stuff->get_option_at(3), 100, '... get option at index 3');
123 is($stuff->remove_first_option, 10, '... getting the first option');
125 is($stuff->num_options, 5, '... got 5 options');
126 is($stuff->get_option_at(0), 20, '... get option at index 0');
128 $stuff->clear_options;
129 is_deeply( $stuff->options, [], "... clear options" );
131 $stuff->add_options(5, 1, 2, 3);
132 $stuff->sort_options_in_place;
133 is_deeply( $stuff->options, [1, 2, 3, 5], "... sort options in place (default sort order)" );
135 $stuff->sort_options_in_place( sub { $_[1] <=> $_[0] } );
136 is_deeply( $stuff->options, [5, 3, 2, 1], "... sort options in place (descending order)" );
138 $stuff->clear_options();
139 $stuff->add_options(5, 1, 2, 3);
141 $stuff->descending_options();
142 } '... curried sort in place lives ok';
144 is_deeply( $stuff->options, [5, 3, 2, 1], "... sort currying" );
146 throws_ok { $stuff->sort_options_in_place('foo') } qr/Argument must be a code reference/,
147 'error when sort_in_place receives a non-coderef argument';
149 $stuff->clear_options;
152 $stuff->add_options('tree');
153 } '... set the options okay';
156 $stuff->add_options_with_speed('compatible', 'safe');
157 } '... add options with speed okay';
159 is_deeply($stuff->options, [qw/tree funrolls funbuns compatible safe/],
160 'check options after add_options_with_speed');
163 $stuff->prepend_prerequisites_along_with();
164 } '... add prerequisite options okay';
166 $stuff->clear_options;
167 $stuff->add_options( 1, 2 );
170 $stuff->splice_options( 1, 0, 'foo' );
171 } '... splice_options works';
174 $stuff->options, [ 1, 'foo', 2 ],
175 'splice added expected option'
178 is($stuff->option_accessor(1 => 'foo++'), 'foo++');
179 is($stuff->option_accessor(1), 'foo++');
184 # $stuff->insert_options(undef);
185 #} '... could not add an undef where a string is expected';
188 # $stuff->set_option(5, {});
189 #} '... could not add a hash ref where a string is expected';
192 Stuff->new(options => [ undef, 10, undef, 20 ]);
193 } '... bad constructor params';
196 my $stuff = Stuff->new();
197 $stuff->add_options(undef);
198 } '... rejects push of an invalid type';
201 my $stuff = Stuff->new();
202 $stuff->insert_options(undef);
203 } '... rejects unshift of an invalid type';
206 my $stuff = Stuff->new();
207 $stuff->set_option_at( 0, undef );
208 } '... rejects set of an invalid type';
211 my $stuff = Stuff->new();
212 $stuff->sort_in_place_options( undef );
213 } '... sort rejects arg of invalid type';
216 my $stuff = Stuff->new();
217 $stuff->option_accessor();
218 } '... accessor rejects 0 args';
221 my $stuff = Stuff->new();
222 $stuff->option_accessor(1, 2, 3);
223 } '... accessor rejects 3 args';
227 my $options = $stuff->meta->get_attribute('options');
228 does_ok($options, 'MooseX::AttributeHelpers::Trait::Collection::Array');
230 is_deeply($options->provides, {
231 'push' => 'add_options',
232 'pop' => 'remove_last_option',
233 'shift' => 'remove_first_option',
234 'unshift' => 'insert_options',
235 'get' => 'get_option_at',
236 'set' => 'set_option_at',
237 'count' => 'num_options',
238 'empty' => 'has_options',
239 'clear' => 'clear_options',
240 'splice' => 'splice_options',
241 'sort_in_place' => 'sort_options_in_place',
242 'accessor' => 'option_accessor',
243 }, '... got the right provides mapping');
245 is($options->type_constraint->type_parameter, 'Str', '... got the right container type');