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