6 use Test::More tests => 50;
10 use_ok('MooseX::AttributeHelpers');
16 use MooseX::AttributeHelpers;
19 metaclass => 'Collection::Hash',
21 isa => 'HashRef[Str]',
22 default => sub { {} },
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',
34 'elements' => 'options_elements',
38 quantity => ['quantity'],
44 my $stuff = Stuff->new();
45 isa_ok($stuff, 'Stuff');
47 can_ok($stuff, $_) for qw[
60 ok(!$stuff->has_options, '... we have no options');
61 is($stuff->num_options, 0, '... we have no options');
63 is_deeply($stuff->options, {}, '... no options yet');
64 ok(!$stuff->has_option('foo'), '... we have no foo option');
67 $stuff->set_option(foo => 'bar');
68 } '... set the option okay';
70 ok($stuff->is_defined('foo'), '... foo is defined');
72 ok($stuff->has_options, '... we have options');
73 is($stuff->num_options, 1, '... we have 1 option(s)');
74 ok($stuff->has_option('foo'), '... we have a foo option');
75 is_deeply($stuff->options, { foo => 'bar' }, '... got options now');
78 $stuff->set_option(bar => 'baz');
79 } '... set the option okay';
81 is($stuff->num_options, 2, '... we have 2 option(s)');
82 is_deeply($stuff->options, { foo => 'bar', bar => 'baz' }, '... got more options now');
84 is($stuff->get_option('foo'), 'bar', '... got the right option');
86 is_deeply([ $stuff->get_option(qw(foo bar)) ], [qw(bar baz)], "get multiple options at once");
89 $stuff->set_option(oink => "blah", xxy => "flop");
90 } '... set the option okay';
92 is($stuff->num_options, 4, "4 options");
93 is_deeply([ $stuff->get_option(qw(foo bar oink xxy)) ], [qw(bar baz blah flop)], "get multiple options at once");
96 $stuff->delete_option('bar');
97 } '... deleted the option okay';
100 $stuff->delete_option('oink');
101 } '... deleted the option okay';
104 $stuff->delete_option('xxy');
105 } '... deleted the option okay';
107 is($stuff->num_options, 1, '... we have 1 option(s)');
108 is_deeply($stuff->options, { foo => 'bar' }, '... got more options now');
110 $stuff->clear_options;
112 is_deeply($stuff->options, { }, "... cleared options" );
116 } '... options added okay with defaults';
118 is($stuff->quantity, 4, 'reader part of curried accessor works');
120 is_deeply($stuff->options, {quantity => 4}, '... returns what we expect');
123 Stuff->new(options => { foo => 'BAR' });
124 } '... good constructor params';
129 $stuff->set_option(bar => {});
130 } '... could not add a hash ref where an string is expected';
133 Stuff->new(options => { foo => [] });
134 } '... bad constructor params';
137 my $stuff = Stuff->new;
138 $stuff->option_accessor();
139 } '... accessor dies on 0 args';
142 my $stuff = Stuff->new;
143 $stuff->option_accessor(1 => 2, 3);
144 } '... accessor dies on 3 args';
147 my $stuff = Stuff->new;
148 $stuff->option_accessor(1 => 2, 3 => 4);
149 } '... accessor dies on 4 args';
153 my $options = $stuff->meta->get_attribute('options');
154 isa_ok($options, 'MooseX::AttributeHelpers::Collection::Hash');
156 is_deeply($options->provides, {
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',
167 'elements' => 'options_elements',
168 }, '... got the right provides mapping');
170 is($options->type_constraint->type_parameter, 'Str', '... got the right container type');
172 $stuff->set_option( oink => "blah", xxy => "flop" );
173 my @key_value = sort { $a->[0] cmp $b->[0] } $stuff->key_value;
176 [ [ 'oink', 'blah' ], [ 'quantity', 4 ], [ 'xxy', 'flop' ] ],
177 '... got the right key value pairs'
180 my %options_elements = $stuff->options_elements;
188 '... got the right hash elements'