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