6 use Test::More no_plan => 1;
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',
35 my $stuff = Stuff->new(options => [ 10, 12 ]);
36 isa_ok($stuff, 'Stuff');
38 can_ok($stuff, $_) for qw[
49 is_deeply($stuff->options, [10, 12], '... got options');
51 ok($stuff->has_options, '... we have options');
52 is($stuff->num_options, 2, '... got 2 options');
54 is($stuff->remove_last_option, 12, '... removed the last option');
55 is($stuff->remove_first_option, 10, '... removed the last option');
57 is_deeply($stuff->options, [], '... no options anymore');
59 ok(!$stuff->has_options, '... no options');
60 is($stuff->num_options, 0, '... got no options');
63 $stuff->add_options(1, 2, 3);
64 } '... set the option okay';
66 is_deeply($stuff->options, [1, 2, 3], '... got options now');
68 ok($stuff->has_options, '... no options');
69 is($stuff->num_options, 3, '... got 3 options');
71 is($stuff->get_option_at(0), 1, '... get option at index 0');
72 is($stuff->get_option_at(1), 2, '... get option at index 1');
73 is($stuff->get_option_at(2), 3, '... get option at index 2');
76 $stuff->set_option_at(1, 100);
77 } '... set the option okay';
79 is($stuff->get_option_at(1), 100, '... get option at index 1');
82 $stuff->add_options(10, 15);
83 } '... set the option okay';
85 is_deeply($stuff->options, [1, 100, 3, 10, 15], '... got more options now');
87 is($stuff->num_options, 5, '... got 5 options');
89 is($stuff->remove_last_option, 15, '... removed the last option');
91 is($stuff->num_options, 4, '... got 4 options');
92 is_deeply($stuff->options, [1, 100, 3, 10], '... got diff options now');
95 $stuff->insert_options(10, 20);
96 } '... set the option okay';
98 is($stuff->num_options, 6, '... got 6 options');
99 is_deeply($stuff->options, [10, 20, 1, 100, 3, 10], '... got diff options now');
101 is($stuff->get_option_at(0), 10, '... get option at index 0');
102 is($stuff->get_option_at(1), 20, '... get option at index 1');
103 is($stuff->get_option_at(3), 100, '... get option at index 3');
105 is($stuff->remove_first_option, 10, '... getting the first option');
107 is($stuff->num_options, 5, '... got 5 options');
108 is($stuff->get_option_at(0), 20, '... get option at index 0');
113 $stuff->add_options([]);
114 } '... could not add an array ref where an int is expected';
117 $stuff->insert_options(undef);
118 } '... could not add an undef where an int is expected';
121 $stuff->set_option(5, {});
122 } '... could not add a hash ref where an int is expected';
125 Stuff->new(options => [ 'Foo', 10, 'Bar', 20 ]);
126 } '... bad constructor params';
130 my $options = $stuff->meta->get_attribute('options');
131 isa_ok($options, 'MooseX::AttributeHelpers::Collection::Array');
133 is_deeply($options->provides, {
134 'push' => 'add_options',
135 'pop' => 'remove_last_option',
136 'shift' => 'remove_first_option',
137 'unshift' => 'insert_options',
138 'get' => 'get_option_at',
139 'set' => 'set_option_at',
140 'count' => 'num_options',
141 'empty' => 'has_options',
142 }, '... got the right provies mapping');
144 is($options->container_type, 'Int', '... got the right container type');