some doc cleanup
[gitmo/MooseX-AttributeHelpers.git] / t / 005_basic_list.t
CommitLineData
457dc4fb 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
40c302a9 6use Test::More;
457dc4fb 7use Test::Exception;
40c302a9 8
9BEGIN {
ea986fe8 10 plan tests => 28;
40c302a9 11}
457dc4fb 12
13BEGIN {
14 use_ok('MooseX::AttributeHelpers');
15}
16
17{
18 package Stuff;
19 use Moose;
20
6f60a71e 21 has '_options' => (
457dc4fb 22 metaclass => 'Collection::List',
23 is => 'ro',
24 isa => 'ArrayRef[Int]',
6f60a71e 25 init_arg => 'options',
457dc4fb 26 default => sub { [] },
27 provides => {
6f60a71e 28 'count' => 'num_options',
29 'empty' => 'has_options',
30 'map' => 'map_options',
31 'grep' => 'filter_options',
32 'find' => 'find_option',
33 'elements' => 'options',
654096bc 34 'join' => 'join_options',
b77cfe61 35 'get' => 'get_option_at',
36 'first' => 'get_first_option',
37 'last' => 'get_last_option',
c43a2317 38 },
39 curries => {
3656a0d7 40 'grep' => {less_than_five => [ sub { $_ < 5 } ]},
41 'map' => {up_by_one => [ sub { $_ + 1 } ]},
42 'join' => {dashify => [ '-' ]}
457dc4fb 43 }
44 );
45}
46
47my $stuff = Stuff->new(options => [ 1 .. 10 ]);
48isa_ok($stuff, 'Stuff');
49
50can_ok($stuff, $_) for qw[
6f60a71e 51 _options
457dc4fb 52 num_options
53 has_options
54 map_options
55 filter_options
56 find_option
6f60a71e 57 options
654096bc 58 join_options
b77cfe61 59 get_option_at
457dc4fb 60];
61
6f60a71e 62is_deeply($stuff->_options, [1 .. 10], '... got options');
457dc4fb 63
64ok($stuff->has_options, '... we have options');
65is($stuff->num_options, 10, '... got 2 options');
b77cfe61 66cmp_ok($stuff->get_option_at(0), '==', 1, '... get option 0');
67cmp_ok($stuff->get_first_option, '==', 1, '... get first');
68cmp_ok($stuff->get_last_option, '==', 10, '... get first');
457dc4fb 69
70is_deeply(
71[ $stuff->filter_options(sub { $_[0] % 2 == 0 }) ],
72[ 2, 4, 6, 8, 10 ],
73'... got the right filtered values'
74);
75
76is_deeply(
77[ $stuff->map_options(sub { $_[0] * 2 }) ],
78[ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 ],
79'... got the right mapped values'
80);
81
82is($stuff->find_option(sub { $_[0] % 2 == 0 }), 2, '.. found the right option');
83
6f60a71e 84is_deeply([ $stuff->options ], [1 .. 10], '... got the list of options');
85
654096bc 86is($stuff->join_options(':'), '1:2:3:4:5:6:7:8:9:10', '... joined the list of options by :');
87
c43a2317 88# test the currying
89is_deeply([ $stuff->less_than_five() ], [1 .. 4]);
90
91is_deeply([ $stuff->up_by_one() ], [2 .. 11]);
92
93is($stuff->dashify, '1-2-3-4-5-6-7-8-9-10');
94
457dc4fb 95## test the meta
96
6f60a71e 97my $options = $stuff->meta->get_attribute('_options');
457dc4fb 98isa_ok($options, 'MooseX::AttributeHelpers::Collection::List');
99
100is_deeply($options->provides, {
6f60a71e 101 'map' => 'map_options',
102 'grep' => 'filter_options',
103 'find' => 'find_option',
104 'count' => 'num_options',
105 'empty' => 'has_options',
106 'elements' => 'options',
654096bc 107 'join' => 'join_options',
b77cfe61 108 'get' => 'get_option_at',
109 'first' => 'get_first_option',
110 'last' => 'get_last_option'
457dc4fb 111}, '... got the right provies mapping');
112
9a976497 113is($options->type_constraint->type_parameter, 'Int', '... got the right container type');