more tests and tweaks
[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 no_plan => 1;
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         }
32     );
33 }
34
35 my $stuff = Stuff->new();
36 isa_ok($stuff, 'Stuff');
37
38 can_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
49 is_deeply($stuff->options, [], '... no options yet');
50
51 ok(!$stuff->has_options, '... no options');
52 is($stuff->num_options, 0, '... got no options');
53
54 lives_ok {
55     $stuff->add_options(1, 2, 3);
56 } '... set the option okay';
57
58 is_deeply($stuff->options, [1, 2, 3], '... got options now');
59
60 ok($stuff->has_options, '... no options');
61 is($stuff->num_options, 3, '... got 3 options');
62
63 is($stuff->get_option_at(0), 1, '... get option at index 0');
64 is($stuff->get_option_at(1), 2, '... get option at index 1');
65 is($stuff->get_option_at(2), 3, '... get option at index 2');
66
67 lives_ok {
68     $stuff->set_option_at(1, 100);
69 } '... set the option okay';
70
71 is($stuff->get_option_at(1), 100, '... get option at index 1');
72
73 lives_ok {
74     $stuff->add_options(10, 15);
75 } '... set the option okay';
76
77 is_deeply($stuff->options, [1, 100, 3, 10, 15], '... got more options now');
78
79 is($stuff->num_options, 5, '... got 5 options');
80
81 is($stuff->remove_last_option, 15, '... removed the last option');
82
83 is($stuff->num_options, 4, '... got 4 options');
84 is_deeply($stuff->options, [1, 100, 3, 10], '... got diff options now');
85
86 lives_ok {
87     $stuff->insert_options(10, 20);
88 } '... set the option okay';
89
90 is($stuff->num_options, 6, '... got 6 options');
91 is_deeply($stuff->options, [10, 20, 1, 100, 3, 10], '... got diff options now');
92
93 is($stuff->get_option_at(0), 10, '... get option at index 0');
94 is($stuff->get_option_at(1), 20, '... get option at index 1');
95 is($stuff->get_option_at(3), 100, '... get option at index 3');
96
97 is($stuff->remove_first_option, 10, '... getting the first option');
98
99 is($stuff->num_options, 5, '... got 5 options');
100 is($stuff->get_option_at(0), 20, '... get option at index 0');
101
102 ## check some errors
103
104 dies_ok {
105     $stuff->add_options([]);
106 } '... could not add an array ref where an int is expected';
107
108 dies_ok {
109     $stuff->insert_options(undef);
110 } '... could not add an undef where an int is expected';
111
112 dies_ok {
113     $stuff->set_option(5, {});
114 } '... could not add a hash ref where an int is expected';
115
116 ## test the meta
117
118 my $options = $stuff->meta->get_attribute('options');
119 isa_ok($options, 'MooseX::AttributeHelpers::Collection::Array');
120
121 is_deeply($options->provides, {
122     'push'    => 'add_options',
123     'pop'     => 'remove_last_option',    
124     'shift'   => 'remove_first_option',
125     'unshift' => 'insert_options',
126     'get'     => 'get_option_at',
127     'set'     => 'set_option_at',
128     'count'   => 'num_options',
129     'empty'   => 'has_options',    
130 }, '... got the right provies mapping');
131
132 is($options->container_type, 'Int', '... got the right container type');