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" );
173 my $expect = qr/\QAttribute (a4) does not pass the type constraint because: Validation failed for 'ArrayRef' with value invalid/;
176 exception { $foo->accessor_a4(0); },
178 'invalid default is caught when trying to read via accessor'
182 exception { $foo->accessor_a4(0 => 42); },
184 'invalid default is caught when trying to write via accessor'
188 exception { $foo->push_a4(42); },
190 'invalid default is caught when trying to push'
194 exception { $foo->get_a4(42); },
196 'invalid default is caught when trying to get'
204 $foo->accessor_a5(0), 'invalid',
205 'lazy default is coerced when trying to read via accessor'
210 $foo->accessor_a5( 1 => 'thing' );
214 [ 'invalid', 'thing' ],
215 'lazy default is coerced when trying to write via accessor'
220 $foo->push_a5('thing');
224 [ 'invalid', 'thing' ],
225 'lazy default is coerced when trying to push'
231 $foo->get_a5(0), 'invalid',
232 'lazy default is coerced when trying to get'