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' );
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' );
78 '... set the option okay';
80 is( $stuff->num_options, 2, '... we have 2 option(s)' );
81 is_deeply( $stuff->options, { foo => 'bar', bar => 'baz' },
82 '... got more options now' );
84 is( $stuff->get_option('foo'), 'bar', '... got the right option' );
86 is_deeply( [ $stuff->get_option(qw(foo bar)) ], [qw(bar baz)],
87 "get multiple options at once" );
90 $stuff->set_option( oink => "blah", xxy => "flop" );
92 '... set the option okay';
94 is( $stuff->num_options, 4, "4 options" );
95 is_deeply( [ $stuff->get_option(qw(foo bar oink xxy)) ],
96 [qw(bar baz blah flop)], "get multiple options at once" );
99 $stuff->delete_option('bar');
101 '... deleted the option okay';
104 $stuff->delete_option('oink');
106 '... deleted the option okay';
109 $stuff->delete_option('xxy');
111 '... deleted the option okay';
113 is( $stuff->num_options, 1, '... we have 1 option(s)' );
114 is_deeply( $stuff->options, { foo => 'bar' }, '... got more options now' );
116 $stuff->clear_options;
118 is_deeply( $stuff->options, {}, "... cleared options" );
123 '... options added okay with defaults';
125 is( $stuff->quantity, 4, 'reader part of curried accessor works' );
127 is_deeply( $stuff->options, { quantity => 4 }, '... returns what we expect' );
130 Stuff->new( options => { foo => 'BAR' } );
132 '... good constructor params';
137 $stuff->set_option( bar => {} );
139 '... could not add a hash ref where an string is expected';
142 Stuff->new( options => { foo => [] } );
144 '... bad constructor params';
148 my $options = $stuff->meta->get_attribute('options');
149 does_ok( $options, 'Moose::AttributeHelpers::Trait::Collection::Hash' );
154 'set_option' => 'set',
155 'get_option' => 'get',
156 'has_options' => 'empty',
157 'num_options' => 'count',
158 'clear_options' => 'clear',
159 'delete_option' => 'delete',
160 'has_option' => 'exists',
161 'is_defined' => 'defined',
162 'option_accessor' => 'accessor',
164 'options_elements' => 'elements',
165 'quantity' => [ accessor => ['quantity'] ],
167 '... got the right handles mapping'
170 is( $options->type_constraint->type_parameter, 'Str',
171 '... got the right container type' );
173 $stuff->set_option( oink => "blah", xxy => "flop" );
174 my @key_value = $stuff->key_value;
177 [ [ 'xxy', 'flop' ], [ 'quantity', 4 ], [ 'oink', 'blah' ] ],
178 '... got the right key value pairs'
181 my %options_elements = $stuff->options_elements;
189 '... got the right hash elements'