Update ignore files
[gitmo/Mouse.git] / Moose-t-failing / 070_native_traits / 205_trait_list.t
CommitLineData
c47cf415 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!!!
4use t::lib::MooseCompat;
5
6use strict;
7use warnings;
8
9use Test::More;
10$TODO = q{Mouse is not yet completed};
11use Test::Exception;
12use Test::Mouse 'does_ok';
13
14my $sort;
15my $less;
16my $up;
17my $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
51my $stuff = Stuff->new( options => [ 1 .. 10 ] );
52isa_ok( $stuff, 'Stuff' );
53
54can_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
75is_deeply( $stuff->_options, [ 1 .. 10 ], '... got options' );
76
77ok( !$stuff->has_no_options, '... we have options' );
78is( $stuff->num_options, 10, '... got 2 options' );
79cmp_ok( $stuff->get_option_at(0), '==', 1, '... get option 0' );
80
81is_deeply(
82 [ $stuff->filter_options( sub { $_ % 2 == 0 } ) ],
83 [ 2, 4, 6, 8, 10 ],
84 '... got the right filtered values'
85);
86
87is_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
93is( $stuff->find_option( sub { $_ % 2 == 0 } ), 2,
94 '.. found the right option' );
95
96is_deeply( [ $stuff->options ], [ 1 .. 10 ], '... got the list of options' );
97
98is( $stuff->join_options(':'), '1:2:3:4:5:6:7:8:9:10',
99 '... joined the list of options by :' );
100
101is_deeply(
102 [ $stuff->sorted_options ], [ sort ( 1 .. 10 ) ],
103 '... got sorted options (default sort order)'
104);
105is_deeply(
106 [ $stuff->sorted_options( sub { $_[1] <=> $_[0] } ) ],
107 [ sort { $b <=> $a } ( 1 .. 10 ) ],
108 '... got sorted options (descending sort order) '
109);
110
111throws_ok { $stuff->sorted_options('foo') }
112qr/Argument must be a code reference/,
113 'error when sort receives a non-coderef argument';
114
115is_deeply( [ sort { $a <=> $b } $stuff->randomized_options ], [ 1 .. 10 ] );
116
117my @pairs;
118$stuff->pairwise_options(sub { push @pairs, [@_] });
119is_deeply( \@pairs, [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ], [ 7, 8 ], [ 9, 10 ] ] );
120
121# test the currying
122is_deeply( [ $stuff->less_than_five() ], [ 1 .. 4 ] );
123
124is_deeply( [ $stuff->up_by_one() ], [ 2 .. 11 ] );
125
126is( $stuff->dashify, '1-2-3-4-5-6-7-8-9-10' );
127
128is_deeply( [ $stuff->descending ], [ reverse 1 .. 10 ] );
129
130is( $stuff->product, 3628800 );
131
132my $other_stuff = Stuff->new( options => [ 1, 1, 2, 3, 5 ] );
133is_deeply( [ $other_stuff->unique_options ], [1, 2, 3, 5] );
134
135## test the meta
136
137my $options = $stuff->meta->get_attribute('_options');
138does_ok( $options, 'Mouse::Meta::Attribute::Native::Trait::Array' );
139
140is_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
164is( $options->type_constraint->type_parameter, 'Int',
165 '... got the right container type' );
166
167dies_ok {
168 $stuff->sort_in_place_options(undef);
169}
170'... sort rejects arg of invalid type';
171
172done_testing;