Implemented List::sort and Array::sort_in_place. Added basic tests and pod.
[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 {
80894c0a 10 plan tests => 33;
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',
80894c0a 38 'sort' => 'sort_options',
c43a2317 39 },
40 curries => {
3656a0d7 41 'grep' => {less_than_five => [ sub { $_ < 5 } ]},
42 'map' => {up_by_one => [ sub { $_ + 1 } ]},
80894c0a 43 'join' => {dashify => [ '-' ]},
44 'sort' => {ascending => [ sub { $_[0] <=> $_[1] } ]},
457dc4fb 45 }
46 );
72d47512 47
48 has animals => (
49 is => 'rw',
50 isa => 'ArrayRef[Str]',
51 metaclass => 'Collection::List',
52 curries => {
53 grep => {
54 double_length_of => sub {
55 my ($self, $body, $arg) = @_;
56
57 $body->($self, sub { length($_) == length($arg) * 2 });
58 }
59 }
60 }
61 )
457dc4fb 62}
63
64my $stuff = Stuff->new(options => [ 1 .. 10 ]);
65isa_ok($stuff, 'Stuff');
66
67can_ok($stuff, $_) for qw[
6f60a71e 68 _options
457dc4fb 69 num_options
70 has_options
71 map_options
72 filter_options
73 find_option
6f60a71e 74 options
654096bc 75 join_options
b77cfe61 76 get_option_at
80894c0a 77 sort_options
457dc4fb 78];
79
6f60a71e 80is_deeply($stuff->_options, [1 .. 10], '... got options');
457dc4fb 81
82ok($stuff->has_options, '... we have options');
83is($stuff->num_options, 10, '... got 2 options');
b77cfe61 84cmp_ok($stuff->get_option_at(0), '==', 1, '... get option 0');
85cmp_ok($stuff->get_first_option, '==', 1, '... get first');
80894c0a 86cmp_ok($stuff->get_last_option, '==', 10, '... get last');
457dc4fb 87
88is_deeply(
89[ $stuff->filter_options(sub { $_[0] % 2 == 0 }) ],
90[ 2, 4, 6, 8, 10 ],
91'... got the right filtered values'
92);
93
94is_deeply(
95[ $stuff->map_options(sub { $_[0] * 2 }) ],
96[ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 ],
97'... got the right mapped values'
98);
99
100is($stuff->find_option(sub { $_[0] % 2 == 0 }), 2, '.. found the right option');
101
6f60a71e 102is_deeply([ $stuff->options ], [1 .. 10], '... got the list of options');
103
654096bc 104is($stuff->join_options(':'), '1:2:3:4:5:6:7:8:9:10', '... joined the list of options by :');
105
80894c0a 106is_deeply([ $stuff->sort_options( sub { $_[1] <=> $_[0] } ) ], [sort { $b <=> $a } (1..10)], '... got sorted options');
107
c43a2317 108# test the currying
109is_deeply([ $stuff->less_than_five() ], [1 .. 4]);
110
111is_deeply([ $stuff->up_by_one() ], [2 .. 11]);
112
113is($stuff->dashify, '1-2-3-4-5-6-7-8-9-10');
114
72d47512 115$stuff->animals([ qw/cat duck horse cattle gorilla elephant flamingo kangaroo/ ]);
116
117# 4 * 2 = 8
118is_deeply(
119 [ sort $stuff->double_length_of('fish') ],
120 [ sort qw/elephant flamingo kangaroo/ ],
121 'returns all elements with double length of string "fish"'
122);
123
80894c0a 124is_deeply([$stuff->ascending], [1 .. 10]);
125
457dc4fb 126## test the meta
127
6f60a71e 128my $options = $stuff->meta->get_attribute('_options');
457dc4fb 129isa_ok($options, 'MooseX::AttributeHelpers::Collection::List');
130
131is_deeply($options->provides, {
6f60a71e 132 'map' => 'map_options',
133 'grep' => 'filter_options',
134 'find' => 'find_option',
135 'count' => 'num_options',
136 'empty' => 'has_options',
137 'elements' => 'options',
654096bc 138 'join' => 'join_options',
b77cfe61 139 'get' => 'get_option_at',
140 'first' => 'get_first_option',
80894c0a 141 'last' => 'get_last_option',
142 'sort' => 'sort_options',
457dc4fb 143}, '... got the right provies mapping');
144
9a976497 145is($options->type_constraint->type_parameter, 'Int', '... got the right container type');
80894c0a 146
147dies_ok {
148 $stuff->sort_in_place_options( undef );
149} '... sort rejects arg of invalid type';
150