Commit | Line | Data |
0951a229 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
301da83f |
6 | use Test::More tests => 35; |
0951a229 |
7 | use Test::Exception; |
8 | use Test::Moose 'does_ok'; |
9 | |
10 | BEGIN { |
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 | |
61 | my $stuff = Stuff->new(options => [ 1 .. 10 ]); |
62 | isa_ok($stuff, 'Stuff'); |
63 | |
64 | can_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 | |
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'); |
301da83f |
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 last'); |
0951a229 |
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 | |
301da83f |
103 | is_deeply([ $stuff->sorted_options ], [sort (1..10)], |
104 | '... got sorted options (default sort order)'); |
105 | is_deeply([ $stuff->sorted_options( sub { $_[1] <=> $_[0] } ) ], [sort { $b <=> $a } (1..10)], |
106 | '... got sorted options (descending sort order) '); |
107 | |
108 | throws_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 |
112 | is_deeply([ $stuff->less_than_five() ], [1 .. 4]); |
113 | |
114 | is_deeply([ $stuff->up_by_one() ], [2 .. 11]); |
115 | |
116 | is($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 |
121 | is_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 | |
127 | is_deeply([$stuff->descending], [reverse 1 .. 10]); |
128 | |
0951a229 |
129 | ## test the meta |
130 | |
131 | my $options = $stuff->meta->get_attribute('_options'); |
132 | does_ok($options, 'MooseX::AttributeHelpers::Trait::Collection::List'); |
133 | |
134 | is_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 | |
148 | is($options->type_constraint->type_parameter, 'Int', '... got the right container type'); |
301da83f |
149 | |
150 | dies_ok { |
151 | $stuff->sort_in_place_options( undef ); |
152 | } '... sort rejects arg of invalid type'; |
153 | |