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