All unit tests passing with refactored stuff, documentation updated significantly.
[gitmo/MooseX-AttributeHelpers.git] / t / 002_basic_array.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 54;
7 use Test::Exception;
8
9 BEGIN {
10     use_ok('MooseX::AttributeHelpers');   
11 }
12
13 {
14     package Stuff;
15     use Moose;
16
17     has 'options' => (
18         metaclass => 'Collection::Array',
19         is        => 'ro',
20         isa       => 'ArrayRef[Int]',
21         default   => sub { [] },
22         provides  => {
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',        
33         }
34     );
35 }
36
37 my $stuff = Stuff->new(options => [ 10, 12 ]);
38 isa_ok($stuff, 'Stuff');
39
40 can_ok($stuff, $_) for qw[
41     add_options
42     remove_last_option
43     remove_first_option
44     insert_options
45     get_option_at
46     set_option_at
47     num_options
48     clear_options
49     has_options
50     no_options
51 ];
52
53 is_deeply($stuff->options, [10, 12], '... got options');
54
55 ok($stuff->has_options, '... we have options');
56 is($stuff->num_options, 2, '... got 2 options');
57
58 is($stuff->remove_last_option, 12, '... removed the last option');
59 is($stuff->remove_first_option, 10, '... removed the last option');
60
61 is_deeply($stuff->options, [], '... no options anymore');
62
63 ok($stuff->no_options, '... no options');
64 is($stuff->num_options, 0, '... got no options');
65
66 lives_ok {
67     $stuff->add_options(1, 2, 3);
68 } '... set the option okay';
69
70 is_deeply($stuff->options, [1, 2, 3], '... got options now');
71
72 ok($stuff->has_options, '... have options');
73 is($stuff->num_options, 3, '... got 3 options');
74
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');
78
79 lives_ok {
80     $stuff->set_option_at(1, 100);
81 } '... set the option okay';
82
83 is($stuff->get_option_at(1), 100, '... get option at index 1');
84
85 lives_ok {
86     $stuff->add_options(10, 15);
87 } '... set the option okay';
88
89 is_deeply($stuff->options, [1, 100, 3, 10, 15], '... got more options now');
90
91 is($stuff->num_options, 5, '... got 5 options');
92
93 is($stuff->remove_last_option, 15, '... removed the last option');
94
95 is($stuff->num_options, 4, '... got 4 options');
96 is_deeply($stuff->options, [1, 100, 3, 10], '... got diff options now');
97
98 lives_ok {
99     $stuff->insert_options(10, 20);
100 } '... set the option okay';
101
102 is($stuff->num_options, 6, '... got 6 options');
103 is_deeply($stuff->options, [10, 20, 1, 100, 3, 10], '... got diff options now');
104
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');
108
109 is($stuff->remove_first_option, 10, '... getting the first option');
110
111 is($stuff->num_options, 5, '... got 5 options');
112 is($stuff->get_option_at(0), 20, '... get option at index 0');
113
114 $stuff->clear_options;
115 is_deeply( $stuff->options, [], "... clear options" );
116
117 lives_ok {
118     $stuff->set_option_at([0..2], 10, 20, 30);
119 } '... set multiple options';
120
121 is_deeply(
122     [$stuff->get_option_at(0..2)], 
123     [10,20,30], 
124     '... and got what we set'
125 );
126
127 ## check some errors
128
129 dies_ok {
130     $stuff->add_options([]);
131 } '... could not add an array ref where an int is expected';
132
133 dies_ok {
134     $stuff->insert_options(undef);
135 } '... could not add an undef where an int is expected';
136
137 dies_ok {
138     $stuff->set_option(5, {});
139 } '... could not add a hash ref where an int is expected';
140
141 dies_ok {
142     Stuff->new(options => [ 'Foo', 10, 'Bar', 20 ]);
143 } '... bad constructor params';
144
145 ## test the meta
146
147 my $options = $stuff->meta->get_attribute('options');
148 isa_ok($options, 'MooseX::AttributeHelpers::Collection::Array');
149
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');
162
163 is($options->type_constraint->type_parameter, 'Int', '... got the right container type');