6 use Test::More tests => 47;
8 use Test::Moose 'does_ok';
11 use_ok('MooseX::AttributeHelpers');
17 use MooseX::AttributeHelpers;
20 traits => [qw/MooseX::AttributeHelpers::Trait::Collection::Hash/],
22 isa => 'HashRef[Str]',
23 default => sub { {} },
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',
35 'elements' => 'options_elements',
39 quantity => ['quantity'],
45 my $stuff = Stuff->new();
46 isa_ok($stuff, 'Stuff');
48 can_ok($stuff, $_) for qw[
61 ok(!$stuff->has_options, '... we have no options');
62 is($stuff->num_options, 0, '... we have no options');
64 is_deeply($stuff->options, {}, '... no options yet');
65 ok(!$stuff->has_option('foo'), '... we have no foo option');
68 $stuff->set_option(foo => 'bar');
69 } '... set the option okay';
71 ok($stuff->is_defined('foo'), '... foo is defined');
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');
79 $stuff->set_option(bar => 'baz');
80 } '... set the option okay';
82 is($stuff->num_options, 2, '... we have 2 option(s)');
83 is_deeply($stuff->options, { foo => 'bar', bar => 'baz' }, '... got more options now');
85 is($stuff->get_option('foo'), 'bar', '... got the right option');
87 is_deeply([ $stuff->get_option(qw(foo bar)) ], [qw(bar baz)], "get multiple options at once");
90 $stuff->set_option(oink => "blah", xxy => "flop");
91 } '... set the option okay';
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");
97 $stuff->delete_option('bar');
98 } '... deleted the option okay';
101 $stuff->delete_option('oink');
102 } '... deleted the option okay';
105 $stuff->delete_option('xxy');
106 } '... deleted the option okay';
108 is($stuff->num_options, 1, '... we have 1 option(s)');
109 is_deeply($stuff->options, { foo => 'bar' }, '... got more options now');
111 $stuff->clear_options;
113 is_deeply($stuff->options, { }, "... cleared options" );
117 } '... options added okay with defaults';
119 is($stuff->quantity, 4, 'reader part of curried accessor works');
121 is_deeply($stuff->options, {quantity => 4}, '... returns what we expect');
124 Stuff->new(options => { foo => 'BAR' });
125 } '... good constructor params';
130 $stuff->set_option(bar => {});
131 } '... could not add a hash ref where an string is expected';
134 Stuff->new(options => { foo => [] });
135 } '... bad constructor params';
139 my $options = $stuff->meta->get_attribute('options');
140 does_ok($options, 'MooseX::AttributeHelpers::Trait::Collection::Hash');
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',
153 'elements' => 'options_elements',
154 }, '... got the right provides mapping');
156 is($options->type_constraint->type_parameter, 'Str', '... got the right container type');
158 $stuff->set_option( oink => "blah", xxy => "flop" );
159 my @key_value = sort { $a->[0] cmp $b->[0] } $stuff->key_value;
162 [ [ 'oink', 'blah' ], [ 'quantity', 4 ], [ 'xxy', 'flop' ] ],
163 '... got the right key value pairs'
166 my %options_elements = $stuff->options_elements;
174 '... got the right hash elements'