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',
67 dies_ok { $foo->push_array('foo') } "array - can't push onto undef";
70 is_deeply( $foo->array, [], "array - correct contents" );
72 $foo->push_array('foo');
73 is_deeply( $foo->array, ['foo'], "array - correct contents" );
77 dies_ok { $foo->push_array_int(1) } "array_int - can't push onto undef";
79 $foo->array_int( [] );
80 is_deeply( $foo->array_int, [], "array_int - correct contents" );
82 dies_ok { $foo->push_array_int('foo') }
83 "array_int - can't push wrong type";
84 is_deeply( $foo->array_int, [], "array_int - correct contents" );
86 $foo->push_array_int(1);
87 is_deeply( $foo->array_int, [1], "array_int - correct contents" );
91 dies_ok { $foo->push_a1('foo') } "a1 - can't push onto undef";
94 is_deeply( $foo->a1, [], "a1 - correct contents" );
96 dies_ok { $foo->push_a1('foo') } "a1 - can't push wrong type";
98 is_deeply( $foo->a1, [], "a1 - correct contents" );
101 is_deeply( $foo->a1, [1], "a1 - correct contents" );
105 dies_ok { $foo->push_a2('foo') } "a2 - can't push onto undef";
108 is_deeply( $foo->a2, [], "a2 - correct contents" );
110 $foo->push_a2('foo');
111 is_deeply( $foo->a2, ['foo'], "a2 - correct contents" );
113 dies_ok { $foo->push_a2('bar') } "a2 - can't push more than one element";
115 is_deeply( $foo->a2, ['foo'], "a2 - correct contents" );
119 dies_ok { $foo->push_a3(1) } "a3 - can't push onto undef";
122 is_deeply( $foo->a3, [], "a3 - correct contents" );
124 dies_ok { $foo->push_a3('foo') } "a3 - can't push non-int";
126 dies_ok { $foo->push_a3(100) }
127 "a3 - can't violate overall type constraint";
129 is_deeply( $foo->a3, [], "a3 - correct contents" );
132 is_deeply( $foo->a3, [1], "a3 - correct contents" );
134 dies_ok { $foo->push_a3(100) }
135 "a3 - can't violate overall type constraint";
137 is_deeply( $foo->a3, [1], "a3 - correct contents" );
140 is_deeply( $foo->a3, [ 1, 3 ], "a3 - correct contents" );