misc crap
[gitmo/MooseX-AttributeHelpers.git] / t / 005_basic_list.t
CommitLineData
457dc4fb 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More no_plan => 1;
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 => {
23 'count' => 'num_options',
24 'empty' => 'has_options',
25 'map' => 'map_options',
26 'grep' => 'filter_options',
27 'find' => 'find_option',
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, {
68 'map' => 'map_options',
69 'grep' => 'filter_options',
70 'find' => 'find_option',
71 'count' => 'num_options',
72 'empty' => 'has_options',
73}, '... got the right provies mapping');
74
75is($options->container_type, 'Int', '... got the right container type');