Commit | Line | Data |
22d869ff |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
af1ade48 |
6 | use Test::More tests => 64; |
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', |
34 | }, |
c43a2317 |
35 | curries => { |
3656a0d7 |
36 | 'push' => { |
37 | add_options_with_speed => ['funrolls', 'funbuns'] |
38 | }, |
39 | 'unshift' => { |
40 | prepend_prerequisites_along_with => ['first', 'second'] |
80894c0a |
41 | }, |
71703b28 |
42 | 'sort_in_place' => { descending_options => [ sub { $_[1] <=> $_[0] } ], |
80894c0a |
43 | }, |
22d869ff |
44 | } |
45 | ); |
46 | } |
47 | |
77d02b8b |
48 | my $stuff = Stuff->new(options => [ 10, 12 ]); |
22d869ff |
49 | isa_ok($stuff, 'Stuff'); |
50 | |
8c651099 |
51 | can_ok($stuff, $_) for qw[ |
52 | add_options |
53 | remove_last_option |
54 | remove_first_option |
55 | insert_options |
56 | get_option_at |
57 | set_option_at |
58 | num_options |
8cf40f80 |
59 | clear_options |
8c651099 |
60 | has_options |
71703b28 |
61 | sort_options_in_place |
8c651099 |
62 | ]; |
63 | |
77d02b8b |
64 | is_deeply($stuff->options, [10, 12], '... got options'); |
65 | |
66 | ok($stuff->has_options, '... we have options'); |
67 | is($stuff->num_options, 2, '... got 2 options'); |
68 | |
69 | is($stuff->remove_last_option, 12, '... removed the last option'); |
70 | is($stuff->remove_first_option, 10, '... removed the last option'); |
71 | |
72 | is_deeply($stuff->options, [], '... no options anymore'); |
22d869ff |
73 | |
8c651099 |
74 | ok(!$stuff->has_options, '... no options'); |
75 | is($stuff->num_options, 0, '... got no options'); |
76 | |
69dde336 |
77 | lives_ok { |
78 | $stuff->add_options(1, 2, 3); |
79 | } '... set the option okay'; |
80 | |
22d869ff |
81 | is_deeply($stuff->options, [1, 2, 3], '... got options now'); |
82 | |
8c651099 |
83 | ok($stuff->has_options, '... no options'); |
84 | is($stuff->num_options, 3, '... got 3 options'); |
85 | |
86 | is($stuff->get_option_at(0), 1, '... get option at index 0'); |
87 | is($stuff->get_option_at(1), 2, '... get option at index 1'); |
88 | is($stuff->get_option_at(2), 3, '... get option at index 2'); |
89 | |
69dde336 |
90 | lives_ok { |
91 | $stuff->set_option_at(1, 100); |
92 | } '... set the option okay'; |
8c651099 |
93 | |
94 | is($stuff->get_option_at(1), 100, '... get option at index 1'); |
95 | |
69dde336 |
96 | lives_ok { |
97 | $stuff->add_options(10, 15); |
98 | } '... set the option okay'; |
99 | |
8c651099 |
100 | is_deeply($stuff->options, [1, 100, 3, 10, 15], '... got more options now'); |
101 | |
102 | is($stuff->num_options, 5, '... got 5 options'); |
22d869ff |
103 | |
104 | is($stuff->remove_last_option, 15, '... removed the last option'); |
105 | |
8c651099 |
106 | is($stuff->num_options, 4, '... got 4 options'); |
107 | is_deeply($stuff->options, [1, 100, 3, 10], '... got diff options now'); |
108 | |
69dde336 |
109 | lives_ok { |
110 | $stuff->insert_options(10, 20); |
111 | } '... set the option okay'; |
8c651099 |
112 | |
113 | is($stuff->num_options, 6, '... got 6 options'); |
114 | is_deeply($stuff->options, [10, 20, 1, 100, 3, 10], '... got diff options now'); |
115 | |
116 | is($stuff->get_option_at(0), 10, '... get option at index 0'); |
117 | is($stuff->get_option_at(1), 20, '... get option at index 1'); |
118 | is($stuff->get_option_at(3), 100, '... get option at index 3'); |
119 | |
120 | is($stuff->remove_first_option, 10, '... getting the first option'); |
121 | |
122 | is($stuff->num_options, 5, '... got 5 options'); |
123 | is($stuff->get_option_at(0), 20, '... get option at index 0'); |
124 | |
8cf40f80 |
125 | $stuff->clear_options; |
126 | is_deeply( $stuff->options, [], "... clear options" ); |
127 | |
71703b28 |
128 | $stuff->add_options(5, 1, 2, 3); |
129 | $stuff->sort_options_in_place; |
130 | is_deeply( $stuff->options, [1, 2, 3, 5], "... sort options in place (default sort order)" ); |
80894c0a |
131 | |
71703b28 |
132 | $stuff->sort_options_in_place( sub { $_[1] <=> $_[0] } ); |
133 | is_deeply( $stuff->options, [5, 3, 2, 1], "... sort options in place (descending order)" ); |
134 | |
135 | $stuff->clear_options(); |
136 | $stuff->add_options(5, 1, 2, 3); |
137 | lives_ok { |
138 | $stuff->descending_options(); |
139 | } '... curried sort in place lives ok'; |
140 | |
141 | is_deeply( $stuff->options, [5, 3, 2, 1], "... sort currying" ); |
80894c0a |
142 | |
71703b28 |
143 | throws_ok { $stuff->sort_options_in_place('foo') } qr/Argument must be a code reference/, |
144 | 'error when sort_in_place receives a non-coderef argument'; |
80894c0a |
145 | |
146 | $stuff->clear_options; |
147 | |
c43a2317 |
148 | lives_ok { |
149 | $stuff->add_options('tree'); |
150 | } '... set the options okay'; |
69dde336 |
151 | |
c43a2317 |
152 | lives_ok { |
153 | $stuff->add_options_with_speed('compatible', 'safe'); |
154 | } '... add options with speed okay'; |
69dde336 |
155 | |
af1ade48 |
156 | is_deeply($stuff->options, [qw/tree funrolls funbuns compatible safe/], |
157 | 'check options after add_options_with_speed'); |
69dde336 |
158 | |
c43a2317 |
159 | lives_ok { |
160 | $stuff->prepend_prerequisites_along_with(); |
161 | } '... add prerequisite options okay'; |
162 | |
af1ade48 |
163 | $stuff->clear_options; |
164 | $stuff->add_options( 1, 2 ); |
165 | |
166 | lives_ok { |
167 | $stuff->splice_options( 1, 0, 'foo' ); |
168 | } '... splice_options works'; |
169 | |
170 | is_deeply( |
171 | $stuff->options, [ 1, 'foo', 2 ], |
172 | 'splice added expected option' |
173 | ); |
174 | |
175 | |
c43a2317 |
176 | ## check some errors |
177 | |
178 | #dies_ok { |
179 | # $stuff->insert_options(undef); |
180 | #} '... could not add an undef where a string is expected'; |
181 | # |
182 | #dies_ok { |
183 | # $stuff->set_option(5, {}); |
184 | #} '... could not add a hash ref where a string is expected'; |
69dde336 |
185 | |
77d02b8b |
186 | dies_ok { |
c43a2317 |
187 | Stuff->new(options => [ undef, 10, undef, 20 ]); |
77d02b8b |
188 | } '... bad constructor params'; |
189 | |
5b380a52 |
190 | dies_ok { |
191 | my $stuff = Stuff->new(); |
192 | $stuff->add_options(undef); |
193 | } '... rejects push of an invalid type'; |
194 | |
195 | dies_ok { |
196 | my $stuff = Stuff->new(); |
197 | $stuff->insert_options(undef); |
198 | } '... rejects unshift of an invalid type'; |
199 | |
200 | dies_ok { |
201 | my $stuff = Stuff->new(); |
202 | $stuff->set_option_at( 0, undef ); |
203 | } '... rejects set of an invalid type'; |
204 | |
80894c0a |
205 | dies_ok { |
206 | my $stuff = Stuff->new(); |
207 | $stuff->sort_in_place_options( undef ); |
208 | } '... sort rejects arg of invalid type'; |
209 | |
8c651099 |
210 | ## test the meta |
211 | |
212 | my $options = $stuff->meta->get_attribute('options'); |
213 | isa_ok($options, 'MooseX::AttributeHelpers::Collection::Array'); |
22d869ff |
214 | |
8c651099 |
215 | is_deeply($options->provides, { |
216 | 'push' => 'add_options', |
217 | 'pop' => 'remove_last_option', |
218 | 'shift' => 'remove_first_option', |
219 | 'unshift' => 'insert_options', |
220 | 'get' => 'get_option_at', |
221 | 'set' => 'set_option_at', |
222 | 'count' => 'num_options', |
223 | 'empty' => 'has_options', |
8cf40f80 |
224 | 'clear' => 'clear_options', |
af1ade48 |
225 | 'splice' => 'splice_options', |
71703b28 |
226 | 'sort_in_place' => 'sort_options_in_place', |
227 | }, '... got the right provides mapping'); |
22d869ff |
228 | |
c43a2317 |
229 | is($options->type_constraint->type_parameter, 'Str', '... got the right container type'); |