6 use Test::More tests => 33;
8 use Test::Moose 'does_ok';
20 isa => 'ArrayRef[Int]',
21 init_arg => 'options',
22 default => sub { [] },
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 => [ $less = sub { $_ < 5 } ] ],
36 'up_by_one' => [ map => [ $up = sub { $_ + 1 } ] ],
37 'dashify' => [ join => ['-'] ],
38 'descending' => [ sort => [ $sort = sub { $_[1] <=> $_[0] } ] ],
44 my $stuff = Stuff->new( options => [ 1 .. 10 ] );
45 isa_ok( $stuff, 'Stuff' );
47 can_ok( $stuff, $_ ) for qw[
60 is_deeply( $stuff->_options, [ 1 .. 10 ], '... got options' );
62 ok( $stuff->has_options, '... we have options' );
63 is( $stuff->num_options, 10, '... got 2 options' );
64 cmp_ok( $stuff->get_option_at(0), '==', 1, '... get option 0' );
65 cmp_ok( $stuff->get_first_option, '==', 1, '... get first' );
66 cmp_ok( $stuff->get_last_option, '==', 10, '... get last' );
69 [ $stuff->filter_options( sub { $_[0] % 2 == 0 } ) ],
71 '... got the right filtered values'
75 [ $stuff->map_options( sub { $_[0] * 2 } ) ],
76 [ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 ],
77 '... got the right mapped values'
80 is( $stuff->find_option( sub { $_[0] % 2 == 0 } ), 2,
81 '.. found the right option' );
83 is_deeply( [ $stuff->options ], [ 1 .. 10 ], '... got the list of options' );
85 is( $stuff->join_options(':'), '1:2:3:4:5:6:7:8:9:10',
86 '... joined the list of options by :' );
89 [ $stuff->sorted_options ], [ sort ( 1 .. 10 ) ],
90 '... got sorted options (default sort order)'
93 [ $stuff->sorted_options( sub { $_[1] <=> $_[0] } ) ],
94 [ sort { $b <=> $a } ( 1 .. 10 ) ],
95 '... got sorted options (descending sort order) '
98 throws_ok { $stuff->sorted_options('foo') }
99 qr/Argument must be a code reference/,
100 'error when sort receives a non-coderef argument';
103 is_deeply( [ $stuff->less_than_five() ], [ 1 .. 4 ] );
105 is_deeply( [ $stuff->up_by_one() ], [ 2 .. 11 ] );
107 is( $stuff->dashify, '1-2-3-4-5-6-7-8-9-10' );
109 is_deeply( [ $stuff->descending ], [ reverse 1 .. 10 ] );
113 my $options = $stuff->meta->get_attribute('_options');
114 does_ok( $options, 'Moose::Meta::Attribute::Native::Trait::Array' );
119 'num_options' => 'count',
120 'has_options' => 'empty',
121 'map_options', => 'map',
122 'filter_options' => 'grep',
123 'find_option' => 'find',
124 'options' => 'elements',
125 'join_options' => 'join',
126 'get_option_at' => 'get',
127 'get_first_option' => 'first',
128 'get_last_option' => 'last',
129 'sorted_options' => 'sort',
130 'less_than_five' => [ grep => [$less] ],
131 'up_by_one' => [ map => [$up] ],
132 'dashify' => [ join => ['-'] ],
133 'descending' => [ sort => [$sort] ],
135 '... got the right handles mapping'
138 is( $options->type_constraint->type_parameter, 'Int',
139 '... got the right container type' );
142 $stuff->sort_in_place_options(undef);
144 '... sort rejects arg of invalid type';