Make coderef for sort_in_place optional as well.
[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 => 62;
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             'sort_in_place' => 'sort_options_in_place',
33             },
34         curries   => {
35             'push'    => {
36                 add_options_with_speed => ['funrolls', 'funbuns']
37             },
38             'unshift'  => {
39                 prepend_prerequisites_along_with => ['first', 'second']
40             },
41             'sort_in_place' => { descending_options => [ sub { $_[1] <=> $_[0] } ],
42             },
43         }
44     );
45 }
46
47 my $stuff = Stuff->new(options => [ 10, 12 ]);
48 isa_ok($stuff, 'Stuff');
49
50 can_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
60     sort_options_in_place
61 ];
62
63 is_deeply($stuff->options, [10, 12], '... got options');
64
65 ok($stuff->has_options, '... we have options');
66 is($stuff->num_options, 2, '... got 2 options');
67
68 is($stuff->remove_last_option, 12, '... removed the last option');
69 is($stuff->remove_first_option, 10, '... removed the last option');
70
71 is_deeply($stuff->options, [], '... no options anymore');
72
73 ok(!$stuff->has_options, '... no options');
74 is($stuff->num_options, 0, '... got no options');
75
76 lives_ok {
77     $stuff->add_options(1, 2, 3);
78 } '... set the option okay';
79
80 is_deeply($stuff->options, [1, 2, 3], '... got options now');
81
82 ok($stuff->has_options, '... no options');
83 is($stuff->num_options, 3, '... got 3 options');
84
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');
88
89 lives_ok {
90     $stuff->set_option_at(1, 100);
91 } '... set the option okay';
92
93 is($stuff->get_option_at(1), 100, '... get option at index 1');
94
95 lives_ok {
96     $stuff->add_options(10, 15);
97 } '... set the option okay';
98
99 is_deeply($stuff->options, [1, 100, 3, 10, 15], '... got more options now');
100
101 is($stuff->num_options, 5, '... got 5 options');
102
103 is($stuff->remove_last_option, 15, '... removed the last option');
104
105 is($stuff->num_options, 4, '... got 4 options');
106 is_deeply($stuff->options, [1, 100, 3, 10], '... got diff options now');
107
108 lives_ok {
109     $stuff->insert_options(10, 20);
110 } '... set the option okay';
111
112 is($stuff->num_options, 6, '... got 6 options');
113 is_deeply($stuff->options, [10, 20, 1, 100, 3, 10], '... got diff options now');
114
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');
118
119 is($stuff->remove_first_option, 10, '... getting the first option');
120
121 is($stuff->num_options, 5, '... got 5 options');
122 is($stuff->get_option_at(0), 20, '... get option at index 0');
123
124 $stuff->clear_options;
125 is_deeply( $stuff->options, [], "... clear options" );
126
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)" );
130
131 $stuff->sort_options_in_place( sub { $_[1] <=> $_[0] } );
132 is_deeply( $stuff->options, [5, 3, 2, 1], "... sort options in place (descending order)" );
133
134 $stuff->clear_options();
135 $stuff->add_options(5, 1, 2, 3);
136 lives_ok {
137    $stuff->descending_options();
138 } '... curried sort in place lives ok';
139
140 is_deeply( $stuff->options, [5, 3, 2, 1], "... sort currying" );
141
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';
144
145 $stuff->clear_options;
146
147 lives_ok {
148     $stuff->add_options('tree');
149 } '... set the options okay';
150
151 lives_ok { 
152     $stuff->add_options_with_speed('compatible', 'safe');
153 } '... add options with speed okay';
154
155 is_deeply($stuff->options, [qw/tree funrolls funbuns compatible safe/]);
156
157 lives_ok {
158     $stuff->prepend_prerequisites_along_with();
159 } '... add prerequisite options okay';
160
161 ## check some errors
162
163 #dies_ok {
164 #    $stuff->insert_options(undef);
165 #} '... could not add an undef where a string is expected';
166 #
167 #dies_ok {
168 #    $stuff->set_option(5, {});
169 #} '... could not add a hash ref where a string is expected';
170
171 dies_ok {
172     Stuff->new(options => [ undef, 10, undef, 20 ]);
173 } '... bad constructor params';
174
175 dies_ok {
176     my $stuff = Stuff->new();
177     $stuff->add_options(undef);
178 } '... rejects push of an invalid type';
179
180 dies_ok {
181     my $stuff = Stuff->new();
182     $stuff->insert_options(undef);
183 } '... rejects unshift of an invalid type';
184
185 dies_ok {
186     my $stuff = Stuff->new();
187     $stuff->set_option_at( 0, undef );
188 } '... rejects set of an invalid type';
189
190 dies_ok {
191     my $stuff = Stuff->new();
192     $stuff->sort_in_place_options( undef );
193 } '... sort rejects arg of invalid type';
194
195 ## test the meta
196
197 my $options = $stuff->meta->get_attribute('options');
198 isa_ok($options, 'MooseX::AttributeHelpers::Collection::Array');
199
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');
212
213 is($options->type_constraint->type_parameter, 'Str', '... got the right container type');