Add doy to the main list of authors
[gitmo/MooseX-AttributeHelpers.git] / t / 205_trait_list.t
CommitLineData
0951a229 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More tests => 21;
7use Test::Exception;
8use Test::Moose 'does_ok';
9
10BEGIN {
11 use_ok('MooseX::AttributeHelpers');
12}
13
14{
15 package Stuff;
16 use Moose;
17
18 has '_options' => (
19 traits => [qw/Collection::List/],
20 is => 'ro',
21 isa => 'ArrayRef[Int]',
22 init_arg => 'options',
23 default => sub { [] },
24 provides => {
25 'count' => 'num_options',
26 'empty' => 'has_options',
27 'map' => 'map_options',
28 'grep' => 'filter_options',
29 'find' => 'find_option',
30 'elements' => 'options',
31 'join' => 'join_options',
32 }
33 );
34}
35
36my $stuff = Stuff->new(options => [ 1 .. 10 ]);
37isa_ok($stuff, 'Stuff');
38
39can_ok($stuff, $_) for qw[
40 _options
41 num_options
42 has_options
43 map_options
44 filter_options
45 find_option
46 options
47 join_options
48];
49
50is_deeply($stuff->_options, [1 .. 10], '... got options');
51
52ok($stuff->has_options, '... we have options');
53is($stuff->num_options, 10, '... got 2 options');
54
55is_deeply(
56[ $stuff->filter_options(sub { $_[0] % 2 == 0 }) ],
57[ 2, 4, 6, 8, 10 ],
58'... got the right filtered values'
59);
60
61is_deeply(
62[ $stuff->map_options(sub { $_[0] * 2 }) ],
63[ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 ],
64'... got the right mapped values'
65);
66
67is($stuff->find_option(sub { $_[0] % 2 == 0 }), 2, '.. found the right option');
68
69is_deeply([ $stuff->options ], [1 .. 10], '... got the list of options');
70
71is($stuff->join_options(':'), '1:2:3:4:5:6:7:8:9:10', '... joined the list of options by :');
72
73## test the meta
74
75my $options = $stuff->meta->get_attribute('_options');
76does_ok($options, 'MooseX::AttributeHelpers::Trait::Collection::List');
77
78is_deeply($options->provides, {
79 'map' => 'map_options',
80 'grep' => 'filter_options',
81 'find' => 'find_option',
82 'count' => 'num_options',
83 'empty' => 'has_options',
84 'elements' => 'options',
85 'join' => 'join_options',
86}, '... got the right provies mapping');
87
88is($options->type_constraint->type_parameter, 'Int', '... got the right container type');