do all the renaming that was discussed
[gitmo/Moose.git] / t / 070_attribute_helpers / 202_trait_array.t
CommitLineData
e3c07b19 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
a5209c26 6use Test::More tests => 68;
e3c07b19 7use Test::Exception;
8use Test::Moose 'does_ok';
9
18281451 10my $sort;
d50fc84a 11
e3c07b19 12{
d50fc84a 13
e3c07b19 14 package Stuff;
15 use Moose;
a5209c26 16 use Moose::AttributeHelpers;
e3c07b19 17
18 has 'options' => (
a40b446a 19 traits => ['Array'],
d50fc84a 20 is => 'ro',
21 isa => 'ArrayRef[Str]',
22 default => sub { [] },
0d103ac9 23 handles => {
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',
18281451 36 'add_options_with_speed' =>
d50fc84a 37 [ 'push' => [ 'funrolls', 'funbuns' ] ],
0d103ac9 38 'prepend_prerequisites_along_with' =>
d50fc84a 39 [ 'unshift' => [ 'first', 'second' ] ],
0d103ac9 40 'descending_options' =>
d50fc84a 41 [ 'sort_in_place' => [ $sort = sub { $_[1] <=> $_[0] } ] ],
e3c07b19 42 }
43 );
44}
45
d50fc84a 46my $stuff = Stuff->new( options => [ 10, 12 ] );
47isa_ok( $stuff, 'Stuff' );
e3c07b19 48
d50fc84a 49can_ok( $stuff, $_ ) for qw[
e3c07b19 50 add_options
51 remove_last_option
52 remove_first_option
53 insert_options
54 get_option_at
55 set_option_at
56 num_options
57 clear_options
58 has_options
59de9de4 59 sort_options_in_place
60 option_accessor
e3c07b19 61];
62
d50fc84a 63is_deeply( $stuff->options, [ 10, 12 ], '... got options' );
e3c07b19 64
d50fc84a 65ok( $stuff->has_options, '... we have options' );
66is( $stuff->num_options, 2, '... got 2 options' );
e3c07b19 67
d50fc84a 68is( $stuff->remove_last_option, 12, '... removed the last option' );
69is( $stuff->remove_first_option, 10, '... removed the last option' );
e3c07b19 70
d50fc84a 71is_deeply( $stuff->options, [], '... no options anymore' );
e3c07b19 72
d50fc84a 73ok( !$stuff->has_options, '... no options' );
74is( $stuff->num_options, 0, '... got no options' );
e3c07b19 75
76lives_ok {
d50fc84a 77 $stuff->add_options( 1, 2, 3 );
78}
79'... set the option okay';
e3c07b19 80
d50fc84a 81is_deeply( $stuff->options, [ 1, 2, 3 ], '... got options now' );
e3c07b19 82
d50fc84a 83ok( $stuff->has_options, '... no options' );
84is( $stuff->num_options, 3, '... got 3 options' );
e3c07b19 85
d50fc84a 86is( $stuff->get_option_at(0), 1, '... get option at index 0' );
87is( $stuff->get_option_at(1), 2, '... get option at index 1' );
88is( $stuff->get_option_at(2), 3, '... get option at index 2' );
e3c07b19 89
90lives_ok {
d50fc84a 91 $stuff->set_option_at( 1, 100 );
92}
93'... set the option okay';
e3c07b19 94
d50fc84a 95is( $stuff->get_option_at(1), 100, '... get option at index 1' );
e3c07b19 96
97lives_ok {
d50fc84a 98 $stuff->add_options( 10, 15 );
99}
100'... set the option okay';
e3c07b19 101
d50fc84a 102is_deeply( $stuff->options, [ 1, 100, 3, 10, 15 ],
103 '... got more options now' );
e3c07b19 104
d50fc84a 105is( $stuff->num_options, 5, '... got 5 options' );
e3c07b19 106
d50fc84a 107is( $stuff->remove_last_option, 15, '... removed the last option' );
e3c07b19 108
d50fc84a 109is( $stuff->num_options, 4, '... got 4 options' );
110is_deeply( $stuff->options, [ 1, 100, 3, 10 ], '... got diff options now' );
e3c07b19 111
112lives_ok {
d50fc84a 113 $stuff->insert_options( 10, 20 );
114}
115'... set the option okay';
e3c07b19 116
d50fc84a 117is( $stuff->num_options, 6, '... got 6 options' );
118is_deeply( $stuff->options, [ 10, 20, 1, 100, 3, 10 ],
119 '... got diff options now' );
e3c07b19 120
d50fc84a 121is( $stuff->get_option_at(0), 10, '... get option at index 0' );
122is( $stuff->get_option_at(1), 20, '... get option at index 1' );
123is( $stuff->get_option_at(3), 100, '... get option at index 3' );
e3c07b19 124
d50fc84a 125is( $stuff->remove_first_option, 10, '... getting the first option' );
e3c07b19 126
d50fc84a 127is( $stuff->num_options, 5, '... got 5 options' );
128is( $stuff->get_option_at(0), 20, '... get option at index 0' );
e3c07b19 129
130$stuff->clear_options;
131is_deeply( $stuff->options, [], "... clear options" );
132
d50fc84a 133$stuff->add_options( 5, 1, 2, 3 );
59de9de4 134$stuff->sort_options_in_place;
d50fc84a 135is_deeply( $stuff->options, [ 1, 2, 3, 5 ],
136 "... sort options in place (default sort order)" );
59de9de4 137
138$stuff->sort_options_in_place( sub { $_[1] <=> $_[0] } );
d50fc84a 139is_deeply( $stuff->options, [ 5, 3, 2, 1 ],
140 "... sort options in place (descending order)" );
59de9de4 141
142$stuff->clear_options();
d50fc84a 143$stuff->add_options( 5, 1, 2, 3 );
59de9de4 144lives_ok {
d50fc84a 145 $stuff->descending_options();
146}
147'... curried sort in place lives ok';
59de9de4 148
d50fc84a 149is_deeply( $stuff->options, [ 5, 3, 2, 1 ], "... sort currying" );
59de9de4 150
d50fc84a 151throws_ok { $stuff->sort_options_in_place('foo') }
152qr/Argument must be a code reference/,
59de9de4 153 'error when sort_in_place receives a non-coderef argument';
154
155$stuff->clear_options;
156
157lives_ok {
158 $stuff->add_options('tree');
d50fc84a 159}
160'... set the options okay';
59de9de4 161
162lives_ok {
d50fc84a 163 $stuff->add_options_with_speed( 'compatible', 'safe' );
164}
165'... add options with speed okay';
59de9de4 166
d50fc84a 167is_deeply(
168 $stuff->options, [qw/tree funrolls funbuns compatible safe/],
169 'check options after add_options_with_speed'
170);
59de9de4 171
172lives_ok {
173 $stuff->prepend_prerequisites_along_with();
d50fc84a 174}
175'... add prerequisite options okay';
59de9de4 176
177$stuff->clear_options;
178$stuff->add_options( 1, 2 );
179
180lives_ok {
181 $stuff->splice_options( 1, 0, 'foo' );
d50fc84a 182}
183'... splice_options works';
59de9de4 184
185is_deeply(
186 $stuff->options, [ 1, 'foo', 2 ],
187 'splice added expected option'
188);
189
d50fc84a 190is( $stuff->option_accessor( 1 => 'foo++' ), 'foo++' );
191is( $stuff->option_accessor(1), 'foo++' );
59de9de4 192
e3c07b19 193## check some errors
194
59de9de4 195#dies_ok {
196# $stuff->insert_options(undef);
197#} '... could not add an undef where a string is expected';
198#
199#dies_ok {
200# $stuff->set_option(5, {});
201#} '... could not add a hash ref where a string is expected';
202
e3c07b19 203dies_ok {
d50fc84a 204 Stuff->new( options => [ undef, 10, undef, 20 ] );
205}
206'... bad constructor params';
e3c07b19 207
208dies_ok {
59de9de4 209 my $stuff = Stuff->new();
210 $stuff->add_options(undef);
d50fc84a 211}
212'... rejects push of an invalid type';
59de9de4 213
214dies_ok {
215 my $stuff = Stuff->new();
e3c07b19 216 $stuff->insert_options(undef);
d50fc84a 217}
218'... rejects unshift of an invalid type';
e3c07b19 219
220dies_ok {
59de9de4 221 my $stuff = Stuff->new();
222 $stuff->set_option_at( 0, undef );
d50fc84a 223}
224'... rejects set of an invalid type';
e3c07b19 225
226dies_ok {
59de9de4 227 my $stuff = Stuff->new();
d50fc84a 228 $stuff->sort_in_place_options(undef);
229}
230'... sort rejects arg of invalid type';
59de9de4 231
232dies_ok {
233 my $stuff = Stuff->new();
234 $stuff->option_accessor();
d50fc84a 235}
236'... accessor rejects 0 args';
59de9de4 237
238dies_ok {
239 my $stuff = Stuff->new();
d50fc84a 240 $stuff->option_accessor( 1, 2, 3 );
241}
242'... accessor rejects 3 args';
e3c07b19 243
244## test the meta
245
246my $options = $stuff->meta->get_attribute('options');
a40b446a 247does_ok( $options, 'Moose::Meta::Attribute::Trait::Native::Array' );
d50fc84a 248
249is_deeply(
250 $options->handles,
251 {
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] ],
270 },
271 '... got the right handles mapping'
272);
273
274is( $options->type_constraint->type_parameter, 'Str',
275 '... got the right container type' );