6 use Test::More tests => 39;
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');
59 $stuff->set_option(foo => 'bar');
60 } '... set the option okay';
62 ok($stuff->has_options, '... we have options');
63 is($stuff->num_options, 1, '... we have 1 option(s)');
64 ok($stuff->has_option('foo'), '... we have a foo option');
65 is_deeply($stuff->options, { foo => 'bar' }, '... got options now');
68 $stuff->set_option(bar => 'baz');
69 } '... set the option okay';
71 is($stuff->num_options, 2, '... we have 2 option(s)');
72 is_deeply($stuff->options, { foo => 'bar', bar => 'baz' }, '... got more options now');
74 is($stuff->get_option('foo'), 'bar', '... got the right option');
76 is_deeply([ $stuff->get_option(qw(foo bar)) ], [qw(bar baz)], "get multiple options at once");
79 $stuff->set_option(oink => "blah", xxy => "flop");
80 } '... set the option okay';
82 is($stuff->num_options, 4, "4 options");
83 is_deeply([ $stuff->get_option(qw(foo bar oink xxy)) ], [qw(bar baz blah flop)], "get multiple options at once");
86 $stuff->delete_option('bar');
87 } '... deleted the option okay';
90 $stuff->delete_option('oink');
91 } '... deleted the option okay';
94 $stuff->delete_option('xxy');
95 } '... deleted the option okay';
97 is($stuff->num_options, 1, '... we have 1 option(s)');
98 is_deeply($stuff->options, { foo => 'bar' }, '... got more options now');
100 $stuff->clear_options;
102 is_deeply($stuff->options, { }, "... cleared options" );
105 $stuff->set_quantity(4);
106 } '... options added okay with defaults';
108 is_deeply($stuff->options, {quantity => 4}, '... returns what we expect');
111 Stuff->new(options => { foo => 'BAR' });
112 } '... good constructor params';
117 $stuff->set_option(bar => {});
118 } '... could not add a hash ref where an string is expected';
121 Stuff->new(options => { foo => [] });
122 } '... bad constructor params';
126 my $options = $stuff->meta->get_attribute('options');
127 isa_ok($options, 'MooseX::AttributeHelpers::Collection::Hash');
129 is_deeply($options->provides, {
130 'set' => 'set_option',
131 'get' => 'get_option',
132 'empty' => 'has_options',
133 'count' => 'num_options',
134 'clear' => 'clear_options',
135 'delete' => 'delete_option',
136 'exists' => 'has_option',
137 }, '... got the right provies mapping');
139 is($options->type_constraint->type_parameter, 'Str', '... got the right container type');