Remove doubled semi-colon
[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         key_value        => 'kv',
28         set_option       => 'set',
29     );
30
31     my $name = 'Foo1';
32
33     sub build_class {
34         my %attr = @_;
35
36         my $class = Moose::Meta::Class->create(
37             $name++,
38             superclasses => ['Moose::Object'],
39         );
40
41         my @traits = 'Hash';
42         push @traits, 'NoInlineAttribute'
43             if delete $attr{no_inline};
44
45         $class->add_attribute(
46             options => (
47                 traits  => \@traits,
48                 is      => 'ro',
49                 isa     => 'HashRef[Str]',
50                 default => sub { {} },
51                 handles => \%handles,
52                 clearer => '_clear_options',
53                 %attr,
54             ),
55         );
56
57         return ( $class->name, \%handles );
58     }
59 }
60
61 {
62     run_tests(build_class);
63     run_tests( build_class( lazy => 1, default => sub { { x => 1 } } ) );
64     run_tests( build_class( trigger => sub { } ) );
65     run_tests( build_class( no_inline => 1 ) );
66
67     # Will force the inlining code to check the entire hashref when it is modified.
68     subtype 'MyHashRef', as 'HashRef[Str]', where { 1 };
69
70     run_tests( build_class( isa => 'MyHashRef' ) );
71
72     coerce 'MyHashRef', from 'HashRef', via { $_ };
73
74     run_tests( build_class( isa => 'MyHashRef', coerce => 1 ) );
75 }
76
77 sub run_tests {
78     my ( $class, $handles ) = @_;
79
80     can_ok( $class, $_ ) for sort keys %{$handles};
81
82     with_immutable {
83         my $obj = $class->new( options => {} );
84
85         ok( $obj->has_no_options, '... we have no options' );
86         is( $obj->num_options, 0, '... we have no options' );
87
88         is_deeply( $obj->options, {}, '... no options yet' );
89         ok( !$obj->has_option('foo'), '... we have no foo option' );
90
91         ok ! exception {
92             is(
93                 $obj->set_option( foo => 'bar' ),
94                 'bar',
95                 'set return single new value in scalar context'
96             );
97         },
98         '... set the option okay';
99
100         ok( $obj->is_defined('foo'), '... foo is defined' );
101
102         ok( !$obj->has_no_options, '... we have options' );
103         is( $obj->num_options, 1, '... we have 1 option(s)' );
104         ok( $obj->has_option('foo'), '... we have a foo option' );
105         is_deeply( $obj->options, { foo => 'bar' }, '... got options now' );
106
107         ok ! exception {
108             $obj->set_option( bar => 'baz' );
109         },
110         '... 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         ok ! exception {
131             $obj->set_option( oink => "blah", xxy => "flop" );
132         },
133         '... set the option okay';
134
135         is( $obj->num_options, 4, "4 options" );
136         is_deeply(
137             [ $obj->get_option(qw(foo bar oink xxy)) ],
138             [qw(bar baz blah flop)], "get multiple options at once"
139         );
140
141         ok ! exception {
142             is( scalar $obj->delete_option('bar'), 'baz',
143                 'delete returns deleted value' );
144         },
145         '... deleted the option okay';
146
147         ok ! exception {
148             is_deeply(
149                 [ $obj->delete_option( 'oink', 'xxy' ) ],
150                 [ 'blah', 'flop' ],
151                 'delete returns all deleted values in list context'
152             );
153         },
154         '... deleted multiple option okay';
155
156         is( $obj->num_options, 1, '... we have 1 option(s)' );
157         is_deeply(
158             $obj->options, { foo => 'bar' },
159             '... got more options now'
160         );
161
162         $obj->clear_options;
163
164         is_deeply( $obj->options, {}, "... cleared options" );
165
166         ok ! exception {
167             $obj->quantity(4);
168         },
169         '... options added okay with defaults';
170
171         is( $obj->quantity, 4, 'reader part of curried accessor works' );
172
173         is_deeply(
174             $obj->options, { quantity => 4 },
175             '... returns what we expect'
176         );
177
178         ok ! exception {
179             $class->new( options => { foo => 'BAR' } );
180         },
181         '... good constructor params';
182
183         ok exception {
184             $obj->set_option( bar => {} );
185         },
186         '... could not add a hash ref where an string is expected';
187
188         ok exception {
189             $class->new( options => { foo => [] } );
190         },
191         '... bad constructor params';
192
193         is_deeply(
194             [ $obj->set_option( oink => "blah", xxy => "flop" ) ],
195             [ 'blah', 'flop' ],
196             'set returns newly set values in order of keys provided'
197         );
198
199         my @key_value = sort { $a->[0] cmp $b->[0] } $obj->key_value;
200         is_deeply(
201             \@key_value,
202             [
203                 sort { $a->[0] cmp $b->[0] }[ 'xxy', 'flop' ],
204                 [ 'quantity', 4 ],
205                 [ 'oink',     'blah' ]
206             ],
207             '... got the right key value pairs'
208             )
209             or do {
210             require Data::Dumper;
211             diag( Data::Dumper::Dumper( \@key_value ) );
212             };
213
214         my %options_elements = $obj->options_elements;
215         is_deeply(
216             \%options_elements, {
217                 'oink'     => 'blah',
218                 'quantity' => 4,
219                 'xxy'      => 'flop'
220             },
221             '... got the right hash elements'
222         );
223
224         if ( $class->meta->get_attribute('options')->is_lazy ) {
225             my $obj = $class->new;
226
227             $obj->set_option( y => 2 );
228
229             is_deeply(
230                 $obj->options, { x => 1, y => 2 },
231                 'set_option with lazy default'
232             );
233
234             $obj->_clear_options;
235
236             ok(
237                 $obj->has_option('x'),
238                 'key for x exists - lazy default'
239             );
240
241             $obj->_clear_options;
242
243             ok(
244                 $obj->is_defined('x'),
245                 'key for x is defined - lazy default'
246             );
247
248             $obj->_clear_options;
249
250             is_deeply(
251                 [ $obj->key_value ],
252                 [ [ x => 1 ] ],
253                 'kv returns lazy default'
254             );
255         }
256     }
257     $class;
258 }
259
260 done_testing;