b7bb837685975d833f9caea0e12770312082e808
[gitmo/Moose.git] / t / 070_native_traits / 050_trait_hash.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use lib 't/lib';
7
8 use Moose ();
9 use Moose::Util::TypeConstraints;
10 use NoInlineAttribute;
11 use Test::Fatal;
12 use Test::More;
13 use Test::Moose;
14
15 {
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',
27         keys             => 'keys',
28         values           => 'values',
29         key_value        => 'kv',
30         set_option       => 'set',
31     );
32
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
43         my @traits = 'Hash';
44         push @traits, 'NoInlineAttribute'
45             if delete $attr{no_inline};
46
47         $class->add_attribute(
48             options => (
49                 traits  => \@traits,
50                 is      => 'rw',
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     }
61 }
62
63 {
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 ) );
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 ) );
77 }
78
79 sub run_tests {
80     my ( $class, $handles ) = @_;
81
82     can_ok( $class, $_ ) for sort keys %{$handles};
83
84     with_immutable {
85         my $obj = $class->new( options => {} );
86
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
93         is( exception {
94             is(
95                 $obj->set_option( foo => 'bar' ),
96                 'bar',
97                 'set return single new value in scalar context'
98             );
99         }, undef, '... set the option okay' );
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
108         is( exception {
109             $obj->set_option( bar => 'baz' );
110         }, undef, '... set the option okay' );
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
130         is( exception {
131             $obj->set_option( oink => "blah", xxy => "flop" );
132         }, undef, '... set the option okay' );
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
140         is( exception {
141             is( scalar $obj->delete_option('bar'), 'baz',
142                 'delete returns deleted value' );
143         }, undef, '... deleted the option okay' );
144
145         is( exception {
146             is_deeply(
147                 [ $obj->delete_option( 'oink', 'xxy' ) ],
148                 [ 'blah', 'flop' ],
149                 'delete returns all deleted values in list context'
150             );
151         }, undef, '... deleted multiple option okay' );
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
163         is( exception {
164             $obj->quantity(4);
165         }, undef, '... options added okay with defaults' );
166
167         is( $obj->quantity, 4, 'reader part of curried accessor works' );
168
169         is(
170             $obj->option_accessor('quantity'), 4,
171             'accessor as reader'
172         );
173
174         is_deeply(
175             $obj->options, { quantity => 4 },
176             '... returns what we expect'
177         );
178
179         $obj->option_accessor( size => 42, group => 10 );
180
181         like(
182             exception {
183                 $obj->option_accessor;
184             },
185             qr/Cannot call accessor without at least 1 argument/,
186             'error when calling accessor with no arguments'
187         );
188
189         is_deeply(
190             $obj->options, { quantity => 4, size => 42, group => 10 },
191             'accessor as writer'
192         );
193
194         is( exception {
195             $class->new( options => { foo => 'BAR' } );
196         }, undef, '... good constructor params' );
197
198         isnt( exception {
199             $obj->set_option( bar => {} );
200         }, undef, '... could not add a hash ref where an string is expected' );
201
202         isnt( exception {
203             $class->new( options => { foo => [] } );
204         }, undef, '... bad constructor params' );
205
206         $obj->options( {} );
207
208         is_deeply(
209             [ $obj->set_option( oink => "blah", xxy => "flop" ) ],
210             [ 'blah', 'flop' ],
211             'set returns newly set values in order of keys provided'
212         );
213
214         is_deeply(
215             [ sort $obj->keys ],
216             [ 'oink', 'xxy' ],
217             'keys returns expected keys'
218         );
219
220         is_deeply(
221             [ sort $obj->values ],
222             [ 'blah', 'flop' ],
223             'values returns expected values'
224         );
225
226         my @key_value = sort { $a->[0] cmp $b->[0] } $obj->key_value;
227         is_deeply(
228             \@key_value,
229             [
230                 sort { $a->[0] cmp $b->[0] }[ 'xxy', 'flop' ],
231                 [ 'oink',     'blah' ]
232             ],
233             '... got the right key value pairs'
234             )
235             or do {
236             require Data::Dumper;
237             diag( Data::Dumper::Dumper( \@key_value ) );
238             };
239
240         my %options_elements = $obj->options_elements;
241         is_deeply(
242             \%options_elements, {
243                 'oink'     => 'blah',
244                 'xxy'      => 'flop'
245             },
246             '... got the right hash elements'
247         );
248
249         if ( $class->meta->get_attribute('options')->is_lazy ) {
250             my $obj = $class->new;
251
252             $obj->set_option( y => 2 );
253
254             is_deeply(
255                 $obj->options, { x => 1, y => 2 },
256                 'set_option with lazy default'
257             );
258
259             $obj->_clear_options;
260
261             ok(
262                 $obj->has_option('x'),
263                 'key for x exists - lazy default'
264             );
265
266             $obj->_clear_options;
267
268             ok(
269                 $obj->is_defined('x'),
270                 'key for x is defined - lazy default'
271             );
272
273             $obj->_clear_options;
274
275             is_deeply(
276                 [ $obj->key_value ],
277                 [ [ x => 1 ] ],
278                 'kv returns lazy default'
279             );
280
281             $obj->_clear_options;
282
283             $obj->option_accessor( y => 2 );
284
285             is_deeply(
286                 [ sort $obj->keys ],
287                 [ 'x', 'y' ],
288                 'accessor triggers lazy default generator'
289             );
290         }
291     }
292     $class;
293 }
294
295 done_testing;