6 use Test::More tests => 51;
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 'empty' => 'has_options',
31 'clear' => 'clear_options',
36 my $stuff = Stuff->new(options => [ 10, 12 ]);
37 isa_ok($stuff, 'Stuff');
39 can_ok($stuff, $_) for qw[
51 is_deeply($stuff->options, [10, 12], '... got options');
53 ok($stuff->has_options, '... we have options');
54 is($stuff->num_options, 2, '... got 2 options');
56 is($stuff->remove_last_option, 12, '... removed the last option');
57 is($stuff->remove_first_option, 10, '... removed the last option');
59 is_deeply($stuff->options, [], '... no options anymore');
61 ok(!$stuff->has_options, '... no options');
62 is($stuff->num_options, 0, '... got no options');
65 $stuff->add_options(1, 2, 3);
66 } '... set the option okay';
68 is_deeply($stuff->options, [1, 2, 3], '... got options now');
70 ok($stuff->has_options, '... no options');
71 is($stuff->num_options, 3, '... got 3 options');
73 is($stuff->get_option_at(0), 1, '... get option at index 0');
74 is($stuff->get_option_at(1), 2, '... get option at index 1');
75 is($stuff->get_option_at(2), 3, '... get option at index 2');
78 $stuff->set_option_at(1, 100);
79 } '... set the option okay';
81 is($stuff->get_option_at(1), 100, '... get option at index 1');
84 $stuff->add_options(10, 15);
85 } '... set the option okay';
87 is_deeply($stuff->options, [1, 100, 3, 10, 15], '... got more options now');
89 is($stuff->num_options, 5, '... got 5 options');
91 is($stuff->remove_last_option, 15, '... removed the last option');
93 is($stuff->num_options, 4, '... got 4 options');
94 is_deeply($stuff->options, [1, 100, 3, 10], '... got diff options now');
97 $stuff->insert_options(10, 20);
98 } '... set the option okay';
100 is($stuff->num_options, 6, '... got 6 options');
101 is_deeply($stuff->options, [10, 20, 1, 100, 3, 10], '... got diff options now');
103 is($stuff->get_option_at(0), 10, '... get option at index 0');
104 is($stuff->get_option_at(1), 20, '... get option at index 1');
105 is($stuff->get_option_at(3), 100, '... get option at index 3');
107 is($stuff->remove_first_option, 10, '... getting the first option');
109 is($stuff->num_options, 5, '... got 5 options');
110 is($stuff->get_option_at(0), 20, '... get option at index 0');
112 $stuff->clear_options;
113 is_deeply( $stuff->options, [], "... clear options" );
118 $stuff->add_options([]);
119 } '... could not add an array ref where an int is expected';
122 $stuff->insert_options(undef);
123 } '... could not add an undef where an int is expected';
126 $stuff->set_option(5, {});
127 } '... could not add a hash ref where an int is expected';
130 Stuff->new(options => [ 'Foo', 10, 'Bar', 20 ]);
131 } '... bad constructor params';
135 my $options = $stuff->meta->get_attribute('options');
136 isa_ok($options, 'MooseX::AttributeHelpers::Collection::Array');
138 is_deeply($options->provides, {
139 'push' => 'add_options',
140 'pop' => 'remove_last_option',
141 'shift' => 'remove_first_option',
142 'unshift' => 'insert_options',
143 'get' => 'get_option_at',
144 'set' => 'set_option_at',
145 'count' => 'num_options',
146 'empty' => 'has_options',
147 'clear' => 'clear_options',
148 }, '... got the right provies mapping');
150 is($options->type_constraint->type_parameter, 'Int', '... got the right container type');