Commit | Line | Data |
22d869ff |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
57e529ff |
6 | use Test::More tests => 69; |
69dde336 |
7 | use Test::Exception; |
22d869ff |
8 | |
9 | BEGIN { |
10 | use_ok('MooseX::AttributeHelpers'); |
11 | } |
12 | |
13 | { |
14 | package Stuff; |
15 | use Moose; |
16 | |
17 | has 'options' => ( |
d26633fc |
18 | metaclass => 'Collection::Array', |
22d869ff |
19 | is => 'ro', |
c43a2317 |
20 | isa => 'ArrayRef[Str]', |
22d869ff |
21 | default => sub { [] }, |
71703b28 |
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', |
af1ade48 |
32 | 'splice' => 'splice_options', |
71703b28 |
33 | 'sort_in_place' => 'sort_options_in_place', |
57e529ff |
34 | 'accessor' => 'option_accessor', |
71703b28 |
35 | }, |
c43a2317 |
36 | curries => { |
3656a0d7 |
37 | 'push' => { |
38 | add_options_with_speed => ['funrolls', 'funbuns'] |
39 | }, |
40 | 'unshift' => { |
41 | prepend_prerequisites_along_with => ['first', 'second'] |
80894c0a |
42 | }, |
71703b28 |
43 | 'sort_in_place' => { descending_options => [ sub { $_[1] <=> $_[0] } ], |
80894c0a |
44 | }, |
22d869ff |
45 | } |
46 | ); |
47 | } |
48 | |
77d02b8b |
49 | my $stuff = Stuff->new(options => [ 10, 12 ]); |
22d869ff |
50 | isa_ok($stuff, 'Stuff'); |
51 | |
8c651099 |
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 |
8cf40f80 |
60 | clear_options |
8c651099 |
61 | has_options |
71703b28 |
62 | sort_options_in_place |
57e529ff |
63 | option_accessor |
8c651099 |
64 | ]; |
65 | |
77d02b8b |
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'); |
22d869ff |
75 | |
8c651099 |
76 | ok(!$stuff->has_options, '... no options'); |
77 | is($stuff->num_options, 0, '... got no options'); |
78 | |
69dde336 |
79 | lives_ok { |
80 | $stuff->add_options(1, 2, 3); |
81 | } '... set the option okay'; |
82 | |
22d869ff |
83 | is_deeply($stuff->options, [1, 2, 3], '... got options now'); |
84 | |
8c651099 |
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 | |
69dde336 |
92 | lives_ok { |
93 | $stuff->set_option_at(1, 100); |
94 | } '... set the option okay'; |
8c651099 |
95 | |
96 | is($stuff->get_option_at(1), 100, '... get option at index 1'); |
97 | |
69dde336 |
98 | lives_ok { |
99 | $stuff->add_options(10, 15); |
100 | } '... set the option okay'; |
101 | |
8c651099 |
102 | is_deeply($stuff->options, [1, 100, 3, 10, 15], '... got more options now'); |
103 | |
104 | is($stuff->num_options, 5, '... got 5 options'); |
22d869ff |
105 | |
106 | is($stuff->remove_last_option, 15, '... removed the last option'); |
107 | |
8c651099 |
108 | is($stuff->num_options, 4, '... got 4 options'); |
109 | is_deeply($stuff->options, [1, 100, 3, 10], '... got diff options now'); |
110 | |
69dde336 |
111 | lives_ok { |
112 | $stuff->insert_options(10, 20); |
113 | } '... set the option okay'; |
8c651099 |
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 | |
8cf40f80 |
127 | $stuff->clear_options; |
128 | is_deeply( $stuff->options, [], "... clear options" ); |
129 | |
71703b28 |
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)" ); |
80894c0a |
133 | |
71703b28 |
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" ); |
80894c0a |
144 | |
71703b28 |
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'; |
80894c0a |
147 | |
148 | $stuff->clear_options; |
149 | |
c43a2317 |
150 | lives_ok { |
151 | $stuff->add_options('tree'); |
152 | } '... set the options okay'; |
69dde336 |
153 | |
c43a2317 |
154 | lives_ok { |
155 | $stuff->add_options_with_speed('compatible', 'safe'); |
156 | } '... add options with speed okay'; |
69dde336 |
157 | |
af1ade48 |
158 | is_deeply($stuff->options, [qw/tree funrolls funbuns compatible safe/], |
159 | 'check options after add_options_with_speed'); |
69dde336 |
160 | |
c43a2317 |
161 | lives_ok { |
162 | $stuff->prepend_prerequisites_along_with(); |
163 | } '... add prerequisite options okay'; |
164 | |
af1ade48 |
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 | |
57e529ff |
177 | is($stuff->option_accessor(1 => 'foo++'), 'foo++'); |
178 | is($stuff->option_accessor(1), 'foo++'); |
af1ade48 |
179 | |
c43a2317 |
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'; |
69dde336 |
189 | |
77d02b8b |
190 | dies_ok { |
c43a2317 |
191 | Stuff->new(options => [ undef, 10, undef, 20 ]); |
77d02b8b |
192 | } '... bad constructor params'; |
193 | |
5b380a52 |
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 | |
80894c0a |
209 | dies_ok { |
210 | my $stuff = Stuff->new(); |
211 | $stuff->sort_in_place_options( undef ); |
212 | } '... sort rejects arg of invalid type'; |
213 | |
57e529ff |
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 | |
8c651099 |
224 | ## test the meta |
225 | |
226 | my $options = $stuff->meta->get_attribute('options'); |
227 | isa_ok($options, 'MooseX::AttributeHelpers::Collection::Array'); |
22d869ff |
228 | |
8c651099 |
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', |
8cf40f80 |
238 | 'clear' => 'clear_options', |
af1ade48 |
239 | 'splice' => 'splice_options', |
71703b28 |
240 | 'sort_in_place' => 'sort_options_in_place', |
57e529ff |
241 | 'accessor' => 'option_accessor', |
71703b28 |
242 | }, '... got the right provides mapping'); |
22d869ff |
243 | |
c43a2317 |
244 | is($options->type_constraint->type_parameter, 'Str', '... got the right container type'); |