ec0647cd6536784b4e322d0a56f2f35aa9f9efd6
[gitmo/MooseX-AttributeHelpers.git] / t / 205_trait_list.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 21;
7 use Test::Exception;
8 use Test::Moose 'does_ok';
9
10 BEGIN {
11     use_ok('MooseX::AttributeHelpers');   
12 }
13
14 {
15     package Stuff;
16     use Moose;
17
18     has '_options' => (
19         traits    => [qw/MooseX::AttributeHelpers::Trait::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
36 my $stuff = Stuff->new(options => [ 1 .. 10 ]);
37 isa_ok($stuff, 'Stuff');
38
39 can_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
50 is_deeply($stuff->_options, [1 .. 10], '... got options');
51
52 ok($stuff->has_options, '... we have options');
53 is($stuff->num_options, 10, '... got 2 options');
54
55 is_deeply(
56 [ $stuff->filter_options(sub { $_[0] % 2 == 0 }) ],
57 [ 2, 4, 6, 8, 10 ],
58 '... got the right filtered values'
59 );
60
61 is_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
67 is($stuff->find_option(sub { $_[0] % 2 == 0 }), 2, '.. found the right option');
68
69 is_deeply([ $stuff->options ], [1 .. 10], '... got the list of options');
70
71 is($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
75 my $options = $stuff->meta->get_attribute('_options');
76 does_ok($options, 'MooseX::AttributeHelpers::Trait::Collection::List');
77
78 is_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
88 is($options->type_constraint->type_parameter, 'Int', '... got the right container type');