6 use Test::More tests => 33;
8 use Test::Moose 'does_ok';
16 use Moose::AttributeHelpers;
21 isa => 'ArrayRef[Int]',
22 init_arg => 'options',
23 default => sub { [] },
25 'num_options' => 'count',
26 'has_options' => 'empty',
27 'map_options', => 'map',
28 'filter_options' => 'grep',
29 'find_option' => 'find',
30 'options' => 'elements',
31 'join_options' => 'join',
32 'get_option_at' => 'get',
33 'get_first_option' => 'first',
34 'get_last_option' => 'last',
35 'sorted_options' => 'sort',
36 'less_than_five' => [ grep => [ $less = sub { $_ < 5 } ] ],
37 'up_by_one' => [ map => [ $up = sub { $_ + 1 } ] ],
38 'dashify' => [ join => ['-'] ],
39 'descending' => [ sort => [ $sort = sub { $_[1] <=> $_[0] } ] ],
45 my $stuff = Stuff->new( options => [ 1 .. 10 ] );
46 isa_ok( $stuff, 'Stuff' );
48 can_ok( $stuff, $_ ) for qw[
61 is_deeply( $stuff->_options, [ 1 .. 10 ], '... got options' );
63 ok( $stuff->has_options, '... we have options' );
64 is( $stuff->num_options, 10, '... got 2 options' );
65 cmp_ok( $stuff->get_option_at(0), '==', 1, '... get option 0' );
66 cmp_ok( $stuff->get_first_option, '==', 1, '... get first' );
67 cmp_ok( $stuff->get_last_option, '==', 10, '... get last' );
70 [ $stuff->filter_options( sub { $_[0] % 2 == 0 } ) ],
72 '... got the right filtered values'
76 [ $stuff->map_options( sub { $_[0] * 2 } ) ],
77 [ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 ],
78 '... got the right mapped values'
81 is( $stuff->find_option( sub { $_[0] % 2 == 0 } ), 2,
82 '.. found the right option' );
84 is_deeply( [ $stuff->options ], [ 1 .. 10 ], '... got the list of options' );
86 is( $stuff->join_options(':'), '1:2:3:4:5:6:7:8:9:10',
87 '... joined the list of options by :' );
90 [ $stuff->sorted_options ], [ sort ( 1 .. 10 ) ],
91 '... got sorted options (default sort order)'
94 [ $stuff->sorted_options( sub { $_[1] <=> $_[0] } ) ],
95 [ sort { $b <=> $a } ( 1 .. 10 ) ],
96 '... got sorted options (descending sort order) '
99 throws_ok { $stuff->sorted_options('foo') }
100 qr/Argument must be a code reference/,
101 'error when sort receives a non-coderef argument';
104 is_deeply( [ $stuff->less_than_five() ], [ 1 .. 4 ] );
106 is_deeply( [ $stuff->up_by_one() ], [ 2 .. 11 ] );
108 is( $stuff->dashify, '1-2-3-4-5-6-7-8-9-10' );
110 is_deeply( [ $stuff->descending ], [ reverse 1 .. 10 ] );
114 my $options = $stuff->meta->get_attribute('_options');
115 does_ok( $options, 'Moose::Meta::Attribute::Trait::Native::Array' );
120 'num_options' => 'count',
121 'has_options' => 'empty',
122 'map_options', => 'map',
123 'filter_options' => 'grep',
124 'find_option' => 'find',
125 'options' => 'elements',
126 'join_options' => 'join',
127 'get_option_at' => 'get',
128 'get_first_option' => 'first',
129 'get_last_option' => 'last',
130 'sorted_options' => 'sort',
131 'less_than_five' => [ grep => [$less] ],
132 'up_by_one' => [ map => [$up] ],
133 'dashify' => [ join => ['-'] ],
134 'descending' => [ sort => [$sort] ],
136 '... got the right handles mapping'
139 is( $options->type_constraint->type_parameter, 'Int',
140 '... got the right container type' );
143 $stuff->sort_in_place_options(undef);
145 '... sort rejects arg of invalid type';