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
8 BEGIN {
9     use_ok('MooseX::AttributeHelpers');   
10 }
11
12 {
13     package Stuff;
14     use Moose;
15
16     has 'options' => (
17         metaclass => 'Collection::Array',
18         is        => 'ro',
19         isa       => 'ArrayRef[Int]',
20         default   => sub { [] },
21         provides  => {
22             'push'    => 'add_options',
23             'pop'     => 'remove_last_option',    
24             'shift'   => 'remove_first_option',
25             'unshift' => 'insert_options',
26             'get'     => 'get_option_at',
27             'set'     => 'set_option_at',
28             'count'   => 'num_options',
29             'empty'   => 'has_options',        
30         }
31     );
32 }
33
34 my $stuff = Stuff->new();
35 isa_ok($stuff, 'Stuff');
36
37 can_ok($stuff, $_) for qw[
38     add_options
39     remove_last_option
40     remove_first_option
41     insert_options
42     get_option_at
43     set_option_at
44     num_options
45     has_options
46 ];
47
48 is_deeply($stuff->options, [], '... no options yet');
49
50 ok(!$stuff->has_options, '... no options');
51 is($stuff->num_options, 0, '... got no options');
52
53 $stuff->add_options(1, 2, 3);
54 is_deeply($stuff->options, [1, 2, 3], '... got options now');
55
56 ok($stuff->has_options, '... no options');
57 is($stuff->num_options, 3, '... got 3 options');
58
59 is($stuff->get_option_at(0), 1, '... get option at index 0');
60 is($stuff->get_option_at(1), 2, '... get option at index 1');
61 is($stuff->get_option_at(2), 3, '... get option at index 2');
62
63 $stuff->set_option_at(1, 100);
64
65 is($stuff->get_option_at(1), 100, '... get option at index 1');
66
67 $stuff->add_options(10, 15);
68 is_deeply($stuff->options, [1, 100, 3, 10, 15], '... got more options now');
69
70 is($stuff->num_options, 5, '... got 5 options');
71
72 is($stuff->remove_last_option, 15, '... removed the last option');
73
74 is($stuff->num_options, 4, '... got 4 options');
75 is_deeply($stuff->options, [1, 100, 3, 10], '... got diff options now');
76
77 $stuff->insert_options(10, 20);
78
79 is($stuff->num_options, 6, '... got 6 options');
80 is_deeply($stuff->options, [10, 20, 1, 100, 3, 10], '... got diff options now');
81
82 is($stuff->get_option_at(0), 10, '... get option at index 0');
83 is($stuff->get_option_at(1), 20, '... get option at index 1');
84 is($stuff->get_option_at(3), 100, '... get option at index 3');
85
86 is($stuff->remove_first_option, 10, '... getting the first option');
87
88 is($stuff->num_options, 5, '... got 5 options');
89 is($stuff->get_option_at(0), 20, '... get option at index 0');
90
91 ## test the meta
92
93 my $options = $stuff->meta->get_attribute('options');
94 isa_ok($options, 'MooseX::AttributeHelpers::Collection::Array');
95
96 is_deeply($options->provides, {
97     'push'    => 'add_options',
98     'pop'     => 'remove_last_option',    
99     'shift'   => 'remove_first_option',
100     'unshift' => 'insert_options',
101     'get'     => 'get_option_at',
102     'set'     => 'set_option_at',
103     'count'   => 'num_options',
104     'empty'   => 'has_options',    
105 }, '... got the right provies mapping');
106
107 is($options->container_type, 'Int', '... got the right container type');