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