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