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