6 use Test::More tests => 35;
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',
34 my $stuff = Stuff->new();
35 isa_ok($stuff, 'Stuff');
37 can_ok($stuff, $_) for qw[
46 ok(!$stuff->has_options, '... we have no options');
47 is($stuff->num_options, 0, '... we have no options');
49 is_deeply($stuff->options, {}, '... no options yet');
52 $stuff->set_option(foo => 'bar');
53 } '... set the option okay';
55 ok($stuff->has_options, '... we have options');
56 is($stuff->num_options, 1, '... we have 1 option(s)');
57 is_deeply($stuff->options, { foo => 'bar' }, '... got options now');
60 $stuff->set_option(bar => 'baz');
61 } '... set the option okay';
63 is($stuff->num_options, 2, '... we have 2 option(s)');
64 is_deeply($stuff->options, { foo => 'bar', bar => 'baz' }, '... got more options now');
66 is($stuff->get_option('foo'), 'bar', '... got the right option');
68 is_deeply([ $stuff->get_option(qw(foo bar)) ], [qw(bar baz)], "get multiple options at once");
71 $stuff->set_option(oink => "blah", xxy => "flop");
72 } '... set the option okay';
74 is($stuff->num_options, 4, "4 options");
75 is_deeply([ $stuff->get_option(qw(foo bar oink xxy)) ], [qw(bar baz blah flop)], "get multiple options at once");
78 $stuff->delete_option('bar');
79 } '... deleted the option okay';
82 $stuff->delete_option('oink');
83 } '... deleted the option okay';
86 $stuff->delete_option('xxy');
87 } '... deleted the option okay';
89 is($stuff->num_options, 1, '... we have 1 option(s)');
90 is_deeply($stuff->options, { foo => 'bar' }, '... got more options now');
92 $stuff->clear_options;
94 is_deeply($stuff->options, { }, "... cleared options" );
97 Stuff->new(options => { foo => 'BAR' });
98 } '... good constructor params';
103 $stuff->set_option(bar => {});
104 } '... could not add a hash ref where an string is expected';
107 Stuff->new(options => { foo => [] });
108 } '... bad constructor params';
112 my $options = $stuff->meta->get_attribute('options');
113 isa_ok($options, 'MooseX::AttributeHelpers::Collection::Hash');
115 is_deeply($options->provides, {
116 'set' => 'set_option',
117 'get' => 'get_option',
118 'empty' => 'has_options',
119 'count' => 'num_options',
120 'clear' => 'clear_options',
121 'delete' => 'delete_option',
122 }, '... got the right provies mapping');
124 is($options->type_constraint->type_parameter, 'Str', '... got the right container type');