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