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 dies_ok { $foo->push_array_int('foo') }
79 "array_int - can't push wrong type";
80 is_deeply( $foo->array_int, [], "array_int - correct contents" );
82 $foo->push_array_int(1);
83 is_deeply( $foo->array_int, [1], "array_int - correct contents" );
87 dies_ok { $foo->push_a1('foo') } "a1 - can't push onto undef";
90 is_deeply( $foo->a1, [], "a1 - correct contents" );
92 dies_ok { $foo->push_a1('foo') } "a1 - can't push wrong type";
94 is_deeply( $foo->a1, [], "a1 - correct contents" );
97 is_deeply( $foo->a1, [1], "a1 - correct contents" );
101 dies_ok { $foo->push_a2('foo') } "a2 - can't push onto undef";
104 is_deeply( $foo->a2, [], "a2 - correct contents" );
106 $foo->push_a2('foo');
107 is_deeply( $foo->a2, ['foo'], "a2 - correct contents" );
109 dies_ok { $foo->push_a2('bar') } "a2 - can't push more than one element";
111 is_deeply( $foo->a2, ['foo'], "a2 - correct contents" );
115 dies_ok { $foo->push_a3(1) } "a3 - can't push onto undef";
118 is_deeply( $foo->a3, [], "a3 - correct contents" );
120 dies_ok { $foo->push_a3('foo') } "a3 - can't push non-int";
122 dies_ok { $foo->push_a3(100) }
123 "a3 - can't violate overall type constraint";
125 is_deeply( $foo->a3, [], "a3 - correct contents" );
128 is_deeply( $foo->a3, [1], "a3 - correct contents" );
130 dies_ok { $foo->push_a3(100) }
131 "a3 - can't violate overall type constraint";
133 is_deeply( $foo->a3, [1], "a3 - correct contents" );
136 is_deeply( $foo->a3, [ 1, 3 ], "a3 - correct contents" );