add whitespace for clarity
[gitmo/MooseX-AttributeHelpers.git] / t / 005_basic_list.t
CommitLineData
457dc4fb 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
40c302a9 6use Test::More;
457dc4fb 7use Test::Exception;
40c302a9 8
9BEGIN {
d57bba34 10 plan tests => 36;
40c302a9 11}
457dc4fb 12
13BEGIN {
14 use_ok('MooseX::AttributeHelpers');
15}
16
17{
18 package Stuff;
19 use Moose;
20
6f60a71e 21 has '_options' => (
457dc4fb 22 metaclass => 'Collection::List',
23 is => 'ro',
24 isa => 'ArrayRef[Int]',
6f60a71e 25 init_arg => 'options',
457dc4fb 26 default => sub { [] },
27 provides => {
6f60a71e 28 'count' => 'num_options',
29 'empty' => 'has_options',
30 'map' => 'map_options',
31 'grep' => 'filter_options',
32 'find' => 'find_option',
33 'elements' => 'options',
654096bc 34 'join' => 'join_options',
b77cfe61 35 'get' => 'get_option_at',
36 'first' => 'get_first_option',
37 'last' => 'get_last_option',
d57bba34 38 'contains' => 'options_contains',
c43a2317 39 },
40 curries => {
3656a0d7 41 'grep' => {less_than_five => [ sub { $_ < 5 } ]},
42 'map' => {up_by_one => [ sub { $_ + 1 } ]},
43 'join' => {dashify => [ '-' ]}
457dc4fb 44 }
45 );
72d47512 46
47 has animals => (
48 is => 'rw',
49 isa => 'ArrayRef[Str]',
50 metaclass => 'Collection::List',
51 curries => {
52 grep => {
53 double_length_of => sub {
54 my ($self, $body, $arg) = @_;
55
56 $body->($self, sub { length($_) == length($arg) * 2 });
57 }
58 }
59 }
60 )
457dc4fb 61}
62
63my $stuff = Stuff->new(options => [ 1 .. 10 ]);
64isa_ok($stuff, 'Stuff');
65
66can_ok($stuff, $_) for qw[
6f60a71e 67 _options
457dc4fb 68 num_options
69 has_options
70 map_options
71 filter_options
72 find_option
6f60a71e 73 options
654096bc 74 join_options
b77cfe61 75 get_option_at
d57bba34 76 options_contains
457dc4fb 77];
78
6f60a71e 79is_deeply($stuff->_options, [1 .. 10], '... got options');
457dc4fb 80
81ok($stuff->has_options, '... we have options');
82is($stuff->num_options, 10, '... got 2 options');
b77cfe61 83cmp_ok($stuff->get_option_at(0), '==', 1, '... get option 0');
84cmp_ok($stuff->get_first_option, '==', 1, '... get first');
85cmp_ok($stuff->get_last_option, '==', 10, '... get first');
457dc4fb 86
87is_deeply(
88[ $stuff->filter_options(sub { $_[0] % 2 == 0 }) ],
89[ 2, 4, 6, 8, 10 ],
90'... got the right filtered values'
91);
92
93is_deeply(
94[ $stuff->map_options(sub { $_[0] * 2 }) ],
95[ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 ],
96'... got the right mapped values'
97);
98
99is($stuff->find_option(sub { $_[0] % 2 == 0 }), 2, '.. found the right option');
100
6f60a71e 101is_deeply([ $stuff->options ], [1 .. 10], '... got the list of options');
102
654096bc 103is($stuff->join_options(':'), '1:2:3:4:5:6:7:8:9:10', '... joined the list of options by :');
104
d7fc3bfd 105is($stuff->options_contains(), 0 ,'... does not contain undef');
106is($stuff->options_contains(5), 1 ,'... contains "5"');
107is($stuff->options_contains(11), 0 ,'... does not contain "11"');
108
d57bba34 109push @{$stuff->_options}, undef;
d7fc3bfd 110is($stuff->options_contains(undef), 1 ,'... does contain undef');
111
d57bba34 112push @{$stuff->_options}, 5;
d7fc3bfd 113is($stuff->options_contains(5), 2 ,'... contains returns count');
114
d57bba34 115splice @{$stuff->_options}, -2;
116is_deeply($stuff->_options, [1 .. 10], '... reset list for regression');
117
c43a2317 118# test the currying
119is_deeply([ $stuff->less_than_five() ], [1 .. 4]);
120
121is_deeply([ $stuff->up_by_one() ], [2 .. 11]);
122
123is($stuff->dashify, '1-2-3-4-5-6-7-8-9-10');
124
72d47512 125$stuff->animals([ qw/cat duck horse cattle gorilla elephant flamingo kangaroo/ ]);
126
127# 4 * 2 = 8
128is_deeply(
129 [ sort $stuff->double_length_of('fish') ],
130 [ sort qw/elephant flamingo kangaroo/ ],
131 'returns all elements with double length of string "fish"'
132);
133
457dc4fb 134## test the meta
135
6f60a71e 136my $options = $stuff->meta->get_attribute('_options');
457dc4fb 137isa_ok($options, 'MooseX::AttributeHelpers::Collection::List');
138
139is_deeply($options->provides, {
6f60a71e 140 'map' => 'map_options',
141 'grep' => 'filter_options',
142 'find' => 'find_option',
143 'count' => 'num_options',
144 'empty' => 'has_options',
145 'elements' => 'options',
654096bc 146 'join' => 'join_options',
b77cfe61 147 'get' => 'get_option_at',
148 'first' => 'get_first_option',
d57bba34 149 'last' => 'get_last_option',
150 'contains' => 'options_contains',
457dc4fb 151}, '... got the right provies mapping');
152
9a976497 153is($options->type_constraint->type_parameter, 'Int', '... got the right container type');