6 use Test::More tests => 37;
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',
33 'set_with_defaults' =>
34 size => 'medium', quantity => 1
40 my $stuff = Stuff->new();
41 isa_ok($stuff, 'Stuff');
43 can_ok($stuff, $_) for qw[
52 ok(!$stuff->has_options, '... we have no options');
53 is($stuff->num_options, 0, '... we have no options');
55 is_deeply($stuff->options, {}, '... no options yet');
58 $stuff->set_option(foo => 'bar');
59 } '... set the option okay';
61 ok($stuff->has_options, '... we have options');
62 is($stuff->num_options, 1, '... we have 1 option(s)');
63 is_deeply($stuff->options, { foo => 'bar' }, '... got options now');
66 $stuff->set_option(bar => 'baz');
67 } '... set the option okay';
69 is($stuff->num_options, 2, '... we have 2 option(s)');
70 is_deeply($stuff->options, { foo => 'bar', bar => 'baz' }, '... got more options now');
72 is($stuff->get_option('foo'), 'bar', '... got the right option');
74 is_deeply([ $stuff->get_option(qw(foo bar)) ], [qw(bar baz)], "get multiple options at once");
77 $stuff->set_option(oink => "blah", xxy => "flop");
78 } '... set the option okay';
80 is($stuff->num_options, 4, "4 options");
81 is_deeply([ $stuff->get_option(qw(foo bar oink xxy)) ], [qw(bar baz blah flop)], "get multiple options at once");
84 $stuff->delete_option('bar');
85 } '... deleted the option okay';
88 $stuff->delete_option('oink');
89 } '... deleted the option okay';
92 $stuff->delete_option('xxy');
93 } '... deleted the option okay';
95 is($stuff->num_options, 1, '... we have 1 option(s)');
96 is_deeply($stuff->options, { foo => 'bar' }, '... got more options now');
98 $stuff->clear_options;
100 is_deeply($stuff->options, { }, "... cleared options" );
103 $stuff->set_with_defaults(foo => 'bar');
104 } '... options added okay with defaults';
106 is_deeply($stuff->options, {size => 'medium', quantity => 1, foo => 'bar'});
109 Stuff->new(options => { foo => 'BAR' });
110 } '... good constructor params';
115 $stuff->set_option(bar => {});
116 } '... could not add a hash ref where an string is expected';
119 Stuff->new(options => { foo => [] });
120 } '... bad constructor params';
124 my $options = $stuff->meta->get_attribute('options');
125 isa_ok($options, 'MooseX::AttributeHelpers::Collection::Hash');
127 is_deeply($options->provides, {
128 'set' => 'set_option',
129 'get' => 'get_option',
130 'empty' => 'has_options',
131 'count' => 'num_options',
132 'clear' => 'clear_options',
133 'delete' => 'delete_option',
134 }, '... got the right provies mapping');
136 is($options->type_constraint->type_parameter, 'Str', '... got the right container type');