6 use Test::More tests => 35;
8 use Test::Moose 'does_ok';
11 use_ok('Moose::AttributeHelpers');
17 use Moose::AttributeHelpers;
20 traits => [qw/Collection::Hash/],
22 isa => 'HashRef[Str]',
23 default => sub { {} },
25 'set' => 'set_option',
26 'get' => 'get_option',
27 'empty' => 'has_options',
28 'count' => 'num_options',
29 'clear' => 'clear_options',
30 'delete' => 'delete_option',
35 my $stuff = Stuff->new();
36 isa_ok($stuff, 'Stuff');
38 can_ok($stuff, $_) for qw[
47 ok(!$stuff->has_options, '... we have no options');
48 is($stuff->num_options, 0, '... we have no options');
50 is_deeply($stuff->options, {}, '... no options yet');
53 $stuff->set_option(foo => 'bar');
54 } '... set the option okay';
56 ok($stuff->has_options, '... we have options');
57 is($stuff->num_options, 1, '... we have 1 option(s)');
58 is_deeply($stuff->options, { foo => 'bar' }, '... got options now');
61 $stuff->set_option(bar => 'baz');
62 } '... set the option okay';
64 is($stuff->num_options, 2, '... we have 2 option(s)');
65 is_deeply($stuff->options, { foo => 'bar', bar => 'baz' }, '... got more options now');
67 is($stuff->get_option('foo'), 'bar', '... got the right option');
69 is_deeply([ $stuff->get_option(qw(foo bar)) ], [qw(bar baz)], "get multiple options at once");
72 $stuff->set_option(oink => "blah", xxy => "flop");
73 } '... set the option okay';
75 is($stuff->num_options, 4, "4 options");
76 is_deeply([ $stuff->get_option(qw(foo bar oink xxy)) ], [qw(bar baz blah flop)], "get multiple options at once");
79 $stuff->delete_option('bar');
80 } '... deleted the option okay';
83 $stuff->delete_option('oink');
84 } '... deleted the option okay';
87 $stuff->delete_option('xxy');
88 } '... deleted the option okay';
90 is($stuff->num_options, 1, '... we have 1 option(s)');
91 is_deeply($stuff->options, { foo => 'bar' }, '... got more options now');
93 $stuff->clear_options;
95 is_deeply($stuff->options, { }, "... cleared options" );
98 Stuff->new(options => { foo => 'BAR' });
99 } '... good constructor params';
104 $stuff->set_option(bar => {});
105 } '... could not add a hash ref where an string is expected';
108 Stuff->new(options => { foo => [] });
109 } '... bad constructor params';
113 my $options = $stuff->meta->get_attribute('options');
114 does_ok($options, 'Moose::AttributeHelpers::Trait::Collection::Hash');
116 is_deeply($options->provides, {
117 'set' => 'set_option',
118 'get' => 'get_option',
119 'empty' => 'has_options',
120 'count' => 'num_options',
121 'clear' => 'clear_options',
122 'delete' => 'delete_option',
123 }, '... got the right provies mapping');
125 is($options->type_constraint->type_parameter, 'Str', '... got the right container type');