Add doy to the main list of authors
[gitmo/MooseX-AttributeHelpers.git] / t / 003_basic_hash.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 48;
7 use Test::Exception;
8
9 BEGIN {
10     use_ok('MooseX::AttributeHelpers');
11 }
12
13 {
14     package Stuff;
15     use Moose;
16     use MooseX::AttributeHelpers;
17
18     has 'options' => (
19         metaclass => 'Collection::Hash',
20         is        => 'ro',
21         isa       => 'HashRef[Str]',
22         default   => sub { {} },
23         provides  => {
24             'set'      => 'set_option',
25             'get'      => 'get_option',
26             'empty'    => 'has_options',
27             'count'    => 'num_options',
28             'clear'    => 'clear_options',
29             'delete'   => 'delete_option',
30             'exists'   => 'has_option',
31             'defined'  => 'is_defined',
32             'accessor' => 'option_accessor',
33         },
34         curries   => {
35             'accessor' => {
36                 quantity => ['quantity'],
37             },
38         }
39     );
40 }
41
42 my $stuff = Stuff->new();
43 isa_ok($stuff, 'Stuff');
44
45 can_ok($stuff, $_) for qw[
46     set_option
47     get_option
48     has_options
49     num_options
50     delete_option
51     clear_options
52     is_defined
53     has_option
54     quantity
55     option_accessor
56 ];
57
58 ok(!$stuff->has_options, '... we have no options');
59 is($stuff->num_options, 0, '... we have no options');
60
61 is_deeply($stuff->options, {}, '... no options yet');
62 ok(!$stuff->has_option('foo'), '... we have no foo option');
63
64 lives_ok {
65     $stuff->set_option(foo => 'bar');
66 } '... set the option okay';
67
68 ok($stuff->is_defined('foo'), '... foo is defined');
69
70 ok($stuff->has_options, '... we have options');
71 is($stuff->num_options, 1, '... we have 1 option(s)');
72 ok($stuff->has_option('foo'), '... we have a foo option');
73 is_deeply($stuff->options, { foo => 'bar' }, '... got options now');
74
75 lives_ok {
76     $stuff->set_option(bar => 'baz');
77 } '... set the option okay';
78
79 is($stuff->num_options, 2, '... we have 2 option(s)');
80 is_deeply($stuff->options, { foo => 'bar', bar => 'baz' }, '... got more options now');
81
82 is($stuff->get_option('foo'), 'bar', '... got the right option');
83
84 is_deeply([ $stuff->get_option(qw(foo bar)) ], [qw(bar baz)], "get multiple options at once");
85
86 lives_ok {
87     $stuff->set_option(oink => "blah", xxy => "flop");
88 } '... set the option okay';
89
90 is($stuff->num_options, 4, "4 options");
91 is_deeply([ $stuff->get_option(qw(foo bar oink xxy)) ], [qw(bar baz blah flop)], "get multiple options at once");
92
93 lives_ok {
94     $stuff->delete_option('bar');
95 } '... deleted the option okay';
96
97 lives_ok {
98     $stuff->delete_option('oink');
99 } '... deleted the option okay';
100
101 lives_ok {
102     $stuff->delete_option('xxy');
103 } '... deleted the option okay';
104
105 is($stuff->num_options, 1, '... we have 1 option(s)');
106 is_deeply($stuff->options, { foo => 'bar' }, '... got more options now');
107
108 $stuff->clear_options;
109
110 is_deeply($stuff->options, { }, "... cleared options" );
111
112 lives_ok {
113     $stuff->quantity(4);
114 } '... options added okay with defaults';
115
116 is($stuff->quantity, 4, 'reader part of curried accessor works');
117
118 is_deeply($stuff->options, {quantity => 4}, '... returns what we expect');
119
120 lives_ok {
121     Stuff->new(options => { foo => 'BAR' });
122 } '... good constructor params';
123
124 ## check some errors
125
126 dies_ok {
127     $stuff->set_option(bar => {});
128 } '... could not add a hash ref where an string is expected';
129
130 dies_ok {
131     Stuff->new(options => { foo => [] });
132 } '... bad constructor params';
133
134 dies_ok {
135     my $stuff = Stuff->new;
136     $stuff->option_accessor();
137 } '... accessor dies on 0 args';
138
139 dies_ok {
140     my $stuff = Stuff->new;
141     $stuff->option_accessor(1 => 2, 3);
142 } '... accessor dies on 3 args';
143
144 dies_ok {
145     my $stuff = Stuff->new;
146     $stuff->option_accessor(1 => 2, 3 => 4);
147 } '... accessor dies on 4 args';
148
149 ## test the meta
150
151 my $options = $stuff->meta->get_attribute('options');
152 isa_ok($options, 'MooseX::AttributeHelpers::Collection::Hash');
153
154 is_deeply($options->provides, {
155     'set'      => 'set_option',
156     'get'      => 'get_option',
157     'empty'    => 'has_options',
158     'count'    => 'num_options',
159     'clear'    => 'clear_options',
160     'delete'   => 'delete_option',
161     'defined'  => 'is_defined',
162     'exists'   => 'has_option',
163     'accessor' => 'option_accessor',
164 }, '... got the right provides mapping');
165
166 is($options->type_constraint->type_parameter, 'Str', '... got the right container type');