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