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