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