6 use Test::More tests => 48;
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',
32 'accessor' => 'option_accessor',
36 quantity => ['quantity'],
42 my $stuff = Stuff->new();
43 isa_ok($stuff, 'Stuff');
45 can_ok($stuff, $_) for qw[
58 ok(!$stuff->has_options, '... we have no options');
59 is($stuff->num_options, 0, '... we have no options');
61 is_deeply($stuff->options, {}, '... no options yet');
62 ok(!$stuff->has_option('foo'), '... we have no foo option');
65 $stuff->set_option(foo => 'bar');
66 } '... set the option okay';
68 ok($stuff->is_defined('foo'), '... foo is defined');
70 ok($stuff->has_options, '... we have options');
71 is($stuff->num_options, 1, '... we have 1 option(s)');
72 ok($stuff->has_option('foo'), '... we have a foo option');
73 is_deeply($stuff->options, { foo => 'bar' }, '... got options now');
76 $stuff->set_option(bar => 'baz');
77 } '... set the option okay';
79 is($stuff->num_options, 2, '... we have 2 option(s)');
80 is_deeply($stuff->options, { foo => 'bar', bar => 'baz' }, '... got more options now');
82 is($stuff->get_option('foo'), 'bar', '... got the right option');
84 is_deeply([ $stuff->get_option(qw(foo bar)) ], [qw(bar baz)], "get multiple options at once");
87 $stuff->set_option(oink => "blah", xxy => "flop");
88 } '... set the option okay';
90 is($stuff->num_options, 4, "4 options");
91 is_deeply([ $stuff->get_option(qw(foo bar oink xxy)) ], [qw(bar baz blah flop)], "get multiple options at once");
94 $stuff->delete_option('bar');
95 } '... deleted the option okay';
98 $stuff->delete_option('oink');
99 } '... deleted the option okay';
102 $stuff->delete_option('xxy');
103 } '... deleted the option okay';
105 is($stuff->num_options, 1, '... we have 1 option(s)');
106 is_deeply($stuff->options, { foo => 'bar' }, '... got more options now');
108 $stuff->clear_options;
110 is_deeply($stuff->options, { }, "... cleared options" );
114 } '... options added okay with defaults';
116 is($stuff->quantity, 4, 'reader part of curried accessor works');
118 is_deeply($stuff->options, {quantity => 4}, '... returns what we expect');
121 Stuff->new(options => { foo => 'BAR' });
122 } '... good constructor params';
127 $stuff->set_option(bar => {});
128 } '... could not add a hash ref where an string is expected';
131 Stuff->new(options => { foo => [] });
132 } '... bad constructor params';
135 my $stuff = Stuff->new;
136 $stuff->option_accessor();
137 } '... accessor dies on 0 args';
140 my $stuff = Stuff->new;
141 $stuff->option_accessor(1 => 2, 3);
142 } '... accessor dies on 3 args';
145 my $stuff = Stuff->new;
146 $stuff->option_accessor(1 => 2, 3 => 4);
147 } '... accessor dies on 4 args';
151 my $options = $stuff->meta->get_attribute('options');
152 isa_ok($options, 'MooseX::AttributeHelpers::Collection::Hash');
154 is_deeply($options->provides, {
155 'set' => 'set_option',
156 'get' => 'get_option',
157 'empty' => 'has_options',
158 'count' => 'num_options',
159 'clear' => 'clear_options',
160 'delete' => 'delete_option',
161 'defined' => 'is_defined',
162 'exists' => 'has_option',
163 'accessor' => 'option_accessor',
164 }, '... got the right provides mapping');
166 is($options->type_constraint->type_parameter, 'Str', '... got the right container type');