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',
37 my $class = Moose::Meta::Class->create(
39 superclasses => ['Moose::Object'],
43 push @traits, 'NoInlineAttribute'
44 if delete $attr{no_inline};
46 $class->add_attribute(
50 isa => 'HashRef[Str]',
51 default => sub { {} },
53 clearer => '_clear_options',
58 return ( $class->name, \%handles );
63 run_tests(build_class);
64 run_tests( build_class( lazy => 1, default => sub { { x => 1 } } ) );
65 run_tests( build_class( trigger => sub { } ) );
66 run_tests( build_class( no_inline => 1 ) );
68 # Will force the inlining code to check the entire hashref when it is modified.
69 subtype 'MyHashRef', as 'HashRef[Str]', where { 1 };
71 run_tests( build_class( isa => 'MyHashRef' ) );
73 coerce 'MyHashRef', from 'HashRef', via { $_ };
75 run_tests( build_class( isa => 'MyHashRef', coerce => 1 ) );
79 my ( $class, $handles ) = @_;
81 can_ok( $class, $_ ) for sort keys %{$handles};
84 my $obj = $class->new( options => {} );
86 ok( $obj->has_no_options, '... we have no options' );
87 is( $obj->num_options, 0, '... we have no options' );
89 is_deeply( $obj->options, {}, '... no options yet' );
90 ok( !$obj->has_option('foo'), '... we have no foo option' );
94 $obj->set_option( foo => 'bar' ),
96 'set return single new value in scalar context'
98 }, undef, '... set the option okay' );
100 ok( $obj->is_defined('foo'), '... foo is defined' );
102 ok( !$obj->has_no_options, '... we have options' );
103 is( $obj->num_options, 1, '... we have 1 option(s)' );
104 ok( $obj->has_option('foo'), '... we have a foo option' );
105 is_deeply( $obj->options, { foo => 'bar' }, '... got options now' );
108 $obj->set_option( bar => 'baz' );
109 }, undef, '... set the option okay' );
111 is( $obj->num_options, 2, '... we have 2 option(s)' );
113 $obj->options, { foo => 'bar', bar => 'baz' },
114 '... got more options now'
117 is( $obj->get_option('foo'), 'bar', '... got the right option' );
120 [ $obj->get_option(qw(foo bar)) ], [qw(bar baz)],
121 "get multiple options at once"
125 scalar( $obj->get_option(qw( foo bar)) ), "baz",
126 '... got last option in scalar context'
130 $obj->set_option( oink => "blah", xxy => "flop" );
131 }, undef, '... set the option okay' );
133 is( $obj->num_options, 4, "4 options" );
135 [ $obj->get_option(qw(foo bar oink xxy)) ],
136 [qw(bar baz blah flop)], "get multiple options at once"
140 is( scalar $obj->delete_option('bar'), 'baz',
141 'delete returns deleted value' );
142 }, undef, '... deleted the option okay' );
146 [ $obj->delete_option( 'oink', 'xxy' ) ],
148 'delete returns all deleted values in list context'
150 }, undef, '... deleted multiple option okay' );
152 is( $obj->num_options, 1, '... we have 1 option(s)' );
154 $obj->options, { foo => 'bar' },
155 '... got more options now'
160 is_deeply( $obj->options, {}, "... cleared options" );
164 }, undef, '... options added okay with defaults' );
166 is( $obj->quantity, 4, 'reader part of curried accessor works' );
169 $obj->options, { quantity => 4 },
170 '... returns what we expect'
174 $class->new( options => { foo => 'BAR' } );
175 }, undef, '... good constructor params' );
178 $obj->set_option( bar => {} );
179 }, undef, '... could not add a hash ref where an string is expected' );
182 $class->new( options => { foo => [] } );
183 }, undef, '... bad constructor params' );
186 [ $obj->set_option( oink => "blah", xxy => "flop" ) ],
188 'set returns newly set values in order of keys provided'
191 my @key_value = sort { $a->[0] cmp $b->[0] } $obj->key_value;
195 sort { $a->[0] cmp $b->[0] }[ 'xxy', 'flop' ],
199 '... got the right key value pairs'
202 require Data::Dumper;
203 diag( Data::Dumper::Dumper( \@key_value ) );
206 my %options_elements = $obj->options_elements;
208 \%options_elements, {
213 '... got the right hash elements'
216 if ( $class->meta->get_attribute('options')->is_lazy ) {
217 my $obj = $class->new;
219 $obj->set_option( y => 2 );
222 $obj->options, { x => 1, y => 2 },
223 'set_option with lazy default'
226 $obj->_clear_options;
229 $obj->has_option('x'),
230 'key for x exists - lazy default'
233 $obj->_clear_options;
236 $obj->is_defined('x'),
237 'key for x is defined - lazy default'
240 $obj->_clear_options;
245 'kv returns lazy default'
249 $obj->_clear_options;
251 $obj->option_accessor( y => 2 );
256 'accessor triggers lazy default generator'