Fix error when no arguments are passed to Hash's accessor method
[gitmo/Moose.git] / t / 070_native_traits / 050_trait_hash.t
CommitLineData
e3c07b19 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
8b9641b8 6use lib 't/lib';
7
1c08fd75 8use Moose ();
6197a68c 9use Moose::Util::TypeConstraints;
8b9641b8 10use NoInlineAttribute;
b10dde3a 11use Test::Fatal;
1c08fd75 12use Test::More;
13use Test::Moose;
e3c07b19 14
e3c07b19 15{
1c08fd75 16 my %handles = (
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',
25 get_option => 'get',
26 has_no_options => 'is_empty',
865faf6f 27 keys => 'keys',
1399eab0 28 values => 'values',
1c08fd75 29 key_value => 'kv',
30 set_option => 'set',
e3c07b19 31 );
85592815 32
1c08fd75 33 my $name = 'Foo1';
34
35 sub build_class {
36 my %attr = @_;
37
38 my $class = Moose::Meta::Class->create(
39 $name++,
40 superclasses => ['Moose::Object'],
41 );
42
8b9641b8 43 my @traits = 'Hash';
44 push @traits, 'NoInlineAttribute'
45 if delete $attr{no_inline};
46
1c08fd75 47 $class->add_attribute(
48 options => (
8b9641b8 49 traits => \@traits,
2d779ce6 50 is => 'rw',
1c08fd75 51 isa => 'HashRef[Str]',
52 default => sub { {} },
53 handles => \%handles,
54 clearer => '_clear_options',
55 %attr,
56 ),
57 );
58
59 return ( $class->name, \%handles );
60 }
d50fc84a 61}
e3c07b19 62
1c08fd75 63{
64 run_tests(build_class);
65 run_tests( build_class( lazy => 1, default => sub { { x => 1 } } ) );
cf0da4e2 66 run_tests( build_class( trigger => sub { } ) );
8b9641b8 67 run_tests( build_class( no_inline => 1 ) );
6197a68c 68
69 # Will force the inlining code to check the entire hashref when it is modified.
70 subtype 'MyHashRef', as 'HashRef[Str]', where { 1 };
71
72 run_tests( build_class( isa => 'MyHashRef' ) );
73
74 coerce 'MyHashRef', from 'HashRef', via { $_ };
75
76 run_tests( build_class( isa => 'MyHashRef', coerce => 1 ) );
d50fc84a 77}
59de9de4 78
1c08fd75 79sub run_tests {
80 my ( $class, $handles ) = @_;
59de9de4 81
1c08fd75 82 can_ok( $class, $_ ) for sort keys %{$handles};
e3c07b19 83
1c08fd75 84 with_immutable {
85 my $obj = $class->new( options => {} );
e3c07b19 86
1c08fd75 87 ok( $obj->has_no_options, '... we have no options' );
88 is( $obj->num_options, 0, '... we have no options' );
89
90 is_deeply( $obj->options, {}, '... no options yet' );
91 ok( !$obj->has_option('foo'), '... we have no foo option' );
92
b10dde3a 93 is( exception {
7f5ec80d 94 is(
95 $obj->set_option( foo => 'bar' ),
96 'bar',
97 'set return single new value in scalar context'
98 );
b10dde3a 99 }, undef, '... set the option okay' );
1c08fd75 100
101 ok( $obj->is_defined('foo'), '... foo is defined' );
102
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' );
107
b10dde3a 108 is( exception {
1c08fd75 109 $obj->set_option( bar => 'baz' );
b10dde3a 110 }, undef, '... set the option okay' );
1c08fd75 111
112 is( $obj->num_options, 2, '... we have 2 option(s)' );
113 is_deeply(
114 $obj->options, { foo => 'bar', bar => 'baz' },
115 '... got more options now'
116 );
117
118 is( $obj->get_option('foo'), 'bar', '... got the right option' );
119
120 is_deeply(
121 [ $obj->get_option(qw(foo bar)) ], [qw(bar baz)],
122 "get multiple options at once"
123 );
124
125 is(
126 scalar( $obj->get_option(qw( foo bar)) ), "baz",
127 '... got last option in scalar context'
128 );
129
b10dde3a 130 is( exception {
1c08fd75 131 $obj->set_option( oink => "blah", xxy => "flop" );
b10dde3a 132 }, undef, '... set the option okay' );
1c08fd75 133
134 is( $obj->num_options, 4, "4 options" );
135 is_deeply(
136 [ $obj->get_option(qw(foo bar oink xxy)) ],
137 [qw(bar baz blah flop)], "get multiple options at once"
138 );
139
b10dde3a 140 is( exception {
7f5ec80d 141 is( scalar $obj->delete_option('bar'), 'baz',
142 'delete returns deleted value' );
b10dde3a 143 }, undef, '... deleted the option okay' );
1c08fd75 144
b10dde3a 145 is( exception {
7f5ec80d 146 is_deeply(
147 [ $obj->delete_option( 'oink', 'xxy' ) ],
148 [ 'blah', 'flop' ],
149 'delete returns all deleted values in list context'
150 );
b10dde3a 151 }, undef, '... deleted multiple option okay' );
1c08fd75 152
153 is( $obj->num_options, 1, '... we have 1 option(s)' );
154 is_deeply(
155 $obj->options, { foo => 'bar' },
156 '... got more options now'
157 );
158
159 $obj->clear_options;
160
161 is_deeply( $obj->options, {}, "... cleared options" );
162
b10dde3a 163 is( exception {
1c08fd75 164 $obj->quantity(4);
b10dde3a 165 }, undef, '... options added okay with defaults' );
1c08fd75 166
167 is( $obj->quantity, 4, 'reader part of curried accessor works' );
168
2d779ce6 169 is(
170 $obj->option_accessor('quantity'), 4,
171 'accessor as reader'
172 );
173
1c08fd75 174 is_deeply(
175 $obj->options, { quantity => 4 },
176 '... returns what we expect'
177 );
178
2d779ce6 179 $obj->option_accessor( size => 42 );
180
43433317 181 like( exception {
182 $obj->option_accessor;
183 }, qr/Cannot call accessor without at least 1 argument/, '... options added okay with defaults' );
184
2d779ce6 185 is_deeply(
186 $obj->options, { quantity => 4, size => 42 },
187 'accessor as writer'
188 );
189
b10dde3a 190 is( exception {
1c08fd75 191 $class->new( options => { foo => 'BAR' } );
b10dde3a 192 }, undef, '... good constructor params' );
1c08fd75 193
b10dde3a 194 isnt( exception {
1c08fd75 195 $obj->set_option( bar => {} );
b10dde3a 196 }, undef, '... could not add a hash ref where an string is expected' );
1c08fd75 197
b10dde3a 198 isnt( exception {
1c08fd75 199 $class->new( options => { foo => [] } );
b10dde3a 200 }, undef, '... bad constructor params' );
1c08fd75 201
2d779ce6 202 $obj->options( {} );
203
7f5ec80d 204 is_deeply(
205 [ $obj->set_option( oink => "blah", xxy => "flop" ) ],
206 [ 'blah', 'flop' ],
207 'set returns newly set values in order of keys provided'
208 );
209
1399eab0 210 is_deeply(
211 [ sort $obj->keys ],
2d779ce6 212 [ 'oink', 'xxy' ],
1399eab0 213 'keys returns expected keys'
214 );
215
216 is_deeply(
217 [ sort $obj->values ],
2d779ce6 218 [ 'blah', 'flop' ],
1399eab0 219 'values returns expected values'
220 );
221
1c08fd75 222 my @key_value = sort { $a->[0] cmp $b->[0] } $obj->key_value;
223 is_deeply(
224 \@key_value,
225 [
226 sort { $a->[0] cmp $b->[0] }[ 'xxy', 'flop' ],
1c08fd75 227 [ 'oink', 'blah' ]
228 ],
229 '... got the right key value pairs'
230 )
231 or do {
232 require Data::Dumper;
233 diag( Data::Dumper::Dumper( \@key_value ) );
234 };
235
236 my %options_elements = $obj->options_elements;
237 is_deeply(
238 \%options_elements, {
239 'oink' => 'blah',
1c08fd75 240 'xxy' => 'flop'
241 },
242 '... got the right hash elements'
243 );
244
245 if ( $class->meta->get_attribute('options')->is_lazy ) {
246 my $obj = $class->new;
247
248 $obj->set_option( y => 2 );
249
250 is_deeply(
251 $obj->options, { x => 1, y => 2 },
252 'set_option with lazy default'
253 );
254
255 $obj->_clear_options;
256
257 ok(
258 $obj->has_option('x'),
259 'key for x exists - lazy default'
260 );
261
262 $obj->_clear_options;
263
264 ok(
265 $obj->is_defined('x'),
266 'key for x is defined - lazy default'
267 );
268
269 $obj->_clear_options;
270
271 is_deeply(
272 [ $obj->key_value ],
273 [ [ x => 1 ] ],
274 'kv returns lazy default'
275 );
865faf6f 276
865faf6f 277 $obj->_clear_options;
278
279 $obj->option_accessor( y => 2 );
280
281 is_deeply(
282 [ sort $obj->keys ],
283 [ 'x', 'y' ],
284 'accessor triggers lazy default generator'
285 );
1c08fd75 286 }
287 }
288 $class;
d50fc84a 289}
a28e50e4 290
291done_testing;