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