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;
36 isa => 'ArrayRef[Int]',
38 push_array_int => 'push',
71 clearer => '_clear_a4',
75 accessor_a4 => 'accessor',
85 clearer => '_clear_a5',
89 accessor_a5 => 'accessor',
98 is_deeply( $foo->array, [], "array - correct contents" );
100 $foo->push_array('foo');
101 is_deeply( $foo->array, ['foo'], "array - correct contents" );
105 $foo->array_int( [] );
106 is_deeply( $foo->array_int, [], "array_int - correct contents" );
108 isnt( exception { $foo->push_array_int('foo') }, undef, "array_int - can't push wrong type" );
109 is_deeply( $foo->array_int, [], "array_int - correct contents" );
111 $foo->push_array_int(1);
112 is_deeply( $foo->array_int, [1], "array_int - correct contents" );
116 isnt( exception { $foo->push_a1('foo') }, undef, "a1 - can't push onto undef" );
119 is_deeply( $foo->a1, [], "a1 - correct contents" );
121 isnt( exception { $foo->push_a1('foo') }, undef, "a1 - can't push wrong type" );
123 is_deeply( $foo->a1, [], "a1 - correct contents" );
126 is_deeply( $foo->a1, [1], "a1 - correct contents" );
130 isnt( exception { $foo->push_a2('foo') }, undef, "a2 - can't push onto undef" );
133 is_deeply( $foo->a2, [], "a2 - correct contents" );
135 $foo->push_a2('foo');
136 is_deeply( $foo->a2, ['foo'], "a2 - correct contents" );
138 isnt( exception { $foo->push_a2('bar') }, undef, "a2 - can't push more than one element" );
140 is_deeply( $foo->a2, ['foo'], "a2 - correct contents" );
144 isnt( exception { $foo->push_a3(1) }, undef, "a3 - can't push onto undef" );
147 is_deeply( $foo->a3, [], "a3 - correct contents" );
149 isnt( exception { $foo->push_a3('foo') }, undef, "a3 - can't push non-int" );
151 isnt( exception { $foo->push_a3(100) }, undef, "a3 - can't violate overall type constraint" );
153 is_deeply( $foo->a3, [], "a3 - correct contents" );
156 is_deeply( $foo->a3, [1], "a3 - correct contents" );
158 isnt( exception { $foo->push_a3(100) }, undef, "a3 - can't violate overall type constraint" );
160 is_deeply( $foo->a3, [1], "a3 - correct contents" );
163 is_deeply( $foo->a3, [ 1, 3 ], "a3 - correct contents" );
167 my $expect = qr/\QAttribute (a4) does not pass the type constraint because: Validation failed for 'ArrayRef' with value invalid/;
170 exception { $foo->accessor_a4(0); },
172 'invalid default is caught when trying to read via accessor'
176 exception { $foo->accessor_a4(0 => 42); },
178 'invalid default is caught when trying to write via accessor'
182 exception { $foo->push_a4(42); },
184 'invalid default is caught when trying to push'
188 exception { $foo->get_a4(42); },
190 'invalid default is caught when trying to get'
198 $foo->accessor_a5(0), 'invalid',
199 'lazy default is coerced when trying to read via accessor'
204 $foo->accessor_a5( 1 => 'thing' );
208 [ 'invalid', 'thing' ],
209 'lazy default is coerced when trying to write via accessor'
214 $foo->push_a5('thing');
218 [ 'invalid', 'thing' ],
219 'lazy default is coerced when trying to push'
225 $foo->get_a5(0), 'invalid',
226 'lazy default is coerced when trying to get'