Commit | Line | Data |
e3c07b19 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
7960bcc0 |
6 | use Test::More tests => 43; |
e3c07b19 |
7 | use Test::Exception; |
8 | use Test::Moose 'does_ok'; |
9 | |
18281451 |
10 | my $sort; |
11 | my $less; |
12 | my $up; |
7960bcc0 |
13 | my $prod; |
e3c07b19 |
14 | { |
15 | package Stuff; |
16 | use Moose; |
17 | |
18 | has '_options' => ( |
e11fb12c |
19 | traits => ['Array'], |
d50fc84a |
20 | is => 'ro', |
21 | isa => 'ArrayRef[Int]', |
22 | init_arg => 'options', |
23 | default => sub { [] }, |
0d103ac9 |
24 | handles => { |
391c761c |
25 | 'num_options' => 'count', |
1853a27e |
26 | 'has_no_options' => 'is_empty', |
391c761c |
27 | 'map_options', => 'map', |
28 | 'filter_options' => 'grep', |
29 | 'find_option' => 'first', |
30 | 'options' => 'elements', |
31 | 'join_options' => 'join', |
32 | 'get_option_at' => 'get', |
391c761c |
33 | 'sorted_options' => 'sort', |
7960bcc0 |
34 | 'randomized_options' => 'shuffle', |
35 | 'unique_options' => 'uniq', |
3c573ca4 |
36 | 'less_than_five' => [ grep => ($less = sub { $_ < 5 }) ], |
37 | 'up_by_one' => [ map => ($up = sub { $_ + 1 }) ], |
7960bcc0 |
38 | 'pairwise_options' => [ natatime => 2 ], |
3c573ca4 |
39 | 'dashify' => [ join => '-' ], |
40 | 'descending' => [ sort => ($sort = sub { $_[1] <=> $_[0] }) ], |
7960bcc0 |
41 | 'product' => [ reduce => ($prod = sub { $_[0] * $_[1] }) ], |
59de9de4 |
42 | }, |
e3c07b19 |
43 | ); |
59de9de4 |
44 | |
e3c07b19 |
45 | } |
46 | |
d50fc84a |
47 | my $stuff = Stuff->new( options => [ 1 .. 10 ] ); |
48 | isa_ok( $stuff, 'Stuff' ); |
e3c07b19 |
49 | |
d50fc84a |
50 | can_ok( $stuff, $_ ) for qw[ |
e3c07b19 |
51 | _options |
52 | num_options |
af44c00c |
53 | has_no_options |
e3c07b19 |
54 | map_options |
55 | filter_options |
56 | find_option |
57 | options |
58 | join_options |
59de9de4 |
59 | get_option_at |
60 | sorted_options |
7960bcc0 |
61 | randomized_options |
62 | unique_options |
63 | less_than_five |
64 | up_by_one |
65 | pairwise_options |
66 | dashify |
67 | descending |
68 | product |
e3c07b19 |
69 | ]; |
70 | |
d50fc84a |
71 | is_deeply( $stuff->_options, [ 1 .. 10 ], '... got options' ); |
e3c07b19 |
72 | |
af44c00c |
73 | ok( !$stuff->has_no_options, '... we have options' ); |
d50fc84a |
74 | is( $stuff->num_options, 10, '... got 2 options' ); |
75 | cmp_ok( $stuff->get_option_at(0), '==', 1, '... get option 0' ); |
e3c07b19 |
76 | |
77 | is_deeply( |
c9edbf05 |
78 | [ $stuff->filter_options( sub { $_ % 2 == 0 } ) ], |
d50fc84a |
79 | [ 2, 4, 6, 8, 10 ], |
80 | '... got the right filtered values' |
e3c07b19 |
81 | ); |
82 | |
83 | is_deeply( |
c9edbf05 |
84 | [ $stuff->map_options( sub { $_ * 2 } ) ], |
d50fc84a |
85 | [ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 ], |
86 | '... got the right mapped values' |
e3c07b19 |
87 | ); |
88 | |
c9edbf05 |
89 | is( $stuff->find_option( sub { $_ % 2 == 0 } ), 2, |
d50fc84a |
90 | '.. found the right option' ); |
e3c07b19 |
91 | |
d50fc84a |
92 | is_deeply( [ $stuff->options ], [ 1 .. 10 ], '... got the list of options' ); |
e3c07b19 |
93 | |
d50fc84a |
94 | is( $stuff->join_options(':'), '1:2:3:4:5:6:7:8:9:10', |
95 | '... joined the list of options by :' ); |
e3c07b19 |
96 | |
d50fc84a |
97 | is_deeply( |
98 | [ $stuff->sorted_options ], [ sort ( 1 .. 10 ) ], |
99 | '... got sorted options (default sort order)' |
100 | ); |
101 | is_deeply( |
102 | [ $stuff->sorted_options( sub { $_[1] <=> $_[0] } ) ], |
103 | [ sort { $b <=> $a } ( 1 .. 10 ) ], |
104 | '... got sorted options (descending sort order) ' |
105 | ); |
59de9de4 |
106 | |
d50fc84a |
107 | throws_ok { $stuff->sorted_options('foo') } |
108 | qr/Argument must be a code reference/, |
59de9de4 |
109 | 'error when sort receives a non-coderef argument'; |
110 | |
7960bcc0 |
111 | is_deeply( [ sort { $a <=> $b } $stuff->randomized_options ], [ 1 .. 10 ] ); |
112 | |
113 | my @pairs; |
114 | $stuff->pairwise_options(sub { push @pairs, [@_] }); |
115 | is_deeply( \@pairs, [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ], [ 7, 8 ], [ 9, 10 ] ] ); |
116 | |
59de9de4 |
117 | # test the currying |
d50fc84a |
118 | is_deeply( [ $stuff->less_than_five() ], [ 1 .. 4 ] ); |
59de9de4 |
119 | |
d50fc84a |
120 | is_deeply( [ $stuff->up_by_one() ], [ 2 .. 11 ] ); |
59de9de4 |
121 | |
d50fc84a |
122 | is( $stuff->dashify, '1-2-3-4-5-6-7-8-9-10' ); |
59de9de4 |
123 | |
d50fc84a |
124 | is_deeply( [ $stuff->descending ], [ reverse 1 .. 10 ] ); |
59de9de4 |
125 | |
7960bcc0 |
126 | is( $stuff->product, 3628800 ); |
127 | |
128 | my $other_stuff = Stuff->new( options => [ 1, 1, 2, 3, 5 ] ); |
129 | is_deeply( [ $other_stuff->unique_options ], [1, 2, 3, 5] ); |
130 | |
e3c07b19 |
131 | ## test the meta |
132 | |
133 | my $options = $stuff->meta->get_attribute('_options'); |
c466e58f |
134 | does_ok( $options, 'Moose::Meta::Attribute::Native::Trait::Array' ); |
d50fc84a |
135 | |
136 | is_deeply( |
137 | $options->handles, |
138 | { |
391c761c |
139 | 'num_options' => 'count', |
1853a27e |
140 | 'has_no_options' => 'is_empty', |
391c761c |
141 | 'map_options', => 'map', |
142 | 'filter_options' => 'grep', |
143 | 'find_option' => 'first', |
144 | 'options' => 'elements', |
145 | 'join_options' => 'join', |
146 | 'get_option_at' => 'get', |
391c761c |
147 | 'sorted_options' => 'sort', |
7960bcc0 |
148 | 'randomized_options' => 'shuffle', |
149 | 'unique_options' => 'uniq', |
3c573ca4 |
150 | 'less_than_five' => [ grep => $less ], |
151 | 'up_by_one' => [ map => $up ], |
7960bcc0 |
152 | 'pairwise_options' => [ natatime => 2 ], |
3c573ca4 |
153 | 'dashify' => [ join => '-' ], |
154 | 'descending' => [ sort => $sort ], |
7960bcc0 |
155 | 'product' => [ reduce => $prod ], |
d50fc84a |
156 | }, |
157 | '... got the right handles mapping' |
158 | ); |
159 | |
160 | is( $options->type_constraint->type_parameter, 'Int', |
161 | '... got the right container type' ); |
59de9de4 |
162 | |
163 | dies_ok { |
d50fc84a |
164 | $stuff->sort_in_place_options(undef); |
165 | } |
166 | '... sort rejects arg of invalid type'; |
59de9de4 |
167 | |