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