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