Commit | Line | Data |
e3c07b19 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
6 | use Test::More tests => 69; |
7 | use Test::Exception; |
8 | |
9 | BEGIN { |
10 | use_ok('Moose::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 { [] }, |
5f70ee9c |
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', |
30 | 'has_options' => 'empty', |
31 | 'clear_options' => 'clear', |
32 | 'splice_options' => 'splice', |
33 | 'sort_options_in_place' => 'sort_in_place', |
34 | 'option_accessor' => 'accessor', |
35 | 'add_optons_with_speed' => |
36 | [ 'push' => ['funrolls', 'funbuns'] ], |
37 | 'prepend_prerequisites_along_with' => |
38 | [ 'unshift' => ['first', 'second'] ], |
39 | 'descending_options' => |
40 | [ 'sort_in_place' => [ sub { $_[1] <=> $_[0] } ] ], |
e3c07b19 |
41 | }, |
42 | } |
43 | ); |
44 | } |
45 | |
46 | my $stuff = Stuff->new(options => [ 10, 12 ]); |
47 | isa_ok($stuff, 'Stuff'); |
48 | |
49 | can_ok($stuff, $_) for qw[ |
50 | add_options |
51 | remove_last_option |
52 | remove_first_option |
53 | insert_options |
54 | get_option_at |
55 | set_option_at |
56 | num_options |
57 | clear_options |
58 | has_options |
59 | sort_options_in_place |
60 | option_accessor |
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 | 'check options after add_options_with_speed'); |
157 | |
158 | lives_ok { |
159 | $stuff->prepend_prerequisites_along_with(); |
160 | } '... add prerequisite options okay'; |
161 | |
162 | $stuff->clear_options; |
163 | $stuff->add_options( 1, 2 ); |
164 | |
165 | lives_ok { |
166 | $stuff->splice_options( 1, 0, 'foo' ); |
167 | } '... splice_options works'; |
168 | |
169 | is_deeply( |
170 | $stuff->options, [ 1, 'foo', 2 ], |
171 | 'splice added expected option' |
172 | ); |
173 | |
174 | is($stuff->option_accessor(1 => 'foo++'), 'foo++'); |
175 | is($stuff->option_accessor(1), 'foo++'); |
176 | |
177 | ## check some errors |
178 | |
179 | #dies_ok { |
180 | # $stuff->insert_options(undef); |
181 | #} '... could not add an undef where a string is expected'; |
182 | # |
183 | #dies_ok { |
184 | # $stuff->set_option(5, {}); |
185 | #} '... could not add a hash ref where a string is expected'; |
186 | |
187 | dies_ok { |
188 | Stuff->new(options => [ undef, 10, undef, 20 ]); |
189 | } '... bad constructor params'; |
190 | |
191 | dies_ok { |
192 | my $stuff = Stuff->new(); |
193 | $stuff->add_options(undef); |
194 | } '... rejects push of an invalid type'; |
195 | |
196 | dies_ok { |
197 | my $stuff = Stuff->new(); |
198 | $stuff->insert_options(undef); |
199 | } '... rejects unshift of an invalid type'; |
200 | |
201 | dies_ok { |
202 | my $stuff = Stuff->new(); |
203 | $stuff->set_option_at( 0, undef ); |
204 | } '... rejects set of an invalid type'; |
205 | |
206 | dies_ok { |
207 | my $stuff = Stuff->new(); |
208 | $stuff->sort_in_place_options( undef ); |
209 | } '... sort rejects arg of invalid type'; |
210 | |
211 | dies_ok { |
212 | my $stuff = Stuff->new(); |
213 | $stuff->option_accessor(); |
214 | } '... accessor rejects 0 args'; |
215 | |
216 | dies_ok { |
217 | my $stuff = Stuff->new(); |
218 | $stuff->option_accessor(1, 2, 3); |
219 | } '... accessor rejects 3 args'; |
220 | |
221 | ## test the meta |
222 | |
223 | my $options = $stuff->meta->get_attribute('options'); |
224 | isa_ok($options, 'Moose::AttributeHelpers::Collection::Array'); |
225 | |
5f70ee9c |
226 | is_deeply($options->handles, |
227 | 'add_options' => 'push', |
228 | 'remove_last_option' => 'pop', |
229 | 'remove_first_option' => 'shift', |
230 | 'insert_options' => 'unshift', |
231 | 'get_option_at' => 'get', |
232 | 'set_option_at' => 'set', |
233 | 'num_options' => 'count', |
234 | 'has_options' => 'empty', |
235 | 'clear_options' => 'clear', |
236 | 'splice_options' => 'splice', |
237 | 'sort_options_in_place' => 'sort_in_place', |
238 | 'option_accessor' => 'accessor', |
239 | }, '... got the right handles mapping'); |
e3c07b19 |
240 | |
241 | is($options->type_constraint->type_parameter, 'Str', '... got the right container type'); |