6 use Test::More tests => 54;
10 use_ok('MooseX::AttributeHelpers');
18 metaclass => 'Collection::Array',
20 isa => 'ArrayRef[Int]',
21 default => sub { [] },
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',
37 my $stuff = Stuff->new(options => [ 10, 12 ]);
38 isa_ok($stuff, 'Stuff');
40 can_ok($stuff, $_) for qw[
53 is_deeply($stuff->options, [10, 12], '... got options');
55 ok($stuff->has_options, '... we have options');
56 is($stuff->num_options, 2, '... got 2 options');
58 is($stuff->remove_last_option, 12, '... removed the last option');
59 is($stuff->remove_first_option, 10, '... removed the last option');
61 is_deeply($stuff->options, [], '... no options anymore');
63 ok($stuff->no_options, '... no options');
64 is($stuff->num_options, 0, '... got no options');
67 $stuff->add_options(1, 2, 3);
68 } '... set the option okay';
70 is_deeply($stuff->options, [1, 2, 3], '... got options now');
72 ok($stuff->has_options, '... have options');
73 is($stuff->num_options, 3, '... got 3 options');
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');
80 $stuff->set_option_at(1, 100);
81 } '... set the option okay';
83 is($stuff->get_option_at(1), 100, '... get option at index 1');
86 $stuff->add_options(10, 15);
87 } '... set the option okay';
89 is_deeply($stuff->options, [1, 100, 3, 10, 15], '... got more options now');
91 is($stuff->num_options, 5, '... got 5 options');
93 is($stuff->remove_last_option, 15, '... removed the last option');
95 is($stuff->num_options, 4, '... got 4 options');
96 is_deeply($stuff->options, [1, 100, 3, 10], '... got diff options now');
99 $stuff->insert_options(10, 20);
100 } '... set the option okay';
102 is($stuff->num_options, 6, '... got 6 options');
103 is_deeply($stuff->options, [10, 20, 1, 100, 3, 10], '... got diff options now');
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');
109 is($stuff->remove_first_option, 10, '... getting the first option');
111 is($stuff->num_options, 5, '... got 5 options');
112 is($stuff->get_option_at(0), 20, '... get option at index 0');
114 $stuff->clear_options;
115 is_deeply( $stuff->options, [], "... clear options" );
118 $stuff->set_option_at([0..2], 10, 20, 30);
119 } '... set multiple options';
122 [$stuff->get_option_at(0..2)],
124 '... and got what we set'
130 $stuff->add_options([]);
131 } '... could not add an array ref where an int is expected';
134 $stuff->insert_options(undef);
135 } '... could not add an undef where an int is expected';
138 $stuff->set_option(5, {});
139 } '... could not add a hash ref where an int is expected';
142 Stuff->new(options => [ 'Foo', 10, 'Bar', 20 ]);
143 } '... bad constructor params';
147 my $options = $stuff->meta->get_attribute('options');
148 isa_ok($options, 'MooseX::AttributeHelpers::Collection::Array');
150 is_deeply($options->provides, {
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');
163 is($options->type_constraint->type_parameter, 'Int', '... got the right container type');