Provide an "elements" for Collection::List
[gitmo/MooseX-AttributeHelpers.git] / t / 005_basic_list.t
CommitLineData
457dc4fb 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6f60a71e 6use Test::More tests => 19;
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',
457dc4fb 30 }
31 );
32}
33
34my $stuff = Stuff->new(options => [ 1 .. 10 ]);
35isa_ok($stuff, 'Stuff');
36
37can_ok($stuff, $_) for qw[
6f60a71e 38 _options
457dc4fb 39 num_options
40 has_options
41 map_options
42 filter_options
43 find_option
6f60a71e 44 options
457dc4fb 45];
46
6f60a71e 47is_deeply($stuff->_options, [1 .. 10], '... got options');
457dc4fb 48
49ok($stuff->has_options, '... we have options');
50is($stuff->num_options, 10, '... got 2 options');
51
52is_deeply(
53[ $stuff->filter_options(sub { $_[0] % 2 == 0 }) ],
54[ 2, 4, 6, 8, 10 ],
55'... got the right filtered values'
56);
57
58is_deeply(
59[ $stuff->map_options(sub { $_[0] * 2 }) ],
60[ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 ],
61'... got the right mapped values'
62);
63
64is($stuff->find_option(sub { $_[0] % 2 == 0 }), 2, '.. found the right option');
65
6f60a71e 66is_deeply([ $stuff->options ], [1 .. 10], '... got the list of options');
67
457dc4fb 68## test the meta
69
6f60a71e 70my $options = $stuff->meta->get_attribute('_options');
457dc4fb 71isa_ok($options, 'MooseX::AttributeHelpers::Collection::List');
72
73is_deeply($options->provides, {
6f60a71e 74 'map' => 'map_options',
75 'grep' => 'filter_options',
76 'find' => 'find_option',
77 'count' => 'num_options',
78 'empty' => 'has_options',
79 'elements' => 'options',
457dc4fb 80}, '... got the right provies mapping');
81
9a976497 82is($options->type_constraint->type_parameter, 'Int', '... got the right container type');