Changelogging
[gitmo/Mouse.git] / t-failing / 070_native_traits / 205_trait_list.t
1 #!/usr/bin/perl
2 # This is automatically generated by author/import-moose-test.pl.
3 # DO NOT EDIT THIS FILE. ANY CHANGES WILL BE LOST!!!
4 use t::lib::MooseCompat;
5
6 use strict;
7 use warnings;
8
9 use Test::More;
10 $TODO = q{Mouse is not yet completed};
11 use Test::Exception;
12 use Test::Mouse 'does_ok';
13
14 my $sort;
15 my $less;
16 my $up;
17 my $prod;
18 {
19     package Stuff;
20     use Mouse;
21
22     has '_options' => (
23         traits   => ['Array'],
24         is       => 'ro',
25         isa      => 'ArrayRef[Int]',
26         init_arg => 'options',
27         default  => sub { [] },
28         handles  => {
29             'num_options'          => 'count',
30             'has_no_options'       => 'is_empty',
31             'map_options',         => 'map',
32             'filter_options'       => 'grep',
33             'find_option'          => 'first',
34             'options'              => 'elements',
35             'join_options'         => 'join',
36             'get_option_at'        => 'get',
37             'sorted_options'       => 'sort',
38             'randomized_options'   => 'shuffle',
39             'unique_options'       => 'uniq',
40             'less_than_five'       => [ grep => ($less = sub { $_ < 5 }) ],
41             'up_by_one'            => [ map => ($up = sub { $_ + 1 }) ],
42             'pairwise_options'     => [ natatime => 2 ],
43             'dashify'    => [ join => '-' ],
44             'descending' => [ sort => ($sort = sub { $_[1] <=> $_[0] }) ],
45             'product'    => [ reduce => ($prod = sub { $_[0] * $_[1] }) ],
46         },
47     );
48
49 }
50
51 my $stuff = Stuff->new( options => [ 1 .. 10 ] );
52 isa_ok( $stuff, 'Stuff' );
53
54 can_ok( $stuff, $_ ) for qw[
55     _options
56     num_options
57     has_no_options
58     map_options
59     filter_options
60     find_option
61     options
62     join_options
63     get_option_at
64     sorted_options
65     randomized_options
66     unique_options
67     less_than_five
68     up_by_one
69     pairwise_options
70     dashify
71     descending
72     product
73 ];
74
75 is_deeply( $stuff->_options, [ 1 .. 10 ], '... got options' );
76
77 ok( !$stuff->has_no_options, '... we have options' );
78 is( $stuff->num_options, 10, '... got 2 options' );
79 cmp_ok( $stuff->get_option_at(0), '==', 1,  '... get option 0' );
80
81 is_deeply(
82     [ $stuff->filter_options( sub { $_ % 2 == 0 } ) ],
83     [ 2, 4, 6, 8, 10 ],
84     '... got the right filtered values'
85 );
86
87 is_deeply(
88     [ $stuff->map_options( sub { $_ * 2 } ) ],
89     [ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 ],
90     '... got the right mapped values'
91 );
92
93 is( $stuff->find_option( sub { $_ % 2 == 0 } ), 2,
94     '.. found the right option' );
95
96 is_deeply( [ $stuff->options ], [ 1 .. 10 ], '... got the list of options' );
97
98 is( $stuff->join_options(':'), '1:2:3:4:5:6:7:8:9:10',
99     '... joined the list of options by :' );
100
101 is_deeply(
102     [ $stuff->sorted_options ], [ sort ( 1 .. 10 ) ],
103     '... got sorted options (default sort order)'
104 );
105 is_deeply(
106     [ $stuff->sorted_options( sub { $_[1] <=> $_[0] } ) ],
107     [ sort { $b <=> $a } ( 1 .. 10 ) ],
108     '... got sorted options (descending sort order) '
109 );
110
111 throws_ok { $stuff->sorted_options('foo') }
112 qr/Argument must be a code reference/,
113     'error when sort receives a non-coderef argument';
114
115 is_deeply( [ sort { $a <=> $b } $stuff->randomized_options ], [ 1 .. 10 ] );
116
117 my @pairs;
118 $stuff->pairwise_options(sub { push @pairs, [@_] });
119 is_deeply( \@pairs, [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ], [ 7, 8 ], [ 9, 10 ] ] );
120
121 # test the currying
122 is_deeply( [ $stuff->less_than_five() ], [ 1 .. 4 ] );
123
124 is_deeply( [ $stuff->up_by_one() ], [ 2 .. 11 ] );
125
126 is( $stuff->dashify, '1-2-3-4-5-6-7-8-9-10' );
127
128 is_deeply( [ $stuff->descending ], [ reverse 1 .. 10 ] );
129
130 is( $stuff->product, 3628800 );
131
132 my $other_stuff = Stuff->new( options => [ 1, 1, 2, 3, 5 ] );
133 is_deeply( [ $other_stuff->unique_options ], [1, 2, 3, 5] );
134
135 ## test the meta
136
137 my $options = $stuff->meta->get_attribute('_options');
138 does_ok( $options, 'Mouse::Meta::Attribute::Native::Trait::Array' );
139
140 is_deeply(
141     $options->handles,
142     {
143         'num_options'          => 'count',
144         'has_no_options'       => 'is_empty',
145         'map_options',         => 'map',
146         'filter_options'       => 'grep',
147         'find_option'          => 'first',
148         'options'              => 'elements',
149         'join_options'         => 'join',
150         'get_option_at'        => 'get',
151         'sorted_options'       => 'sort',
152         'randomized_options'   => 'shuffle',
153         'unique_options'       => 'uniq',
154         'less_than_five'       => [ grep => $less ],
155         'up_by_one'            => [ map => $up ],
156         'pairwise_options'     => [ natatime => 2 ],
157         'dashify'              => [ join => '-' ],
158         'descending'           => [ sort => $sort ],
159         'product'              => [ reduce => $prod ],
160     },
161     '... got the right handles mapping'
162 );
163
164 is( $options->type_constraint->type_parameter, 'Int',
165     '... got the right container type' );
166
167 dies_ok {
168     $stuff->sort_in_place_options(undef);
169 }
170 '... sort rejects arg of invalid type';
171
172 done_testing;