Commit | Line | Data |
e3c07b19 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
6 | use Test::More tests => 35; |
7 | use Test::Exception; |
8 | |
9 | BEGIN { |
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 | |
55 | my $stuff = Stuff->new(options => [ 1 .. 10 ]); |
56 | isa_ok($stuff, 'Stuff'); |
57 | |
58 | can_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 | |
71 | is_deeply($stuff->_options, [1 .. 10], '... got options'); |
72 | |
73 | ok($stuff->has_options, '... we have options'); |
74 | is($stuff->num_options, 10, '... got 2 options'); |
75 | cmp_ok($stuff->get_option_at(0), '==', 1, '... get option 0'); |
76 | cmp_ok($stuff->get_first_option, '==', 1, '... get first'); |
77 | cmp_ok($stuff->get_last_option, '==', 10, '... get last'); |
78 | |
79 | is_deeply( |
80 | [ $stuff->filter_options(sub { $_[0] % 2 == 0 }) ], |
81 | [ 2, 4, 6, 8, 10 ], |
82 | '... got the right filtered values' |
83 | ); |
84 | |
85 | is_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 | |
91 | is($stuff->find_option(sub { $_[0] % 2 == 0 }), 2, '.. found the right option'); |
92 | |
93 | is_deeply([ $stuff->options ], [1 .. 10], '... got the list of options'); |
94 | |
95 | is($stuff->join_options(':'), '1:2:3:4:5:6:7:8:9:10', '... joined the list of options by :'); |
96 | |
97 | is_deeply([ $stuff->sorted_options ], [sort (1..10)], |
98 | '... got sorted options (default sort order)'); |
99 | is_deeply([ $stuff->sorted_options( sub { $_[1] <=> $_[0] } ) ], [sort { $b <=> $a } (1..10)], |
100 | '... got sorted options (descending sort order) '); |
101 | |
102 | throws_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 |
106 | is_deeply([ $stuff->less_than_five() ], [1 .. 4]); |
107 | |
108 | is_deeply([ $stuff->up_by_one() ], [2 .. 11]); |
109 | |
110 | is($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 |
115 | is_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 | |
121 | is_deeply([$stuff->descending], [reverse 1 .. 10]); |
122 | |
123 | ## test the meta |
124 | |
125 | my $options = $stuff->meta->get_attribute('_options'); |
126 | isa_ok($options, 'Moose::AttributeHelpers::Collection::List'); |
127 | |
5f70ee9c |
128 | is_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 | |
146 | is($options->type_constraint->type_parameter, 'Int', '... got the right container type'); |
147 | |
148 | dies_ok { |
149 | $stuff->sort_in_place_options( undef ); |
150 | } '... sort rejects arg of invalid type'; |
151 | |