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_quantity => ['quantity']
39 my $stuff = Stuff->new();
40 isa_ok($stuff, 'Stuff');
42 can_ok($stuff, $_) for qw[
51 ok(!$stuff->has_options, '... we have no options');
52 is($stuff->num_options, 0, '... we have no options');
54 is_deeply($stuff->options, {}, '... no options yet');
57 $stuff->set_option(foo => 'bar');
58 } '... set the option okay';
60 ok($stuff->has_options, '... we have options');
61 is($stuff->num_options, 1, '... we have 1 option(s)');
62 is_deeply($stuff->options, { foo => 'bar' }, '... got options now');
65 $stuff->set_option(bar => 'baz');
66 } '... set the option okay';
68 is($stuff->num_options, 2, '... we have 2 option(s)');
69 is_deeply($stuff->options, { foo => 'bar', bar => 'baz' }, '... got more options now');
71 is($stuff->get_option('foo'), 'bar', '... got the right option');
73 is_deeply([ $stuff->get_option(qw(foo bar)) ], [qw(bar baz)], "get multiple options at once");
76 $stuff->set_option(oink => "blah", xxy => "flop");
77 } '... set the option okay';
79 is($stuff->num_options, 4, "4 options");
80 is_deeply([ $stuff->get_option(qw(foo bar oink xxy)) ], [qw(bar baz blah flop)], "get multiple options at once");
83 $stuff->delete_option('bar');
84 } '... deleted the option okay';
87 $stuff->delete_option('oink');
88 } '... deleted the option okay';
91 $stuff->delete_option('xxy');
92 } '... deleted the option okay';
94 is($stuff->num_options, 1, '... we have 1 option(s)');
95 is_deeply($stuff->options, { foo => 'bar' }, '... got more options now');
97 $stuff->clear_options;
99 is_deeply($stuff->options, { }, "... cleared options" );
102 $stuff->set_quantity(4);
103 } '... options added okay with defaults';
105 is_deeply($stuff->options, {quantity => 4});
108 Stuff->new(options => { foo => 'BAR' });
109 } '... good constructor params';
114 $stuff->set_option(bar => {});
115 } '... could not add a hash ref where an string is expected';
118 Stuff->new(options => { foo => [] });
119 } '... bad constructor params';
123 my $options = $stuff->meta->get_attribute('options');
124 isa_ok($options, 'MooseX::AttributeHelpers::Collection::Hash');
126 is_deeply($options->provides, {
127 'set' => 'set_option',
128 'get' => 'get_option',
129 'empty' => 'has_options',
130 'count' => 'num_options',
131 'clear' => 'clear_options',
132 'delete' => 'delete_option',
133 }, '... got the right provies mapping');
135 is($options->type_constraint->type_parameter, 'Str', '... got the right container type');