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',
38 my $class = Moose::Meta::Class->create(
40 superclasses => ['Moose::Object'],
44 push @traits, 'NoInlineAttribute'
45 if delete $attr{no_inline};
47 $class->add_attribute(
51 isa => 'HashRef[Str]',
52 default => sub { {} },
54 clearer => '_clear_options',
59 return ( $class->name, \%handles );
64 run_tests(build_class);
65 run_tests( build_class( lazy => 1, default => sub { { x => 1 } } ) );
66 run_tests( build_class( trigger => sub { } ) );
67 run_tests( build_class( no_inline => 1 ) );
69 # Will force the inlining code to check the entire hashref when it is modified.
70 subtype 'MyHashRef', as 'HashRef[Str]', where { 1 };
72 run_tests( build_class( isa => 'MyHashRef' ) );
74 coerce 'MyHashRef', from 'HashRef', via { $_ };
76 run_tests( build_class( isa => 'MyHashRef', coerce => 1 ) );
80 my ( $class, $handles ) = @_;
82 can_ok( $class, $_ ) for sort keys %{$handles};
85 my $obj = $class->new( options => {} );
87 ok( $obj->has_no_options, '... we have no options' );
88 is( $obj->num_options, 0, '... we have no options' );
90 is_deeply( $obj->options, {}, '... no options yet' );
91 ok( !$obj->has_option('foo'), '... we have no foo option' );
95 $obj->set_option( foo => 'bar' ),
97 'set return single new value in scalar context'
99 }, undef, '... set the option okay' );
102 exception { $obj->set_option( foo => 'bar', 'baz' ) },
103 qr/You must pass an even number of arguments to set/,
104 'exception with odd number of arguments'
108 exception { $obj->set_option( undef, 'bar' ) },
109 qr/Hash keys passed to set must be defined/,
110 'exception when using undef as a key'
113 ok( $obj->is_defined('foo'), '... foo is defined' );
115 ok( !$obj->has_no_options, '... we have options' );
116 is( $obj->num_options, 1, '... we have 1 option(s)' );
117 ok( $obj->has_option('foo'), '... we have a foo option' );
118 is_deeply( $obj->options, { foo => 'bar' }, '... got options now' );
121 $obj->set_option( bar => 'baz' );
122 }, undef, '... set the option okay' );
124 is( $obj->num_options, 2, '... we have 2 option(s)' );
126 $obj->options, { foo => 'bar', bar => 'baz' },
127 '... got more options now'
130 is( $obj->get_option('foo'), 'bar', '... got the right option' );
133 [ $obj->get_option(qw(foo bar)) ], [qw(bar baz)],
134 "get multiple options at once"
138 scalar( $obj->get_option(qw( foo bar)) ), "baz",
139 '... got last option in scalar context'
143 $obj->set_option( oink => "blah", xxy => "flop" );
144 }, undef, '... set the option okay' );
146 is( $obj->num_options, 4, "4 options" );
148 [ $obj->get_option(qw(foo bar oink xxy)) ],
149 [qw(bar baz blah flop)], "get multiple options at once"
153 is( scalar $obj->delete_option('bar'), 'baz',
154 'delete returns deleted value' );
155 }, undef, '... deleted the option okay' );
159 [ $obj->delete_option( 'oink', 'xxy' ) ],
161 'delete returns all deleted values in list context'
163 }, undef, '... deleted multiple option okay' );
165 is( $obj->num_options, 1, '... we have 1 option(s)' );
167 $obj->options, { foo => 'bar' },
168 '... got more options now'
173 is_deeply( $obj->options, {}, "... cleared options" );
177 }, undef, '... options added okay with defaults' );
179 is( $obj->quantity, 4, 'reader part of curried accessor works' );
182 $obj->option_accessor('quantity'), 4,
187 $obj->options, { quantity => 4 },
188 '... returns what we expect'
191 $obj->option_accessor( size => 42 );
195 $obj->option_accessor;
197 qr/Cannot call accessor without at least 1 argument/,
198 'error when calling accessor with no arguments'
202 exception { $obj->option_accessor( undef, 'bar' ) },
203 qr/Hash keys passed to accessor must be defined/,
204 'exception when using undef as a key'
208 $obj->options, { quantity => 4, size => 42 },
213 $class->new( options => { foo => 'BAR' } );
214 }, undef, '... good constructor params' );
217 $obj->set_option( bar => {} );
218 }, undef, '... could not add a hash ref where an string is expected' );
221 $class->new( options => { foo => [] } );
222 }, undef, '... bad constructor params' );
227 [ $obj->set_option( oink => "blah", xxy => "flop" ) ],
229 'set returns newly set values in order of keys provided'
235 'keys returns expected keys'
239 [ sort $obj->values ],
241 'values returns expected values'
244 my @key_value = sort { $a->[0] cmp $b->[0] } $obj->key_value;
248 sort { $a->[0] cmp $b->[0] }[ 'xxy', 'flop' ],
251 '... got the right key value pairs'
254 require Data::Dumper;
255 diag( Data::Dumper::Dumper( \@key_value ) );
258 my %options_elements = $obj->options_elements;
260 \%options_elements, {
264 '... got the right hash elements'
267 if ( $class->meta->get_attribute('options')->is_lazy ) {
268 my $obj = $class->new;
270 $obj->set_option( y => 2 );
273 $obj->options, { x => 1, y => 2 },
274 'set_option with lazy default'
277 $obj->_clear_options;
280 $obj->has_option('x'),
281 'key for x exists - lazy default'
284 $obj->_clear_options;
287 $obj->is_defined('x'),
288 'key for x is defined - lazy default'
291 $obj->_clear_options;
296 'kv returns lazy default'
299 $obj->_clear_options;
301 $obj->option_accessor( y => 2 );
306 'accessor triggers lazy default generator'
314 my ( $class, $handles ) = build_class( isa => 'HashRef' );
315 my $obj = $class->new;
318 exception { $obj->option_accessor( 'foo', undef ) },
320 'can use accessor to set value to undef'
323 exception { $obj->quantity(undef) },
325 'can use accessor to set value to undef'