update trait tests with basic test diffs
[gitmo/MooseX-AttributeHelpers.git] / t / 203_trait_hash.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 47;
7 use Test::Exception;
8 use Test::Moose 'does_ok';
9
10 BEGIN {
11     use_ok('MooseX::AttributeHelpers');   
12 }
13
14 {
15     package Stuff;
16     use Moose;
17     use MooseX::AttributeHelpers;
18
19     has 'options' => (
20         traits    => [qw/MooseX::AttributeHelpers::Trait::Collection::Hash/],
21         is        => 'ro',
22         isa       => 'HashRef[Str]',
23         default   => sub { {} },
24         provides  => {
25             'set'      => 'set_option',
26             'get'      => 'get_option',
27             'empty'    => 'has_options',
28             'count'    => 'num_options',
29             'clear'    => 'clear_options',
30             'delete'   => 'delete_option',
31             'exists'   => 'has_option',
32             'defined'  => 'is_defined',
33             'accessor' => 'option_accessor',
34             'kv'       => 'key_value',
35             'elements' => 'options_elements',
36         },
37         curries   => {
38             'accessor' => {
39                 quantity => ['quantity'],
40             },
41         }
42     );
43 }
44
45 my $stuff = Stuff->new();
46 isa_ok($stuff, 'Stuff');
47
48 can_ok($stuff, $_) for qw[
49     set_option
50     get_option
51     has_options
52     num_options
53     delete_option
54     clear_options
55     is_defined
56     has_option
57     quantity
58     option_accessor
59 ];
60
61 ok(!$stuff->has_options, '... we have no options');
62 is($stuff->num_options, 0, '... we have no options');
63
64 is_deeply($stuff->options, {}, '... no options yet');
65 ok(!$stuff->has_option('foo'), '... we have no foo option');
66
67 lives_ok {
68     $stuff->set_option(foo => 'bar');
69 } '... set the option okay';
70
71 ok($stuff->is_defined('foo'), '... foo is defined');
72
73 ok($stuff->has_options, '... we have options');
74 is($stuff->num_options, 1, '... we have 1 option(s)');
75 ok($stuff->has_option('foo'), '... we have a foo option');
76 is_deeply($stuff->options, { foo => 'bar' }, '... got options now');
77
78 lives_ok {
79     $stuff->set_option(bar => 'baz');
80 } '... set the option okay';
81
82 is($stuff->num_options, 2, '... we have 2 option(s)');
83 is_deeply($stuff->options, { foo => 'bar', bar => 'baz' }, '... got more options now');
84
85 is($stuff->get_option('foo'), 'bar', '... got the right option');
86
87 is_deeply([ $stuff->get_option(qw(foo bar)) ], [qw(bar baz)], "get multiple options at once");
88
89 lives_ok {
90     $stuff->set_option(oink => "blah", xxy => "flop");
91 } '... set the option okay';
92
93 is($stuff->num_options, 4, "4 options");
94 is_deeply([ $stuff->get_option(qw(foo bar oink xxy)) ], [qw(bar baz blah flop)], "get multiple options at once");
95
96 lives_ok {
97     $stuff->delete_option('bar');
98 } '... deleted the option okay';
99
100 lives_ok {
101     $stuff->delete_option('oink');
102 } '... deleted the option okay';
103
104 lives_ok {
105     $stuff->delete_option('xxy');
106 } '... deleted the option okay';
107
108 is($stuff->num_options, 1, '... we have 1 option(s)');
109 is_deeply($stuff->options, { foo => 'bar' }, '... got more options now');
110
111 $stuff->clear_options;
112
113 is_deeply($stuff->options, { }, "... cleared options" );
114
115 lives_ok {
116     $stuff->quantity(4);
117 } '... options added okay with defaults';
118
119 is($stuff->quantity, 4, 'reader part of curried accessor works');
120
121 is_deeply($stuff->options, {quantity => 4}, '... returns what we expect');
122
123 lives_ok {
124     Stuff->new(options => { foo => 'BAR' });
125 } '... good constructor params';
126
127 ## check some errors
128
129 dies_ok {
130     $stuff->set_option(bar => {});
131 } '... could not add a hash ref where an string is expected';
132
133 dies_ok {
134     Stuff->new(options => { foo => [] });
135 } '... bad constructor params';
136
137 ## test the meta
138
139 my $options = $stuff->meta->get_attribute('options');
140 does_ok($options, 'MooseX::AttributeHelpers::Trait::Collection::Hash');
141
142 is_deeply($options->provides, {
143     'set'      => 'set_option',
144     'get'      => 'get_option',
145     'empty'    => 'has_options',
146     'count'    => 'num_options',
147     'clear'    => 'clear_options',
148     'delete'   => 'delete_option',
149     'defined'  => 'is_defined',
150     'exists'   => 'has_option',
151     'accessor' => 'option_accessor',
152     'kv'       => 'key_value',
153     'elements' => 'options_elements',
154 }, '... got the right provides mapping');
155
156 is($options->type_constraint->type_parameter, 'Str', '... got the right container type');
157
158 $stuff->set_option( oink => "blah", xxy => "flop" );
159 my @key_value = $stuff->key_value;
160 is_deeply(
161     \@key_value,
162     [ [ 'xxy', 'flop' ], [ 'quantity', 4 ], [ 'oink', 'blah' ] ],
163     '... got the right key value pairs'
164 );
165
166 my %options_elements = $stuff->options_elements;
167 is_deeply(
168     \%options_elements,
169     {
170         'oink'     => 'blah',
171         'quantity' => 4,
172         'xxy'      => 'flop'
173     },
174     '... got the right hash elements'
175 );