Spelling fix in Changes
[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
181 is_deeply(
182 $obj->options, { quantity => 4, size => 42 },
183 'accessor as writer'
184 );
185
b10dde3a 186 is( exception {
1c08fd75 187 $class->new( options => { foo => 'BAR' } );
b10dde3a 188 }, undef, '... good constructor params' );
1c08fd75 189
b10dde3a 190 isnt( exception {
1c08fd75 191 $obj->set_option( bar => {} );
b10dde3a 192 }, undef, '... could not add a hash ref where an string is expected' );
1c08fd75 193
b10dde3a 194 isnt( exception {
1c08fd75 195 $class->new( options => { foo => [] } );
b10dde3a 196 }, undef, '... bad constructor params' );
1c08fd75 197
2d779ce6 198 $obj->options( {} );
199
7f5ec80d 200 is_deeply(
201 [ $obj->set_option( oink => "blah", xxy => "flop" ) ],
202 [ 'blah', 'flop' ],
203 'set returns newly set values in order of keys provided'
204 );
205
1399eab0 206 is_deeply(
207 [ sort $obj->keys ],
2d779ce6 208 [ 'oink', 'xxy' ],
1399eab0 209 'keys returns expected keys'
210 );
211
212 is_deeply(
213 [ sort $obj->values ],
2d779ce6 214 [ 'blah', 'flop' ],
1399eab0 215 'values returns expected values'
216 );
217
1c08fd75 218 my @key_value = sort { $a->[0] cmp $b->[0] } $obj->key_value;
219 is_deeply(
220 \@key_value,
221 [
222 sort { $a->[0] cmp $b->[0] }[ 'xxy', 'flop' ],
1c08fd75 223 [ 'oink', 'blah' ]
224 ],
225 '... got the right key value pairs'
226 )
227 or do {
228 require Data::Dumper;
229 diag( Data::Dumper::Dumper( \@key_value ) );
230 };
231
232 my %options_elements = $obj->options_elements;
233 is_deeply(
234 \%options_elements, {
235 'oink' => 'blah',
1c08fd75 236 'xxy' => 'flop'
237 },
238 '... got the right hash elements'
239 );
240
241 if ( $class->meta->get_attribute('options')->is_lazy ) {
242 my $obj = $class->new;
243
244 $obj->set_option( y => 2 );
245
246 is_deeply(
247 $obj->options, { x => 1, y => 2 },
248 'set_option with lazy default'
249 );
250
251 $obj->_clear_options;
252
253 ok(
254 $obj->has_option('x'),
255 'key for x exists - lazy default'
256 );
257
258 $obj->_clear_options;
259
260 ok(
261 $obj->is_defined('x'),
262 'key for x is defined - lazy default'
263 );
264
265 $obj->_clear_options;
266
267 is_deeply(
268 [ $obj->key_value ],
269 [ [ x => 1 ] ],
270 'kv returns lazy default'
271 );
865faf6f 272
273
274 $obj->_clear_options;
275
276 $obj->option_accessor( y => 2 );
277
278 is_deeply(
279 [ sort $obj->keys ],
280 [ 'x', 'y' ],
281 'accessor triggers lazy default generator'
282 );
1c08fd75 283 }
284 }
285 $class;
d50fc84a 286}
a28e50e4 287
288done_testing;