10 use Moose::Util::TypeConstraints;
11 use List::Util qw( sum );
13 subtype 'H1', as 'HashRef[Int]';
14 subtype 'H2', as 'HashRef', where { scalar keys %{$_} < 2 };
15 subtype 'H3', as 'HashRef[Int]',
16 where { ( sum( values %{$_} ) || 0 ) < 5 };
18 no Moose::Util::TypeConstraints;
29 isa => 'HashRef[Int]',
31 set_hash_int => 'set',
67 is_deeply( $foo->hash_int, {}, "hash_int - correct contents" );
69 isnt( exception { $foo->set_hash_int( x => 'foo' ) }, undef, "hash_int - can't set wrong type" );
70 is_deeply( $foo->hash_int, {}, "hash_int - correct contents" );
72 $foo->set_hash_int( x => 1 );
73 is_deeply( $foo->hash_int, { x => 1 }, "hash_int - correct contents" );
77 isnt( exception { $foo->set_h1('foo') }, undef, "h1 - can't set onto undef" );
80 is_deeply( $foo->h1, {}, "h1 - correct contents" );
82 isnt( exception { $foo->set_h1( x => 'foo' ) }, undef, "h1 - can't set wrong type" );
84 is_deeply( $foo->h1, {}, "h1 - correct contents" );
86 $foo->set_h1( x => 1 );
87 is_deeply( $foo->h1, { x => 1 }, "h1 - correct contents" );
91 isnt( exception { $foo->set_h2('foo') }, undef, "h2 - can't set onto undef" );
94 is_deeply( $foo->h2, {}, "h2 - correct contents" );
96 $foo->set_h2( x => 'foo' );
97 is_deeply( $foo->h2, { x => 'foo' }, "h2 - correct contents" );
99 isnt( exception { $foo->set_h2( y => 'bar' ) }, undef, "h2 - can't set more than one element" );
101 is_deeply( $foo->h2, { x => 'foo' }, "h2 - correct contents" );
105 isnt( exception { $foo->set_h3(1) }, undef, "h3 - can't set onto undef" );
108 is_deeply( $foo->h3, {}, "h3 - correct contents" );
110 isnt( exception { $foo->set_h3( x => 'foo' ) }, undef, "h3 - can't set non-int" );
112 isnt( exception { $foo->set_h3( x => 100 ) }, undef, "h3 - can't violate overall type constraint" );
114 is_deeply( $foo->h3, {}, "h3 - correct contents" );
116 $foo->set_h3( x => 1 );
117 is_deeply( $foo->h3, { x => 1 }, "h3 - correct contents" );
119 isnt( exception { $foo->set_h3( x => 100 ) }, undef, "h3 - can't violate overall type constraint" );
121 is_deeply( $foo->h3, { x => 1 }, "h3 - correct contents" );
123 $foo->set_h3( y => 3 );
124 is_deeply( $foo->h3, { x => 1, y => 3 }, "h3 - correct contents" );