adding in the new junk to this
[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 => 51;
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             'empty'   => 'has_options',        
31             'clear'   => 'clear_options',        
32         }
33     );
34 }
35
36 my $stuff = Stuff->new(options => [ 10, 12 ]);
37 isa_ok($stuff, 'Stuff');
38
39 can_ok($stuff, $_) for qw[
40     add_options
41     remove_last_option
42     remove_first_option
43     insert_options
44     get_option_at
45     set_option_at
46     num_options
47     clear_options
48     has_options
49 ];
50
51 is_deeply($stuff->options, [10, 12], '... got options');
52
53 ok($stuff->has_options, '... we have options');
54 is($stuff->num_options, 2, '... got 2 options');
55
56 is($stuff->remove_last_option, 12, '... removed the last option');
57 is($stuff->remove_first_option, 10, '... removed the last option');
58
59 is_deeply($stuff->options, [], '... no options anymore');
60
61 ok(!$stuff->has_options, '... no options');
62 is($stuff->num_options, 0, '... got no options');
63
64 lives_ok {
65     $stuff->add_options(1, 2, 3);
66 } '... set the option okay';
67
68 is_deeply($stuff->options, [1, 2, 3], '... got options now');
69
70 ok($stuff->has_options, '... no options');
71 is($stuff->num_options, 3, '... got 3 options');
72
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');
76
77 lives_ok {
78     $stuff->set_option_at(1, 100);
79 } '... set the option okay';
80
81 is($stuff->get_option_at(1), 100, '... get option at index 1');
82
83 lives_ok {
84     $stuff->add_options(10, 15);
85 } '... set the option okay';
86
87 is_deeply($stuff->options, [1, 100, 3, 10, 15], '... got more options now');
88
89 is($stuff->num_options, 5, '... got 5 options');
90
91 is($stuff->remove_last_option, 15, '... removed the last option');
92
93 is($stuff->num_options, 4, '... got 4 options');
94 is_deeply($stuff->options, [1, 100, 3, 10], '... got diff options now');
95
96 lives_ok {
97     $stuff->insert_options(10, 20);
98 } '... set the option okay';
99
100 is($stuff->num_options, 6, '... got 6 options');
101 is_deeply($stuff->options, [10, 20, 1, 100, 3, 10], '... got diff options now');
102
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');
106
107 is($stuff->remove_first_option, 10, '... getting the first option');
108
109 is($stuff->num_options, 5, '... got 5 options');
110 is($stuff->get_option_at(0), 20, '... get option at index 0');
111
112 $stuff->clear_options;
113 is_deeply( $stuff->options, [], "... clear options" );
114
115 ## check some errors
116
117 dies_ok {
118     $stuff->add_options([]);
119 } '... could not add an array ref where an int is expected';
120
121 dies_ok {
122     $stuff->insert_options(undef);
123 } '... could not add an undef where an int is expected';
124
125 dies_ok {
126     $stuff->set_option(5, {});
127 } '... could not add a hash ref where an int is expected';
128
129 dies_ok {
130     Stuff->new(options => [ 'Foo', 10, 'Bar', 20 ]);
131 } '... bad constructor params';
132
133 ## test the meta
134
135 my $options = $stuff->meta->get_attribute('options');
136 isa_ok($options, 'MooseX::AttributeHelpers::Collection::Array');
137
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');
149
150 is($options->type_constraint->type_parameter, 'Int', '... got the right container type');