Commit | Line | Data |
22d869ff |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
5b380a52 |
6 | use Test::More tests => 55; |
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 { [] }, |
22 | provides => { |
8c651099 |
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', |
8cf40f80 |
31 | 'clear' => 'clear_options', |
c43a2317 |
32 | }, |
33 | curries => { |
3656a0d7 |
34 | 'push' => { |
35 | add_options_with_speed => ['funrolls', 'funbuns'] |
36 | }, |
37 | 'unshift' => { |
38 | prepend_prerequisites_along_with => ['first', 'second'] |
39 | } |
22d869ff |
40 | } |
41 | ); |
42 | } |
43 | |
77d02b8b |
44 | my $stuff = Stuff->new(options => [ 10, 12 ]); |
22d869ff |
45 | isa_ok($stuff, 'Stuff'); |
46 | |
8c651099 |
47 | can_ok($stuff, $_) for qw[ |
48 | add_options |
49 | remove_last_option |
50 | remove_first_option |
51 | insert_options |
52 | get_option_at |
53 | set_option_at |
54 | num_options |
8cf40f80 |
55 | clear_options |
8c651099 |
56 | has_options |
57 | ]; |
58 | |
77d02b8b |
59 | is_deeply($stuff->options, [10, 12], '... got options'); |
60 | |
61 | ok($stuff->has_options, '... we have options'); |
62 | is($stuff->num_options, 2, '... got 2 options'); |
63 | |
64 | is($stuff->remove_last_option, 12, '... removed the last option'); |
65 | is($stuff->remove_first_option, 10, '... removed the last option'); |
66 | |
67 | is_deeply($stuff->options, [], '... no options anymore'); |
22d869ff |
68 | |
8c651099 |
69 | ok(!$stuff->has_options, '... no options'); |
70 | is($stuff->num_options, 0, '... got no options'); |
71 | |
69dde336 |
72 | lives_ok { |
73 | $stuff->add_options(1, 2, 3); |
74 | } '... set the option okay'; |
75 | |
22d869ff |
76 | is_deeply($stuff->options, [1, 2, 3], '... got options now'); |
77 | |
8c651099 |
78 | ok($stuff->has_options, '... no options'); |
79 | is($stuff->num_options, 3, '... got 3 options'); |
80 | |
81 | is($stuff->get_option_at(0), 1, '... get option at index 0'); |
82 | is($stuff->get_option_at(1), 2, '... get option at index 1'); |
83 | is($stuff->get_option_at(2), 3, '... get option at index 2'); |
84 | |
69dde336 |
85 | lives_ok { |
86 | $stuff->set_option_at(1, 100); |
87 | } '... set the option okay'; |
8c651099 |
88 | |
89 | is($stuff->get_option_at(1), 100, '... get option at index 1'); |
90 | |
69dde336 |
91 | lives_ok { |
92 | $stuff->add_options(10, 15); |
93 | } '... set the option okay'; |
94 | |
8c651099 |
95 | is_deeply($stuff->options, [1, 100, 3, 10, 15], '... got more options now'); |
96 | |
97 | is($stuff->num_options, 5, '... got 5 options'); |
22d869ff |
98 | |
99 | is($stuff->remove_last_option, 15, '... removed the last option'); |
100 | |
8c651099 |
101 | is($stuff->num_options, 4, '... got 4 options'); |
102 | is_deeply($stuff->options, [1, 100, 3, 10], '... got diff options now'); |
103 | |
69dde336 |
104 | lives_ok { |
105 | $stuff->insert_options(10, 20); |
106 | } '... set the option okay'; |
8c651099 |
107 | |
108 | is($stuff->num_options, 6, '... got 6 options'); |
109 | is_deeply($stuff->options, [10, 20, 1, 100, 3, 10], '... got diff options now'); |
110 | |
111 | is($stuff->get_option_at(0), 10, '... get option at index 0'); |
112 | is($stuff->get_option_at(1), 20, '... get option at index 1'); |
113 | is($stuff->get_option_at(3), 100, '... get option at index 3'); |
114 | |
115 | is($stuff->remove_first_option, 10, '... getting the first option'); |
116 | |
117 | is($stuff->num_options, 5, '... got 5 options'); |
118 | is($stuff->get_option_at(0), 20, '... get option at index 0'); |
119 | |
8cf40f80 |
120 | $stuff->clear_options; |
121 | is_deeply( $stuff->options, [], "... clear options" ); |
122 | |
c43a2317 |
123 | lives_ok { |
124 | $stuff->add_options('tree'); |
125 | } '... set the options okay'; |
69dde336 |
126 | |
c43a2317 |
127 | lives_ok { |
128 | $stuff->add_options_with_speed('compatible', 'safe'); |
129 | } '... add options with speed okay'; |
69dde336 |
130 | |
c43a2317 |
131 | is_deeply($stuff->options, [qw/tree funrolls funbuns compatible safe/]); |
69dde336 |
132 | |
c43a2317 |
133 | lives_ok { |
134 | $stuff->prepend_prerequisites_along_with(); |
135 | } '... add prerequisite options okay'; |
136 | |
137 | ## check some errors |
138 | |
139 | #dies_ok { |
140 | # $stuff->insert_options(undef); |
141 | #} '... could not add an undef where a string is expected'; |
142 | # |
143 | #dies_ok { |
144 | # $stuff->set_option(5, {}); |
145 | #} '... could not add a hash ref where a string is expected'; |
69dde336 |
146 | |
77d02b8b |
147 | dies_ok { |
c43a2317 |
148 | Stuff->new(options => [ undef, 10, undef, 20 ]); |
77d02b8b |
149 | } '... bad constructor params'; |
150 | |
5b380a52 |
151 | dies_ok { |
152 | my $stuff = Stuff->new(); |
153 | $stuff->add_options(undef); |
154 | } '... rejects push of an invalid type'; |
155 | |
156 | dies_ok { |
157 | my $stuff = Stuff->new(); |
158 | $stuff->insert_options(undef); |
159 | } '... rejects unshift of an invalid type'; |
160 | |
161 | dies_ok { |
162 | my $stuff = Stuff->new(); |
163 | $stuff->set_option_at( 0, undef ); |
164 | } '... rejects set of an invalid type'; |
165 | |
8c651099 |
166 | ## test the meta |
167 | |
168 | my $options = $stuff->meta->get_attribute('options'); |
169 | isa_ok($options, 'MooseX::AttributeHelpers::Collection::Array'); |
22d869ff |
170 | |
8c651099 |
171 | is_deeply($options->provides, { |
172 | 'push' => 'add_options', |
173 | 'pop' => 'remove_last_option', |
174 | 'shift' => 'remove_first_option', |
175 | 'unshift' => 'insert_options', |
176 | 'get' => 'get_option_at', |
177 | 'set' => 'set_option_at', |
178 | 'count' => 'num_options', |
179 | 'empty' => 'has_options', |
8cf40f80 |
180 | 'clear' => 'clear_options', |
8c651099 |
181 | }, '... got the right provies mapping'); |
22d869ff |
182 | |
c43a2317 |
183 | is($options->type_constraint->type_parameter, 'Str', '... got the right container type'); |