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 no Moose::Util::TypeConstraints;
33 isa => 'ArrayRef[Int]',
35 push_array_int => 'push',
68 is_deeply( $foo->array, [], "array - correct contents" );
70 $foo->push_array('foo');
71 is_deeply( $foo->array, ['foo'], "array - correct contents" );
75 $foo->array_int( [] );
76 is_deeply( $foo->array_int, [], "array_int - correct contents" );
78 isnt( exception { $foo->push_array_int('foo') }, undef, "array_int - can't push wrong type" );
79 is_deeply( $foo->array_int, [], "array_int - correct contents" );
81 $foo->push_array_int(1);
82 is_deeply( $foo->array_int, [1], "array_int - correct contents" );
86 isnt( exception { $foo->push_a1('foo') }, undef, "a1 - can't push onto undef" );
89 is_deeply( $foo->a1, [], "a1 - correct contents" );
91 isnt( exception { $foo->push_a1('foo') }, undef, "a1 - can't push wrong type" );
93 is_deeply( $foo->a1, [], "a1 - correct contents" );
96 is_deeply( $foo->a1, [1], "a1 - correct contents" );
100 isnt( exception { $foo->push_a2('foo') }, undef, "a2 - can't push onto undef" );
103 is_deeply( $foo->a2, [], "a2 - correct contents" );
105 $foo->push_a2('foo');
106 is_deeply( $foo->a2, ['foo'], "a2 - correct contents" );
108 isnt( exception { $foo->push_a2('bar') }, undef, "a2 - can't push more than one element" );
110 is_deeply( $foo->a2, ['foo'], "a2 - correct contents" );
114 isnt( exception { $foo->push_a3(1) }, undef, "a3 - can't push onto undef" );
117 is_deeply( $foo->a3, [], "a3 - correct contents" );
119 isnt( exception { $foo->push_a3('foo') }, undef, "a3 - can't push non-int" );
121 isnt( exception { $foo->push_a3(100) }, undef, "a3 - can't violate overall type constraint" );
123 is_deeply( $foo->a3, [], "a3 - correct contents" );
126 is_deeply( $foo->a3, [1], "a3 - correct contents" );
128 isnt( exception { $foo->push_a3(100) }, undef, "a3 - can't violate overall type constraint" );
130 is_deeply( $foo->a3, [1], "a3 - correct contents" );
133 is_deeply( $foo->a3, [ 1, 3 ], "a3 - correct contents" );