don't fail without DateTime
[gitmo/MooseX-AttributeHelpers.git] / t / 005_basic_list.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7 use Test::Exception;
8
9 BEGIN {
10     plan skip_all => "DateTime required" unless eval { require DateTime };
11     plan tests => 29;
12 }
13
14 BEGIN {
15     use_ok('MooseX::AttributeHelpers');   
16 }
17
18 {
19     package Stuff;
20     use Moose;
21
22     has '_options' => (
23         metaclass => 'Collection::List',
24         is        => 'ro',
25         isa       => 'ArrayRef[Int]',
26         init_arg  => 'options',
27         default   => sub { [] },
28         provides  => {
29             'count'    => 'num_options',
30             'empty'    => 'has_options',        
31             'map'      => 'map_options',
32             'grep'     => 'filter_options',
33             'find'     => 'find_option',
34             'elements' => 'options',
35             'join'     => 'join_options',
36             'get'      => 'get_option_at',
37             'first'    => 'get_first_option',
38             'last'     => 'get_last_option',
39         },
40         curries   => {
41             'grep'     => {less_than_five => [ sub { $_ < 5 } ]},
42             'map'      => {up_by_one      => [ sub { $_ + 1 } ]},
43             'join'     => {dashify        => [ '-' ]}
44         }
45     );
46
47     has datetimes => (
48         metaclass => 'Collection::List',
49         is => 'rw',
50         isa => 'ArrayRef[DateTime]',
51         curries => {
52             grep => {
53                 times_with_day => sub {
54                     my ($self, $body, $datetime) = @_;
55                     $body->($self, sub { $_->ymd eq $datetime->ymd });
56                 },
57             },
58         },
59     );
60 }
61
62 my $stuff = Stuff->new(options => [ 1 .. 10 ]);
63 isa_ok($stuff, 'Stuff');
64
65 can_ok($stuff, $_) for qw[
66     _options
67     num_options
68     has_options
69     map_options
70     filter_options
71     find_option
72     options
73     join_options
74     get_option_at
75 ];
76
77 is_deeply($stuff->_options, [1 .. 10], '... got options');
78
79 ok($stuff->has_options, '... we have options');
80 is($stuff->num_options, 10, '... got 2 options');
81 cmp_ok($stuff->get_option_at(0), '==', 1, '... get option 0');
82 cmp_ok($stuff->get_first_option, '==', 1, '... get first');
83 cmp_ok($stuff->get_last_option, '==', 10, '... get first');
84
85 is_deeply(
86 [ $stuff->filter_options(sub { $_[0] % 2 == 0 }) ],
87 [ 2, 4, 6, 8, 10 ],
88 '... got the right filtered values'
89 );
90
91 is_deeply(
92 [ $stuff->map_options(sub { $_[0] * 2 }) ],
93 [ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 ],
94 '... got the right mapped values'
95 );
96
97 is($stuff->find_option(sub { $_[0] % 2 == 0 }), 2, '.. found the right option');
98
99 is_deeply([ $stuff->options ], [1 .. 10], '... got the list of options');
100
101 is($stuff->join_options(':'), '1:2:3:4:5:6:7:8:9:10', '... joined the list of options by :');
102
103 # test the currying
104 is_deeply([ $stuff->less_than_five() ], [1 .. 4]);
105
106 is_deeply([ $stuff->up_by_one() ], [2 .. 11]);
107
108 is($stuff->dashify, '1-2-3-4-5-6-7-8-9-10');
109
110 $stuff->datetimes([
111     DateTime->now->subtract(days => 1),
112     DateTime->now->subtract(days => 1),
113     DateTime->now,
114     DateTime->now,
115 ]);
116
117 my $my_time = DateTime->now;
118
119 is($stuff->times_with_day($my_time), 2, 'check for currying with a coderef');
120
121 ## test the meta
122
123 my $options = $stuff->meta->get_attribute('_options');
124 isa_ok($options, 'MooseX::AttributeHelpers::Collection::List');
125
126 is_deeply($options->provides, {
127     'map'      => 'map_options',
128     'grep'     => 'filter_options',
129     'find'     => 'find_option',
130     'count'    => 'num_options',
131     'empty'    => 'has_options',
132     'elements' => 'options',
133     'join'     => 'join_options',
134     'get'      => 'get_option_at',
135     'first'    => 'get_first_option',
136     'last'     => 'get_last_option'
137 }, '... got the right provies mapping');
138
139 is($options->type_constraint->type_parameter, 'Int', '... got the right container type');