8 use Moose::Util::TypeConstraints;
9 use List::Util qw(sum);
11 subtype 'A1', as 'ArrayRef[Int]';
12 subtype 'A2', as 'ArrayRef', where { @$_ < 2 };
13 subtype 'A3', as 'ArrayRef[Int]', where { ( sum(@$_) || 0 ) < 5 };
15 subtype 'A5', as 'ArrayRef';
16 coerce 'A5', from 'Str', via { [ $_ ] };
18 no Moose::Util::TypeConstraints;
37 isa => 'ArrayRef[Int]',
39 push_array_int => 'push',
76 clearer => '_clear_a4',
80 accessor_a4 => 'accessor',
91 clearer => '_clear_a5',
95 accessor_a5 => 'accessor',
104 is_deeply( $foo->array, [], "array - correct contents" );
106 $foo->push_array('foo');
107 is_deeply( $foo->array, ['foo'], "array - correct contents" );
111 $foo->array_int( [] );
112 is_deeply( $foo->array_int, [], "array_int - correct contents" );
114 isnt( exception { $foo->push_array_int('foo') }, undef, "array_int - can't push wrong type" );
115 is_deeply( $foo->array_int, [], "array_int - correct contents" );
117 $foo->push_array_int(1);
118 is_deeply( $foo->array_int, [1], "array_int - correct contents" );
122 isnt( exception { $foo->push_a1('foo') }, undef, "a1 - can't push onto undef" );
125 is_deeply( $foo->a1, [], "a1 - correct contents" );
127 isnt( exception { $foo->push_a1('foo') }, undef, "a1 - can't push wrong type" );
129 is_deeply( $foo->a1, [], "a1 - correct contents" );
132 is_deeply( $foo->a1, [1], "a1 - correct contents" );
136 isnt( exception { $foo->push_a2('foo') }, undef, "a2 - can't push onto undef" );
139 is_deeply( $foo->a2, [], "a2 - correct contents" );
141 $foo->push_a2('foo');
142 is_deeply( $foo->a2, ['foo'], "a2 - correct contents" );
144 isnt( exception { $foo->push_a2('bar') }, undef, "a2 - can't push more than one element" );
146 is_deeply( $foo->a2, ['foo'], "a2 - correct contents" );
150 isnt( exception { $foo->push_a3(1) }, undef, "a3 - can't push onto undef" );
153 is_deeply( $foo->a3, [], "a3 - correct contents" );
155 isnt( exception { $foo->push_a3('foo') }, undef, "a3 - can't push non-int" );
157 isnt( exception { $foo->push_a3(100) }, undef, "a3 - can't violate overall type constraint" );
159 is_deeply( $foo->a3, [], "a3 - correct contents" );
162 is_deeply( $foo->a3, [1], "a3 - correct contents" );
164 isnt( exception { $foo->push_a3(100) }, undef, "a3 - can't violate overall type constraint" );
166 is_deeply( $foo->a3, [1], "a3 - correct contents" );
169 is_deeply( $foo->a3, [ 1, 3 ], "a3 - correct contents" );
174 = qr/\QAttribute (a4) does not pass the type constraint because: Validation failed for 'ArrayRef' with value invalid/;
177 exception { $foo->accessor_a4(0); },
179 'invalid default is caught when trying to read via accessor'
183 exception { $foo->accessor_a4( 0 => 42 ); },
185 'invalid default is caught when trying to write via accessor'
189 exception { $foo->push_a4(42); },
191 'invalid default is caught when trying to push'
195 exception { $foo->get_a4(42); },
197 'invalid default is caught when trying to get'
205 $foo->accessor_a5(0), 'invalid',
206 'lazy default is coerced when trying to read via accessor'
211 $foo->accessor_a5( 1 => 'thing' );
215 [ 'invalid', 'thing' ],
216 'lazy default is coerced when trying to write via accessor'
221 $foo->push_a5('thing');
225 [ 'invalid', 'thing' ],
226 'lazy default is coerced when trying to push'
232 $foo->get_a5(0), 'invalid',
233 'lazy default is coerced when trying to get'
247 isa => 'ArrayRef[Foo]',
250 push_objects => 'push',
256 my $ha = HasArray->new();
259 exception { $ha->push_objects( Bar->new ) },
260 qr/\QValidation failed for 'Foo'/,
261 'got expected error when pushing an object of the wrong class onto an array ref'