Redid conversion to Test::Fatal
[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         is( exception {
92             is(
93                 $obj->set_option( foo => 'bar' ),
94                 'bar',
95                 'set return single new value in scalar context'
96             );
97         }, undef, '... set the option okay' );
98
99         ok( $obj->is_defined('foo'), '... foo is defined' );
100
101         ok( !$obj->has_no_options, '... we have options' );
102         is( $obj->num_options, 1, '... we have 1 option(s)' );
103         ok( $obj->has_option('foo'), '... we have a foo option' );
104         is_deeply( $obj->options, { foo => 'bar' }, '... got options now' );
105
106         is( exception {
107             $obj->set_option( bar => 'baz' );
108         }, undef, '... set the option okay' );
109
110         is( $obj->num_options, 2, '... we have 2 option(s)' );
111         is_deeply(
112             $obj->options, { foo => 'bar', bar => 'baz' },
113             '... got more options now'
114         );
115
116         is( $obj->get_option('foo'), 'bar', '... got the right option' );
117
118         is_deeply(
119             [ $obj->get_option(qw(foo bar)) ], [qw(bar baz)],
120             "get multiple options at once"
121         );
122
123         is(
124             scalar( $obj->get_option(qw( foo bar)) ), "baz",
125             '... got last option in scalar context'
126         );
127
128         is( exception {
129             $obj->set_option( oink => "blah", xxy => "flop" );
130         }, undef, '... set the option okay' );
131
132         is( $obj->num_options, 4, "4 options" );
133         is_deeply(
134             [ $obj->get_option(qw(foo bar oink xxy)) ],
135             [qw(bar baz blah flop)], "get multiple options at once"
136         );
137
138         is( exception {
139             is( scalar $obj->delete_option('bar'), 'baz',
140                 'delete returns deleted value' );
141         }, undef, '... deleted the option okay' );
142
143         is( exception {
144             is_deeply(
145                 [ $obj->delete_option( 'oink', 'xxy' ) ],
146                 [ 'blah', 'flop' ],
147                 'delete returns all deleted values in list context'
148             );
149         }, undef, '... deleted multiple option okay' );
150
151         is( $obj->num_options, 1, '... we have 1 option(s)' );
152         is_deeply(
153             $obj->options, { foo => 'bar' },
154             '... got more options now'
155         );
156
157         $obj->clear_options;
158
159         is_deeply( $obj->options, {}, "... cleared options" );
160
161         is( exception {
162             $obj->quantity(4);
163         }, undef, '... options added okay with defaults' );
164
165         is( $obj->quantity, 4, 'reader part of curried accessor works' );
166
167         is_deeply(
168             $obj->options, { quantity => 4 },
169             '... returns what we expect'
170         );
171
172         is( exception {
173             $class->new( options => { foo => 'BAR' } );
174         }, undef, '... good constructor params' );
175
176         isnt( exception {
177             $obj->set_option( bar => {} );
178         }, undef, '... could not add a hash ref where an string is expected' );
179
180         isnt( exception {
181             $class->new( options => { foo => [] } );
182         }, undef, '... bad constructor params' );
183
184         is_deeply(
185             [ $obj->set_option( oink => "blah", xxy => "flop" ) ],
186             [ 'blah', 'flop' ],
187             'set returns newly set values in order of keys provided'
188         );
189
190         my @key_value = sort { $a->[0] cmp $b->[0] } $obj->key_value;
191         is_deeply(
192             \@key_value,
193             [
194                 sort { $a->[0] cmp $b->[0] }[ 'xxy', 'flop' ],
195                 [ 'quantity', 4 ],
196                 [ 'oink',     'blah' ]
197             ],
198             '... got the right key value pairs'
199             )
200             or do {
201             require Data::Dumper;
202             diag( Data::Dumper::Dumper( \@key_value ) );
203             };
204
205         my %options_elements = $obj->options_elements;
206         is_deeply(
207             \%options_elements, {
208                 'oink'     => 'blah',
209                 'quantity' => 4,
210                 'xxy'      => 'flop'
211             },
212             '... got the right hash elements'
213         );
214
215         if ( $class->meta->get_attribute('options')->is_lazy ) {
216             my $obj = $class->new;
217
218             $obj->set_option( y => 2 );
219
220             is_deeply(
221                 $obj->options, { x => 1, y => 2 },
222                 'set_option with lazy default'
223             );
224
225             $obj->_clear_options;
226
227             ok(
228                 $obj->has_option('x'),
229                 'key for x exists - lazy default'
230             );
231
232             $obj->_clear_options;
233
234             ok(
235                 $obj->is_defined('x'),
236                 'key for x is defined - lazy default'
237             );
238
239             $obj->_clear_options;
240
241             is_deeply(
242                 [ $obj->key_value ],
243                 [ [ x => 1 ] ],
244                 'kv returns lazy default'
245             );
246         }
247     }
248     $class;
249 }
250
251 done_testing;