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