add method provider currying support
[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 => 37;
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         },
31         curries   => {
32             'set'    => [
33                 'set_with_defaults' =>
34                     size => 'medium', quantity => 1
35             ],
36         }
37     );
38 }
39
40 my $stuff = Stuff->new();
41 isa_ok($stuff, 'Stuff');
42
43 can_ok($stuff, $_) for qw[
44     set_option
45     get_option
46     has_options
47     num_options
48     delete_option
49     clear_options
50 ];
51
52 ok(!$stuff->has_options, '... we have no options');
53 is($stuff->num_options, 0, '... we have no options');
54
55 is_deeply($stuff->options, {}, '... no options yet');
56
57 lives_ok {
58     $stuff->set_option(foo => 'bar');
59 } '... set the option okay';
60
61 ok($stuff->has_options, '... we have options');
62 is($stuff->num_options, 1, '... we have 1 option(s)');
63 is_deeply($stuff->options, { foo => 'bar' }, '... got options now');
64
65 lives_ok {
66     $stuff->set_option(bar => 'baz');
67 } '... set the option okay';
68
69 is($stuff->num_options, 2, '... we have 2 option(s)');
70 is_deeply($stuff->options, { foo => 'bar', bar => 'baz' }, '... got more options now');
71
72 is($stuff->get_option('foo'), 'bar', '... got the right option');
73
74 is_deeply([ $stuff->get_option(qw(foo bar)) ], [qw(bar baz)], "get multiple options at once");
75
76 lives_ok {
77     $stuff->set_option(oink => "blah", xxy => "flop");
78 } '... set the option okay';
79
80 is($stuff->num_options, 4, "4 options");
81 is_deeply([ $stuff->get_option(qw(foo bar oink xxy)) ], [qw(bar baz blah flop)], "get multiple options at once");
82
83 lives_ok {
84     $stuff->delete_option('bar');
85 } '... deleted the option okay';
86
87 lives_ok {
88     $stuff->delete_option('oink');
89 } '... deleted the option okay';
90
91 lives_ok {
92     $stuff->delete_option('xxy');
93 } '... deleted the option okay';
94
95 is($stuff->num_options, 1, '... we have 1 option(s)');
96 is_deeply($stuff->options, { foo => 'bar' }, '... got more options now');
97
98 $stuff->clear_options;
99
100 is_deeply($stuff->options, { }, "... cleared options" );
101
102 lives_ok {
103     $stuff->set_with_defaults(foo => 'bar');
104 } '... options added okay with defaults';
105
106 is_deeply($stuff->options, {size => 'medium', quantity => 1, foo => 'bar'});
107
108 lives_ok {
109     Stuff->new(options => { foo => 'BAR' });
110 } '... good constructor params';
111
112 ## check some errors
113
114 dies_ok {
115     $stuff->set_option(bar => {});
116 } '... could not add a hash ref where an string is expected';
117
118 dies_ok {
119     Stuff->new(options => { foo => [] });
120 } '... bad constructor params';
121
122 ## test the meta
123
124 my $options = $stuff->meta->get_attribute('options');
125 isa_ok($options, 'MooseX::AttributeHelpers::Collection::Hash');
126
127 is_deeply($options->provides, {
128     'set'    => 'set_option',
129     'get'    => 'get_option',            
130     'empty'  => 'has_options',
131     'count'  => 'num_options',
132     'clear'  => 'clear_options',
133     'delete' => 'delete_option',
134 }, '... got the right provies mapping');
135
136 is($options->type_constraint->type_parameter, 'Str', '... got the right container type');