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