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