adding support for checking container types in the constructor
[gitmo/MooseX-AttributeHelpers.git] / t / 002_basic_array.t
CommitLineData
22d869ff 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More no_plan => 1;
69dde336 7use Test::Exception;
22d869ff 8
9BEGIN {
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 => {
8c651099 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',
22d869ff 31 }
32 );
33}
34
77d02b8b 35my $stuff = Stuff->new(options => [ 10, 12 ]);
22d869ff 36isa_ok($stuff, 'Stuff');
37
8c651099 38can_ok($stuff, $_) for qw[
39 add_options
40 remove_last_option
41 remove_first_option
42 insert_options
43 get_option_at
44 set_option_at
45 num_options
46 has_options
47];
48
77d02b8b 49is_deeply($stuff->options, [10, 12], '... got options');
50
51ok($stuff->has_options, '... we have options');
52is($stuff->num_options, 2, '... got 2 options');
53
54is($stuff->remove_last_option, 12, '... removed the last option');
55is($stuff->remove_first_option, 10, '... removed the last option');
56
57is_deeply($stuff->options, [], '... no options anymore');
22d869ff 58
8c651099 59ok(!$stuff->has_options, '... no options');
60is($stuff->num_options, 0, '... got no options');
61
69dde336 62lives_ok {
63 $stuff->add_options(1, 2, 3);
64} '... set the option okay';
65
22d869ff 66is_deeply($stuff->options, [1, 2, 3], '... got options now');
67
8c651099 68ok($stuff->has_options, '... no options');
69is($stuff->num_options, 3, '... got 3 options');
70
71is($stuff->get_option_at(0), 1, '... get option at index 0');
72is($stuff->get_option_at(1), 2, '... get option at index 1');
73is($stuff->get_option_at(2), 3, '... get option at index 2');
74
69dde336 75lives_ok {
76 $stuff->set_option_at(1, 100);
77} '... set the option okay';
8c651099 78
79is($stuff->get_option_at(1), 100, '... get option at index 1');
80
69dde336 81lives_ok {
82 $stuff->add_options(10, 15);
83} '... set the option okay';
84
8c651099 85is_deeply($stuff->options, [1, 100, 3, 10, 15], '... got more options now');
86
87is($stuff->num_options, 5, '... got 5 options');
22d869ff 88
89is($stuff->remove_last_option, 15, '... removed the last option');
90
8c651099 91is($stuff->num_options, 4, '... got 4 options');
92is_deeply($stuff->options, [1, 100, 3, 10], '... got diff options now');
93
69dde336 94lives_ok {
95 $stuff->insert_options(10, 20);
96} '... set the option okay';
8c651099 97
98is($stuff->num_options, 6, '... got 6 options');
99is_deeply($stuff->options, [10, 20, 1, 100, 3, 10], '... got diff options now');
100
101is($stuff->get_option_at(0), 10, '... get option at index 0');
102is($stuff->get_option_at(1), 20, '... get option at index 1');
103is($stuff->get_option_at(3), 100, '... get option at index 3');
104
105is($stuff->remove_first_option, 10, '... getting the first option');
106
107is($stuff->num_options, 5, '... got 5 options');
108is($stuff->get_option_at(0), 20, '... get option at index 0');
109
69dde336 110## check some errors
111
112dies_ok {
113 $stuff->add_options([]);
114} '... could not add an array ref where an int is expected';
115
116dies_ok {
117 $stuff->insert_options(undef);
118} '... could not add an undef where an int is expected';
119
120dies_ok {
121 $stuff->set_option(5, {});
122} '... could not add a hash ref where an int is expected';
123
77d02b8b 124dies_ok {
125 Stuff->new(options => [ 'Foo', 10, 'Bar', 20 ]);
126} '... bad constructor params';
127
8c651099 128## test the meta
129
130my $options = $stuff->meta->get_attribute('options');
131isa_ok($options, 'MooseX::AttributeHelpers::Collection::Array');
22d869ff 132
8c651099 133is_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');
22d869ff 143
c25a396f 144is($options->container_type, 'Int', '... got the right container type');