9 use Moose::Util::TypeConstraints;
10 use NoInlineAttribute;
17 option_accessor => 'accessor',
18 quantity => [ accessor => 'quantity' ],
19 clear_options => 'clear',
20 num_options => 'count',
21 delete_option => 'delete',
22 is_defined => 'defined',
23 options_elements => 'elements',
24 has_option => 'exists',
26 has_no_options => 'is_empty',
36 my $class = Moose::Meta::Class->create(
38 superclasses => ['Moose::Object'],
42 push @traits, 'NoInlineAttribute'
43 if delete $attr{no_inline};
45 $class->add_attribute(
49 isa => 'HashRef[Str]',
50 default => sub { {} },
52 clearer => '_clear_options',
57 return ( $class->name, \%handles );
62 run_tests(build_class);
63 run_tests( build_class( lazy => 1, default => sub { { x => 1 } } ) );
64 run_tests( build_class( trigger => sub { } ) );
65 run_tests( build_class( no_inline => 1 ) );
67 # Will force the inlining code to check the entire hashref when it is modified.
68 subtype 'MyHashRef', as 'HashRef[Str]', where { 1 };
70 run_tests( build_class( isa => 'MyHashRef' ) );
72 coerce 'MyHashRef', from 'HashRef', via { $_ };
74 run_tests( build_class( isa => 'MyHashRef', coerce => 1 ) );
78 my ( $class, $handles ) = @_;
80 can_ok( $class, $_ ) for sort keys %{$handles};
83 my $obj = $class->new( options => {} );
85 ok( $obj->has_no_options, '... we have no options' );
86 is( $obj->num_options, 0, '... we have no options' );
88 is_deeply( $obj->options, {}, '... no options yet' );
89 ok( !$obj->has_option('foo'), '... we have no foo option' );
92 $obj->set_option( foo => 'bar' );
94 '... set the option okay';
96 ok( $obj->is_defined('foo'), '... foo is defined' );
98 ok( !$obj->has_no_options, '... we have options' );
99 is( $obj->num_options, 1, '... we have 1 option(s)' );
100 ok( $obj->has_option('foo'), '... we have a foo option' );
101 is_deeply( $obj->options, { foo => 'bar' }, '... got options now' );
104 $obj->set_option( bar => 'baz' );
106 '... set the option okay';
108 is( $obj->num_options, 2, '... we have 2 option(s)' );
110 $obj->options, { foo => 'bar', bar => 'baz' },
111 '... got more options now'
114 is( $obj->get_option('foo'), 'bar', '... got the right option' );
117 [ $obj->get_option(qw(foo bar)) ], [qw(bar baz)],
118 "get multiple options at once"
122 scalar( $obj->get_option(qw( foo bar)) ), "baz",
123 '... got last option in scalar context'
127 $obj->set_option( oink => "blah", xxy => "flop" );
129 '... set the option okay';
131 is( $obj->num_options, 4, "4 options" );
133 [ $obj->get_option(qw(foo bar oink xxy)) ],
134 [qw(bar baz blah flop)], "get multiple options at once"
138 $obj->delete_option('bar');
140 '... deleted the option okay';
143 $obj->delete_option( 'oink', 'xxy' );
145 '... deleted multiple option okay';
147 is( $obj->num_options, 1, '... we have 1 option(s)' );
149 $obj->options, { foo => 'bar' },
150 '... got more options now'
155 is_deeply( $obj->options, {}, "... cleared options" );
160 '... options added okay with defaults';
162 is( $obj->quantity, 4, 'reader part of curried accessor works' );
165 $obj->options, { quantity => 4 },
166 '... returns what we expect'
170 $class->new( options => { foo => 'BAR' } );
172 '... good constructor params';
175 $obj->set_option( bar => {} );
177 '... could not add a hash ref where an string is expected';
180 $class->new( options => { foo => [] } );
182 '... bad constructor params';
184 $obj->set_option( oink => "blah", xxy => "flop" );
185 my @key_value = sort { $a->[0] cmp $b->[0] } $obj->key_value;
189 sort { $a->[0] cmp $b->[0] }[ 'xxy', 'flop' ],
193 '... got the right key value pairs'
196 require Data::Dumper;
197 diag( Data::Dumper::Dumper( \@key_value ) );
200 my %options_elements = $obj->options_elements;
202 \%options_elements, {
207 '... got the right hash elements'
210 if ( $class->meta->get_attribute('options')->is_lazy ) {
211 my $obj = $class->new;
213 $obj->set_option( y => 2 );
216 $obj->options, { x => 1, y => 2 },
217 'set_option with lazy default'
220 $obj->_clear_options;
223 $obj->has_option('x'),
224 'key for x exists - lazy default'
227 $obj->_clear_options;
230 $obj->is_defined('x'),
231 'key for x is defined - lazy default'
234 $obj->_clear_options;
239 'kv returns lazy default'