Implemented List::sort and Array::sort_in_place. Added basic tests and pod.
[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 => 60;
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_in_place_options',
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' => { ascending_options => [ sub { $_[0] <=> $_[1] } ],
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_in_place_options
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(1..3);
128 $stuff->sort_in_place_options( sub { $_[1] <=> $_[0] } );
129 is_deeply( $stuff->options, [3, 2, 1], "... sort options in place" );
130
131 lives_ok { 
132    $stuff->ascending_options();
133 } '... add descending options okay';
134
135 is_deeply( $stuff->options, [1, 2, 3], "... sort currying" );
136
137 $stuff->clear_options;
138
139 lives_ok {
140     $stuff->add_options('tree');
141 } '... set the options okay';
142
143 lives_ok { 
144     $stuff->add_options_with_speed('compatible', 'safe');
145 } '... add options with speed okay';
146
147 is_deeply($stuff->options, [qw/tree funrolls funbuns compatible safe/]);
148
149 lives_ok {
150     $stuff->prepend_prerequisites_along_with();
151 } '... add prerequisite options okay';
152
153 ## check some errors
154
155 #dies_ok {
156 #    $stuff->insert_options(undef);
157 #} '... could not add an undef where a string is expected';
158 #
159 #dies_ok {
160 #    $stuff->set_option(5, {});
161 #} '... could not add a hash ref where a string is expected';
162
163 dies_ok {
164     Stuff->new(options => [ undef, 10, undef, 20 ]);
165 } '... bad constructor params';
166
167 dies_ok {
168     my $stuff = Stuff->new();
169     $stuff->add_options(undef);
170 } '... rejects push of an invalid type';
171
172 dies_ok {
173     my $stuff = Stuff->new();
174     $stuff->insert_options(undef);
175 } '... rejects unshift of an invalid type';
176
177 dies_ok {
178     my $stuff = Stuff->new();
179     $stuff->set_option_at( 0, undef );
180 } '... rejects set of an invalid type';
181
182 dies_ok {
183     my $stuff = Stuff->new();
184     $stuff->sort_in_place_options( undef );
185 } '... sort rejects arg of invalid type';
186
187 ## test the meta
188
189 my $options = $stuff->meta->get_attribute('options');
190 isa_ok($options, 'MooseX::AttributeHelpers::Collection::Array');
191
192 is_deeply($options->provides, {
193     'push'    => 'add_options',
194     'pop'     => 'remove_last_option',    
195     'shift'   => 'remove_first_option',
196     'unshift' => 'insert_options',
197     'get'     => 'get_option_at',
198     'set'     => 'set_option_at',
199     'count'   => 'num_options',
200     'empty'   => 'has_options',    
201     'clear'   => 'clear_options',    
202     'sort_in_place' => 'sort_in_place_options',
203 }, '... got the right provies mapping');
204
205 is($options->type_constraint->type_parameter, 'Str', '... got the right container type');