6 use Test::More tests => 42;
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',
35 set_quantity => ['quantity']
41 my $stuff = Stuff->new();
42 isa_ok($stuff, 'Stuff');
44 can_ok($stuff, $_) for qw[
55 ok(!$stuff->has_options, '... we have no options');
56 is($stuff->num_options, 0, '... we have no options');
58 is_deeply($stuff->options, {}, '... no options yet');
59 ok(!$stuff->has_option('foo'), '... we have no foo option');
62 $stuff->set_option(foo => 'bar');
63 } '... set the option okay';
65 ok($stuff->is_defined('foo'), '... foo is defined');
67 ok($stuff->has_options, '... we have options');
68 is($stuff->num_options, 1, '... we have 1 option(s)');
69 ok($stuff->has_option('foo'), '... we have a foo option');
70 is_deeply($stuff->options, { foo => 'bar' }, '... got options now');
73 $stuff->set_option(bar => 'baz');
74 } '... set the option okay';
76 is($stuff->num_options, 2, '... we have 2 option(s)');
77 is_deeply($stuff->options, { foo => 'bar', bar => 'baz' }, '... got more options now');
79 is($stuff->get_option('foo'), 'bar', '... got the right option');
81 is_deeply([ $stuff->get_option(qw(foo bar)) ], [qw(bar baz)], "get multiple options at once");
84 $stuff->set_option(oink => "blah", xxy => "flop");
85 } '... set the option okay';
87 is($stuff->num_options, 4, "4 options");
88 is_deeply([ $stuff->get_option(qw(foo bar oink xxy)) ], [qw(bar baz blah flop)], "get multiple options at once");
91 $stuff->delete_option('bar');
92 } '... deleted the option okay';
95 $stuff->delete_option('oink');
96 } '... deleted the option okay';
99 $stuff->delete_option('xxy');
100 } '... deleted the option okay';
102 is($stuff->num_options, 1, '... we have 1 option(s)');
103 is_deeply($stuff->options, { foo => 'bar' }, '... got more options now');
105 $stuff->clear_options;
107 is_deeply($stuff->options, { }, "... cleared options" );
110 $stuff->set_quantity(4);
111 } '... options added okay with defaults';
113 is_deeply($stuff->options, {quantity => 4}, '... returns what we expect');
116 Stuff->new(options => { foo => 'BAR' });
117 } '... good constructor params';
122 $stuff->set_option(bar => {});
123 } '... could not add a hash ref where an string is expected';
126 Stuff->new(options => { foo => [] });
127 } '... bad constructor params';
131 my $options = $stuff->meta->get_attribute('options');
132 isa_ok($options, 'MooseX::AttributeHelpers::Collection::Hash');
134 is_deeply($options->provides, {
135 'set' => 'set_option',
136 'get' => 'get_option',
137 'empty' => 'has_options',
138 'count' => 'num_options',
139 'clear' => 'clear_options',
140 'delete' => 'delete_option',
141 'defined' => 'is_defined',
142 'exists' => 'has_option',
143 }, '... got the right provies mapping');
145 is($options->type_constraint->type_parameter, 'Str', '... got the right container type');