6 use Test::More tests => 40;
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',
34 set_quantity => ['quantity']
40 my $stuff = Stuff->new();
41 isa_ok($stuff, 'Stuff');
43 can_ok($stuff, $_) for qw[
53 ok(!$stuff->has_options, '... we have no options');
54 is($stuff->num_options, 0, '... we have no options');
56 is_deeply($stuff->options, {}, '... no options yet');
57 ok(!$stuff->has_option('foo'), '... we have no foo option');
60 $stuff->set_option(foo => 'bar');
61 } '... set the option okay';
63 ok($stuff->has_options, '... we have options');
64 is($stuff->num_options, 1, '... we have 1 option(s)');
65 ok($stuff->has_option('foo'), '... we have a foo option');
66 is_deeply($stuff->options, { foo => 'bar' }, '... got options now');
69 $stuff->set_option(bar => 'baz');
70 } '... set the option okay';
72 is($stuff->num_options, 2, '... we have 2 option(s)');
73 is_deeply($stuff->options, { foo => 'bar', bar => 'baz' }, '... got more options now');
75 is($stuff->get_option('foo'), 'bar', '... got the right option');
77 is_deeply([ $stuff->get_option(qw(foo bar)) ], [qw(bar baz)], "get multiple options at once");
80 $stuff->set_option(oink => "blah", xxy => "flop");
81 } '... set the option okay';
83 is($stuff->num_options, 4, "4 options");
84 is_deeply([ $stuff->get_option(qw(foo bar oink xxy)) ], [qw(bar baz blah flop)], "get multiple options at once");
87 $stuff->delete_option('bar');
88 } '... deleted the option okay';
91 $stuff->delete_option('oink');
92 } '... deleted the option okay';
95 $stuff->delete_option('xxy');
96 } '... deleted the option okay';
98 is($stuff->num_options, 1, '... we have 1 option(s)');
99 is_deeply($stuff->options, { foo => 'bar' }, '... got more options now');
101 $stuff->clear_options;
103 is_deeply($stuff->options, { }, "... cleared options" );
106 $stuff->set_quantity(4);
107 } '... options added okay with defaults';
109 is_deeply($stuff->options, {quantity => 4}, '... returns what we expect');
112 Stuff->new(options => { foo => 'BAR' });
113 } '... good constructor params';
118 $stuff->set_option(bar => {});
119 } '... could not add a hash ref where an string is expected';
122 Stuff->new(options => { foo => [] });
123 } '... bad constructor params';
127 my $options = $stuff->meta->get_attribute('options');
128 isa_ok($options, 'MooseX::AttributeHelpers::Collection::Hash');
130 is_deeply($options->provides, {
131 'set' => 'set_option',
132 'get' => 'get_option',
133 'empty' => 'has_options',
134 'count' => 'num_options',
135 'clear' => 'clear_options',
136 'delete' => 'delete_option',
137 'exists' => 'has_option',
138 }, '... got the right provies mapping');
140 is($options->type_constraint->type_parameter, 'Str', '... got the right container type');