Remove register_implementation, warn about future deprecation
[gitmo/MooseX-AttributeHelpers.git] / t / 202_trait_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 use Test::Moose 'does_ok';
9
10 BEGIN {
11     use_ok('MooseX::AttributeHelpers');   
12 }
13
14 {
15     package Stuff;
16     use Moose;
17
18     has 'options' => (
19         traits    => [qw/MooseX::AttributeHelpers::Trait::Collection::Array/],
20         is        => 'ro',
21         isa       => 'ArrayRef[Int]',
22         default   => sub { [] },
23         provides  => {
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',        
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 ];
51
52 is_deeply($stuff->options, [10, 12], '... got options');
53
54 ok($stuff->has_options, '... we have options');
55 is($stuff->num_options, 2, '... got 2 options');
56
57 is($stuff->remove_last_option, 12, '... removed the last option');
58 is($stuff->remove_first_option, 10, '... removed the last option');
59
60 is_deeply($stuff->options, [], '... no options anymore');
61
62 ok(!$stuff->has_options, '... no options');
63 is($stuff->num_options, 0, '... got no options');
64
65 lives_ok {
66     $stuff->add_options(1, 2, 3);
67 } '... set the option okay';
68
69 is_deeply($stuff->options, [1, 2, 3], '... got options now');
70
71 ok($stuff->has_options, '... no options');
72 is($stuff->num_options, 3, '... got 3 options');
73
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');
77
78 lives_ok {
79     $stuff->set_option_at(1, 100);
80 } '... set the option okay';
81
82 is($stuff->get_option_at(1), 100, '... get option at index 1');
83
84 lives_ok {
85     $stuff->add_options(10, 15);
86 } '... set the option okay';
87
88 is_deeply($stuff->options, [1, 100, 3, 10, 15], '... got more options now');
89
90 is($stuff->num_options, 5, '... got 5 options');
91
92 is($stuff->remove_last_option, 15, '... removed the last option');
93
94 is($stuff->num_options, 4, '... got 4 options');
95 is_deeply($stuff->options, [1, 100, 3, 10], '... got diff options now');
96
97 lives_ok {
98     $stuff->insert_options(10, 20);
99 } '... set the option okay';
100
101 is($stuff->num_options, 6, '... got 6 options');
102 is_deeply($stuff->options, [10, 20, 1, 100, 3, 10], '... got diff options now');
103
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');
107
108 is($stuff->remove_first_option, 10, '... getting the first option');
109
110 is($stuff->num_options, 5, '... got 5 options');
111 is($stuff->get_option_at(0), 20, '... get option at index 0');
112
113 $stuff->clear_options;
114 is_deeply( $stuff->options, [], "... clear options" );
115
116 ## check some errors
117
118 dies_ok {
119     $stuff->add_options([]);
120 } '... could not add an array ref where an int is expected';
121
122 dies_ok {
123     $stuff->insert_options(undef);
124 } '... could not add an undef where an int is expected';
125
126 dies_ok {
127     $stuff->set_option(5, {});
128 } '... could not add a hash ref where an int is expected';
129
130 dies_ok {
131     Stuff->new(options => [ 'Foo', 10, 'Bar', 20 ]);
132 } '... bad constructor params';
133
134 ## test the meta
135
136 my $options = $stuff->meta->get_attribute('options');
137 does_ok($options, 'MooseX::AttributeHelpers::Trait::Collection::Array');
138
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');
150
151 is($options->type_constraint->type_parameter, 'Int', '... got the right container type');