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