6 use Test::More tests => 51;
8 use Test::Moose 'does_ok';
11 use_ok('MooseX::AttributeHelpers');
19 traits => [qw/Collection::Array/],
21 isa => 'ArrayRef[Int]',
22 default => sub { [] },
24 'push' => 'add_options',
25 'pop' => 'remove_last_option',
26 'shift' => 'remove_first_option',
27 'unshift' => 'insert_options',
28 'get' => 'get_option_at',
29 'set' => 'set_option_at',
30 'count' => 'num_options',
31 'empty' => '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[
52 is_deeply($stuff->options, [10, 12], '... got options');
54 ok($stuff->has_options, '... we have options');
55 is($stuff->num_options, 2, '... got 2 options');
57 is($stuff->remove_last_option, 12, '... removed the last option');
58 is($stuff->remove_first_option, 10, '... removed the last option');
60 is_deeply($stuff->options, [], '... no options anymore');
62 ok(!$stuff->has_options, '... no options');
63 is($stuff->num_options, 0, '... got no options');
66 $stuff->add_options(1, 2, 3);
67 } '... set the option okay';
69 is_deeply($stuff->options, [1, 2, 3], '... got options now');
71 ok($stuff->has_options, '... no options');
72 is($stuff->num_options, 3, '... got 3 options');
74 is($stuff->get_option_at(0), 1, '... get option at index 0');
75 is($stuff->get_option_at(1), 2, '... get option at index 1');
76 is($stuff->get_option_at(2), 3, '... get option at index 2');
79 $stuff->set_option_at(1, 100);
80 } '... set the option okay';
82 is($stuff->get_option_at(1), 100, '... get option at index 1');
85 $stuff->add_options(10, 15);
86 } '... set the option okay';
88 is_deeply($stuff->options, [1, 100, 3, 10, 15], '... got more options now');
90 is($stuff->num_options, 5, '... got 5 options');
92 is($stuff->remove_last_option, 15, '... removed the last option');
94 is($stuff->num_options, 4, '... got 4 options');
95 is_deeply($stuff->options, [1, 100, 3, 10], '... got diff options now');
98 $stuff->insert_options(10, 20);
99 } '... set the option okay';
101 is($stuff->num_options, 6, '... got 6 options');
102 is_deeply($stuff->options, [10, 20, 1, 100, 3, 10], '... got diff options now');
104 is($stuff->get_option_at(0), 10, '... get option at index 0');
105 is($stuff->get_option_at(1), 20, '... get option at index 1');
106 is($stuff->get_option_at(3), 100, '... get option at index 3');
108 is($stuff->remove_first_option, 10, '... getting the first option');
110 is($stuff->num_options, 5, '... got 5 options');
111 is($stuff->get_option_at(0), 20, '... get option at index 0');
113 $stuff->clear_options;
114 is_deeply( $stuff->options, [], "... clear options" );
119 $stuff->add_options([]);
120 } '... could not add an array ref where an int is expected';
123 $stuff->insert_options(undef);
124 } '... could not add an undef where an int is expected';
127 $stuff->set_option(5, {});
128 } '... could not add a hash ref where an int is expected';
131 Stuff->new(options => [ 'Foo', 10, 'Bar', 20 ]);
132 } '... bad constructor params';
136 my $options = $stuff->meta->get_attribute('options');
137 does_ok($options, 'MooseX::AttributeHelpers::Trait::Collection::Array');
139 is_deeply($options->provides, {
140 'push' => 'add_options',
141 'pop' => 'remove_last_option',
142 'shift' => 'remove_first_option',
143 'unshift' => 'insert_options',
144 'get' => 'get_option_at',
145 'set' => 'set_option_at',
146 'count' => 'num_options',
147 'empty' => 'has_options',
148 'clear' => 'clear_options',
149 }, '... got the right provies mapping');
151 is($options->type_constraint->type_parameter, 'Int', '... got the right container type');