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