Commit | Line | Data |
457dc4fb |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
40c302a9 |
6 | use Test::More; |
457dc4fb |
7 | use Test::Exception; |
40c302a9 |
8 | |
9 | BEGIN { |
72d47512 |
10 | plan tests => 29; |
40c302a9 |
11 | } |
457dc4fb |
12 | |
13 | BEGIN { |
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', |
c43a2317 |
38 | }, |
39 | curries => { |
3656a0d7 |
40 | 'grep' => {less_than_five => [ sub { $_ < 5 } ]}, |
41 | 'map' => {up_by_one => [ sub { $_ + 1 } ]}, |
42 | 'join' => {dashify => [ '-' ]} |
457dc4fb |
43 | } |
44 | ); |
72d47512 |
45 | |
46 | has animals => ( |
47 | is => 'rw', |
48 | isa => 'ArrayRef[Str]', |
49 | metaclass => 'Collection::List', |
50 | curries => { |
51 | grep => { |
52 | double_length_of => sub { |
53 | my ($self, $body, $arg) = @_; |
54 | |
55 | $body->($self, sub { length($_) == length($arg) * 2 }); |
56 | } |
57 | } |
58 | } |
59 | ) |
457dc4fb |
60 | } |
61 | |
62 | my $stuff = Stuff->new(options => [ 1 .. 10 ]); |
63 | isa_ok($stuff, 'Stuff'); |
64 | |
65 | can_ok($stuff, $_) for qw[ |
6f60a71e |
66 | _options |
457dc4fb |
67 | num_options |
68 | has_options |
69 | map_options |
70 | filter_options |
71 | find_option |
6f60a71e |
72 | options |
654096bc |
73 | join_options |
b77cfe61 |
74 | get_option_at |
457dc4fb |
75 | ]; |
76 | |
6f60a71e |
77 | is_deeply($stuff->_options, [1 .. 10], '... got options'); |
457dc4fb |
78 | |
79 | ok($stuff->has_options, '... we have options'); |
80 | is($stuff->num_options, 10, '... got 2 options'); |
b77cfe61 |
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 first'); |
457dc4fb |
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 | |
6f60a71e |
99 | is_deeply([ $stuff->options ], [1 .. 10], '... got the list of options'); |
100 | |
654096bc |
101 | is($stuff->join_options(':'), '1:2:3:4:5:6:7:8:9:10', '... joined the list of options by :'); |
102 | |
c43a2317 |
103 | # test the currying |
104 | is_deeply([ $stuff->less_than_five() ], [1 .. 4]); |
105 | |
106 | is_deeply([ $stuff->up_by_one() ], [2 .. 11]); |
107 | |
108 | is($stuff->dashify, '1-2-3-4-5-6-7-8-9-10'); |
109 | |
72d47512 |
110 | $stuff->animals([ qw/cat duck horse cattle gorilla elephant flamingo kangaroo/ ]); |
111 | |
112 | # 4 * 2 = 8 |
113 | is_deeply( |
114 | [ sort $stuff->double_length_of('fish') ], |
115 | [ sort qw/elephant flamingo kangaroo/ ], |
116 | 'returns all elements with double length of string "fish"' |
117 | ); |
118 | |
457dc4fb |
119 | ## test the meta |
120 | |
6f60a71e |
121 | my $options = $stuff->meta->get_attribute('_options'); |
457dc4fb |
122 | isa_ok($options, 'MooseX::AttributeHelpers::Collection::List'); |
123 | |
124 | is_deeply($options->provides, { |
6f60a71e |
125 | 'map' => 'map_options', |
126 | 'grep' => 'filter_options', |
127 | 'find' => 'find_option', |
128 | 'count' => 'num_options', |
129 | 'empty' => 'has_options', |
130 | 'elements' => 'options', |
654096bc |
131 | 'join' => 'join_options', |
b77cfe61 |
132 | 'get' => 'get_option_at', |
133 | 'first' => 'get_first_option', |
134 | 'last' => 'get_last_option' |
457dc4fb |
135 | }, '... got the right provies mapping'); |
136 | |
9a976497 |
137 | is($options->type_constraint->type_parameter, 'Int', '... got the right container type'); |