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' );
101 ok( $obj->is_defined('foo'), '... foo is defined' );
103 ok( !$obj->has_no_options, '... we have options' );
104 is( $obj->num_options, 1, '... we have 1 option(s)' );
105 ok( $obj->has_option('foo'), '... we have a foo option' );
106 is_deeply( $obj->options, { foo => 'bar' }, '... got options now' );
109 $obj->set_option( bar => 'baz' );
110 }, undef, '... set the option okay' );
112 is( $obj->num_options, 2, '... we have 2 option(s)' );
114 $obj->options, { foo => 'bar', bar => 'baz' },
115 '... got more options now'
118 is( $obj->get_option('foo'), 'bar', '... got the right option' );
121 [ $obj->get_option(qw(foo bar)) ], [qw(bar baz)],
122 "get multiple options at once"
126 scalar( $obj->get_option(qw( foo bar)) ), "baz",
127 '... got last option in scalar context'
131 $obj->set_option( oink => "blah", xxy => "flop" );
132 }, undef, '... set the option okay' );
134 is( $obj->num_options, 4, "4 options" );
136 [ $obj->get_option(qw(foo bar oink xxy)) ],
137 [qw(bar baz blah flop)], "get multiple options at once"
141 is( scalar $obj->delete_option('bar'), 'baz',
142 'delete returns deleted value' );
143 }, undef, '... deleted the option okay' );
147 [ $obj->delete_option( 'oink', 'xxy' ) ],
149 'delete returns all deleted values in list context'
151 }, undef, '... deleted multiple option okay' );
153 is( $obj->num_options, 1, '... we have 1 option(s)' );
155 $obj->options, { foo => 'bar' },
156 '... got more options now'
161 is_deeply( $obj->options, {}, "... cleared options" );
165 }, undef, '... options added okay with defaults' );
167 is( $obj->quantity, 4, 'reader part of curried accessor works' );
170 $obj->option_accessor('quantity'), 4,
175 $obj->options, { quantity => 4 },
176 '... returns what we expect'
179 $obj->option_accessor( size => 42 );
183 $obj->option_accessor;
185 qr/Cannot call accessor without at least 1 argument/,
186 'error when calling accessor with no arguments'
190 $obj->options, { quantity => 4, size => 42 },
195 $class->new( options => { foo => 'BAR' } );
196 }, undef, '... good constructor params' );
199 $obj->set_option( bar => {} );
200 }, undef, '... could not add a hash ref where an string is expected' );
203 $class->new( options => { foo => [] } );
204 }, undef, '... bad constructor params' );
209 [ $obj->set_option( oink => "blah", xxy => "flop" ) ],
211 'set returns newly set values in order of keys provided'
217 'keys returns expected keys'
221 [ sort $obj->values ],
223 'values returns expected values'
226 my @key_value = sort { $a->[0] cmp $b->[0] } $obj->key_value;
230 sort { $a->[0] cmp $b->[0] }[ 'xxy', 'flop' ],
233 '... got the right key value pairs'
236 require Data::Dumper;
237 diag( Data::Dumper::Dumper( \@key_value ) );
240 my %options_elements = $obj->options_elements;
242 \%options_elements, {
246 '... got the right hash elements'
249 if ( $class->meta->get_attribute('options')->is_lazy ) {
250 my $obj = $class->new;
252 $obj->set_option( y => 2 );
255 $obj->options, { x => 1, y => 2 },
256 'set_option with lazy default'
259 $obj->_clear_options;
262 $obj->has_option('x'),
263 'key for x exists - lazy default'
266 $obj->_clear_options;
269 $obj->is_defined('x'),
270 'key for x is defined - lazy default'
273 $obj->_clear_options;
278 'kv returns lazy default'
281 $obj->_clear_options;
283 $obj->option_accessor( y => 2 );
288 'accessor triggers lazy default generator'