6 use Test::More tests => 62;
10 use_ok('MooseX::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 'sort_in_place' => 'sort_options_in_place',
36 add_options_with_speed => ['funrolls', 'funbuns']
39 prepend_prerequisites_along_with => ['first', 'second']
41 'sort_in_place' => { descending_options => [ sub { $_[1] <=> $_[0] } ],
47 my $stuff = Stuff->new(options => [ 10, 12 ]);
48 isa_ok($stuff, 'Stuff');
50 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);
78 } '... set the option okay';
80 is_deeply($stuff->options, [1, 2, 3], '... got options now');
82 ok($stuff->has_options, '... no 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);
91 } '... set the option okay';
93 is($stuff->get_option_at(1), 100, '... get option at index 1');
96 $stuff->add_options(10, 15);
97 } '... set the option okay';
99 is_deeply($stuff->options, [1, 100, 3, 10, 15], '... got more options now');
101 is($stuff->num_options, 5, '... got 5 options');
103 is($stuff->remove_last_option, 15, '... removed the last option');
105 is($stuff->num_options, 4, '... got 4 options');
106 is_deeply($stuff->options, [1, 100, 3, 10], '... got diff options now');
109 $stuff->insert_options(10, 20);
110 } '... set the option okay';
112 is($stuff->num_options, 6, '... got 6 options');
113 is_deeply($stuff->options, [10, 20, 1, 100, 3, 10], '... got diff options now');
115 is($stuff->get_option_at(0), 10, '... get option at index 0');
116 is($stuff->get_option_at(1), 20, '... get option at index 1');
117 is($stuff->get_option_at(3), 100, '... get option at index 3');
119 is($stuff->remove_first_option, 10, '... getting the first option');
121 is($stuff->num_options, 5, '... got 5 options');
122 is($stuff->get_option_at(0), 20, '... get option at index 0');
124 $stuff->clear_options;
125 is_deeply( $stuff->options, [], "... clear options" );
127 $stuff->add_options(5, 1, 2, 3);
128 $stuff->sort_options_in_place;
129 is_deeply( $stuff->options, [1, 2, 3, 5], "... sort options in place (default sort order)" );
131 $stuff->sort_options_in_place( sub { $_[1] <=> $_[0] } );
132 is_deeply( $stuff->options, [5, 3, 2, 1], "... sort options in place (descending order)" );
134 $stuff->clear_options();
135 $stuff->add_options(5, 1, 2, 3);
137 $stuff->descending_options();
138 } '... curried sort in place lives ok';
140 is_deeply( $stuff->options, [5, 3, 2, 1], "... sort currying" );
142 throws_ok { $stuff->sort_options_in_place('foo') } qr/Argument must be a code reference/,
143 'error when sort_in_place receives a non-coderef argument';
145 $stuff->clear_options;
148 $stuff->add_options('tree');
149 } '... set the options okay';
152 $stuff->add_options_with_speed('compatible', 'safe');
153 } '... add options with speed okay';
155 is_deeply($stuff->options, [qw/tree funrolls funbuns compatible safe/]);
158 $stuff->prepend_prerequisites_along_with();
159 } '... add prerequisite options okay';
164 # $stuff->insert_options(undef);
165 #} '... could not add an undef where a string is expected';
168 # $stuff->set_option(5, {});
169 #} '... could not add a hash ref where a string is expected';
172 Stuff->new(options => [ undef, 10, undef, 20 ]);
173 } '... bad constructor params';
176 my $stuff = Stuff->new();
177 $stuff->add_options(undef);
178 } '... rejects push of an invalid type';
181 my $stuff = Stuff->new();
182 $stuff->insert_options(undef);
183 } '... rejects unshift of an invalid type';
186 my $stuff = Stuff->new();
187 $stuff->set_option_at( 0, undef );
188 } '... rejects set of an invalid type';
191 my $stuff = Stuff->new();
192 $stuff->sort_in_place_options( undef );
193 } '... sort rejects arg of invalid type';
197 my $options = $stuff->meta->get_attribute('options');
198 isa_ok($options, 'MooseX::AttributeHelpers::Collection::Array');
200 is_deeply($options->provides, {
201 'push' => 'add_options',
202 'pop' => 'remove_last_option',
203 'shift' => 'remove_first_option',
204 'unshift' => 'insert_options',
205 'get' => 'get_option_at',
206 'set' => 'set_option_at',
207 'count' => 'num_options',
208 'empty' => 'has_options',
209 'clear' => 'clear_options',
210 'sort_in_place' => 'sort_options_in_place',
211 }, '... got the right provides mapping');
213 is($options->type_constraint->type_parameter, 'Str', '... got the right container type');