1c342e66cbcc3e232b8df38feaad9791a05defdf
[gitmo/Moose.git] / t / 070_attribute_helpers / 205_trait_list.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 33;
7 use Test::Exception;
8 use Test::Moose 'does_ok';
9
10 my $sort;
11 my $less;
12 my $up;
13 {
14     package Stuff;
15     use Moose;
16     use Moose::AttributeHelpers;
17
18     has '_options' => (
19         traits   => ['Collection::List'],
20         is       => 'ro',
21         isa      => 'ArrayRef[Int]',
22         init_arg => 'options',
23         default  => sub { [] },
24         handles  => {
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] } ] ],
40         },
41     );
42
43 }
44
45 my $stuff = Stuff->new( options => [ 1 .. 10 ] );
46 isa_ok( $stuff, 'Stuff' );
47
48 can_ok( $stuff, $_ ) for qw[
49     _options
50     num_options
51     has_options
52     map_options
53     filter_options
54     find_option
55     options
56     join_options
57     get_option_at
58     sorted_options
59 ];
60
61 is_deeply( $stuff->_options, [ 1 .. 10 ], '... got options' );
62
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' );
68
69 is_deeply(
70     [ $stuff->filter_options( sub { $_[0] % 2 == 0 } ) ],
71     [ 2, 4, 6, 8, 10 ],
72     '... got the right filtered values'
73 );
74
75 is_deeply(
76     [ $stuff->map_options( sub { $_[0] * 2 } ) ],
77     [ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 ],
78     '... got the right mapped values'
79 );
80
81 is( $stuff->find_option( sub { $_[0] % 2 == 0 } ), 2,
82     '.. found the right option' );
83
84 is_deeply( [ $stuff->options ], [ 1 .. 10 ], '... got the list of options' );
85
86 is( $stuff->join_options(':'), '1:2:3:4:5:6:7:8:9:10',
87     '... joined the list of options by :' );
88
89 is_deeply(
90     [ $stuff->sorted_options ], [ sort ( 1 .. 10 ) ],
91     '... got sorted options (default sort order)'
92 );
93 is_deeply(
94     [ $stuff->sorted_options( sub { $_[1] <=> $_[0] } ) ],
95     [ sort { $b <=> $a } ( 1 .. 10 ) ],
96     '... got sorted options (descending sort order) '
97 );
98
99 throws_ok { $stuff->sorted_options('foo') }
100 qr/Argument must be a code reference/,
101     'error when sort receives a non-coderef argument';
102
103 # test the currying
104 is_deeply( [ $stuff->less_than_five() ], [ 1 .. 4 ] );
105
106 is_deeply( [ $stuff->up_by_one() ], [ 2 .. 11 ] );
107
108 is( $stuff->dashify, '1-2-3-4-5-6-7-8-9-10' );
109
110 is_deeply( [ $stuff->descending ], [ reverse 1 .. 10 ] );
111
112 ## test the meta
113
114 my $options = $stuff->meta->get_attribute('_options');
115 does_ok( $options, 'Moose::AttributeHelpers::Trait::Collection::List' );
116
117 is_deeply(
118     $options->handles,
119     {
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] ],
135     },
136     '... got the right handles mapping'
137 );
138
139 is( $options->type_constraint->type_parameter, 'Int',
140     '... got the right container type' );
141
142 dies_ok {
143     $stuff->sort_in_place_options(undef);
144 }
145 '... sort rejects arg of invalid type';
146