Had duplicate version and authority in MethodProvider.pm
[gitmo/MooseX-AttributeHelpers.git] / t / 005_basic_list.t
CommitLineData
457dc4fb 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
9a976497 6use Test::More tests => 16;
457dc4fb 7use Test::Exception;
8
9BEGIN {
10 use_ok('MooseX::AttributeHelpers');
11}
12
13{
14 package Stuff;
15 use Moose;
16
17 has 'options' => (
18 metaclass => 'Collection::List',
19 is => 'ro',
20 isa => 'ArrayRef[Int]',
21 default => sub { [] },
22 provides => {
720fa35b 23 'count' => 'num_options',
24 'has_items' => 'has_options',
25 'map' => 'map_options',
26 'grep' => 'filter_options',
27 'find' => 'find_option',
457dc4fb 28 }
29 );
30}
31
32my $stuff = Stuff->new(options => [ 1 .. 10 ]);
33isa_ok($stuff, 'Stuff');
34
35can_ok($stuff, $_) for qw[
36 num_options
37 has_options
38 map_options
39 filter_options
40 find_option
41];
42
43is_deeply($stuff->options, [1 .. 10], '... got options');
44
45ok($stuff->has_options, '... we have options');
46is($stuff->num_options, 10, '... got 2 options');
47
48is_deeply(
49[ $stuff->filter_options(sub { $_[0] % 2 == 0 }) ],
50[ 2, 4, 6, 8, 10 ],
51'... got the right filtered values'
52);
53
54is_deeply(
55[ $stuff->map_options(sub { $_[0] * 2 }) ],
56[ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 ],
57'... got the right mapped values'
58);
59
60is($stuff->find_option(sub { $_[0] % 2 == 0 }), 2, '.. found the right option');
61
62## test the meta
63
64my $options = $stuff->meta->get_attribute('options');
65isa_ok($options, 'MooseX::AttributeHelpers::Collection::List');
66
67is_deeply($options->provides, {
720fa35b 68 'map' => 'map_options',
69 'grep' => 'filter_options',
70 'find' => 'find_option',
71 'count' => 'num_options',
72 'has_items' => 'has_options',
73}, '... got the right provides mapping');
457dc4fb 74
9a976497 75is($options->type_constraint->type_parameter, 'Int', '... got the right container type');