13 option_accessor => 'accessor',
14 quantity => [ accessor => 'quantity' ],
15 clear_options => 'clear',
16 num_options => 'count',
17 delete_option => 'delete',
18 is_defined => 'defined',
19 options_elements => 'elements',
20 has_option => 'exists',
22 has_no_options => 'is_empty',
32 my $class = Moose::Meta::Class->create(
34 superclasses => ['Moose::Object'],
37 $class->add_attribute(
41 isa => 'HashRef[Str]',
42 default => sub { {} },
44 clearer => '_clear_options',
49 return ( $class->name, \%handles );
54 run_tests(build_class);
55 run_tests( build_class( lazy => 1, default => sub { { x => 1 } } ) );
59 my ( $class, $handles ) = @_;
61 can_ok( $class, $_ ) for sort keys %{$handles};
64 my $obj = $class->new( options => {} );
66 ok( $obj->has_no_options, '... we have no options' );
67 is( $obj->num_options, 0, '... we have no options' );
69 is_deeply( $obj->options, {}, '... no options yet' );
70 ok( !$obj->has_option('foo'), '... we have no foo option' );
73 $obj->set_option( foo => 'bar' );
75 '... set the option okay';
77 ok( $obj->is_defined('foo'), '... foo is defined' );
79 ok( !$obj->has_no_options, '... we have options' );
80 is( $obj->num_options, 1, '... we have 1 option(s)' );
81 ok( $obj->has_option('foo'), '... we have a foo option' );
82 is_deeply( $obj->options, { foo => 'bar' }, '... got options now' );
85 $obj->set_option( bar => 'baz' );
87 '... set the option okay';
89 is( $obj->num_options, 2, '... we have 2 option(s)' );
91 $obj->options, { foo => 'bar', bar => 'baz' },
92 '... got more options now'
95 is( $obj->get_option('foo'), 'bar', '... got the right option' );
98 [ $obj->get_option(qw(foo bar)) ], [qw(bar baz)],
99 "get multiple options at once"
103 scalar( $obj->get_option(qw( foo bar)) ), "baz",
104 '... got last option in scalar context'
108 $obj->set_option( oink => "blah", xxy => "flop" );
110 '... set the option okay';
112 is( $obj->num_options, 4, "4 options" );
114 [ $obj->get_option(qw(foo bar oink xxy)) ],
115 [qw(bar baz blah flop)], "get multiple options at once"
119 $obj->delete_option('bar');
121 '... deleted the option okay';
124 $obj->delete_option( 'oink', 'xxy' );
126 '... deleted multiple option okay';
128 is( $obj->num_options, 1, '... we have 1 option(s)' );
130 $obj->options, { foo => 'bar' },
131 '... got more options now'
136 is_deeply( $obj->options, {}, "... cleared options" );
141 '... options added okay with defaults';
143 is( $obj->quantity, 4, 'reader part of curried accessor works' );
146 $obj->options, { quantity => 4 },
147 '... returns what we expect'
151 $class->new( options => { foo => 'BAR' } );
153 '... good constructor params';
156 $obj->set_option( bar => {} );
158 '... could not add a hash ref where an string is expected';
161 $class->new( options => { foo => [] } );
163 '... bad constructor params';
165 $obj->set_option( oink => "blah", xxy => "flop" );
166 my @key_value = sort { $a->[0] cmp $b->[0] } $obj->key_value;
170 sort { $a->[0] cmp $b->[0] }[ 'xxy', 'flop' ],
174 '... got the right key value pairs'
177 require Data::Dumper;
178 diag( Data::Dumper::Dumper( \@key_value ) );
181 my %options_elements = $obj->options_elements;
183 \%options_elements, {
188 '... got the right hash elements'
191 if ( $class->meta->get_attribute('options')->is_lazy ) {
192 my $obj = $class->new;
194 $obj->set_option( y => 2 );
197 $obj->options, { x => 1, y => 2 },
198 'set_option with lazy default'
201 $obj->_clear_options;
204 $obj->has_option('x'),
205 'key for x exists - lazy default'
208 $obj->_clear_options;
211 $obj->is_defined('x'),
212 'key for x is defined - lazy default'
215 $obj->_clear_options;
220 'kv returns lazy default'