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