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