6 use Test::More tests => 47;
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_option' => 'set',
26 'get_option' => 'get',
27 'has_options' => 'empty',
28 'num_options' => 'count',
29 'clear_options' => 'clear',
30 'delete_option' => 'delete',
31 'has_option' => 'exists',
32 'is_defined' => 'defined',
33 'option_accessor' => 'accessor',
35 'options_elements' => 'elements',
36 'quantity' => [ accessor => ['quantity'] ],
41 my $stuff = Stuff->new();
42 isa_ok($stuff, 'Stuff');
44 can_ok($stuff, $_) for qw[
57 ok(!$stuff->has_options, '... we have no options');
58 is($stuff->num_options, 0, '... we have no options');
60 is_deeply($stuff->options, {}, '... no options yet');
61 ok(!$stuff->has_option('foo'), '... we have no foo option');
64 $stuff->set_option(foo => 'bar');
65 } '... set the option okay';
67 ok($stuff->is_defined('foo'), '... foo is defined');
69 ok($stuff->has_options, '... we have options');
70 is($stuff->num_options, 1, '... we have 1 option(s)');
71 ok($stuff->has_option('foo'), '... we have a foo option');
72 is_deeply($stuff->options, { foo => 'bar' }, '... got options now');
75 $stuff->set_option(bar => 'baz');
76 } '... set the option okay';
78 is($stuff->num_options, 2, '... we have 2 option(s)');
79 is_deeply($stuff->options, { foo => 'bar', bar => 'baz' }, '... got more options now');
81 is($stuff->get_option('foo'), 'bar', '... got the right option');
83 is_deeply([ $stuff->get_option(qw(foo bar)) ], [qw(bar baz)], "get multiple options at once");
86 $stuff->set_option(oink => "blah", xxy => "flop");
87 } '... set the option okay';
89 is($stuff->num_options, 4, "4 options");
90 is_deeply([ $stuff->get_option(qw(foo bar oink xxy)) ], [qw(bar baz blah flop)], "get multiple options at once");
93 $stuff->delete_option('bar');
94 } '... deleted the option okay';
97 $stuff->delete_option('oink');
98 } '... deleted the option okay';
101 $stuff->delete_option('xxy');
102 } '... deleted the option okay';
104 is($stuff->num_options, 1, '... we have 1 option(s)');
105 is_deeply($stuff->options, { foo => 'bar' }, '... got more options now');
107 $stuff->clear_options;
109 is_deeply($stuff->options, { }, "... cleared options" );
113 } '... options added okay with defaults';
115 is($stuff->quantity, 4, 'reader part of curried accessor works');
117 is_deeply($stuff->options, {quantity => 4}, '... returns what we expect');
120 Stuff->new(options => { foo => 'BAR' });
121 } '... good constructor params';
126 $stuff->set_option(bar => {});
127 } '... could not add a hash ref where an string is expected';
130 Stuff->new(options => { foo => [] });
131 } '... bad constructor params';
135 my $options = $stuff->meta->get_attribute('options');
136 does_ok($options, 'Moose::AttributeHelpers::Trait::Collection::Hash');
138 is_deeply($options->handles, {
139 'add_options' => 'push',
140 'remove_last_option' => 'pop',
141 'remove_first_option' => 'shift',
142 'insert_options' => 'unshift',
143 'get_option_at' => 'get',
144 'set_option_at' => 'set',
145 'num_options' => 'count',
146 'has_options' => 'empty',
147 'clear_options' => 'clear',
148 'splice_options' => 'splice',
149 'sort_options_in_place' => 'sort_in_place',
150 'option_accessor' => 'accessor',
151 }, '... got the right handles mapping');
153 is($options->type_constraint->type_parameter, 'Str', '... got the right container type');
155 $stuff->set_option( oink => "blah", xxy => "flop" );
156 my @key_value = $stuff->key_value;
159 [ [ 'xxy', 'flop' ], [ 'quantity', 4 ], [ 'oink', 'blah' ] ],
160 '... got the right key value pairs'
163 my %options_elements = $stuff->options_elements;
171 '... got the right hash elements'