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 dies_ok { $foo->set_hash_int( x => 'foo' ) }
70 "hash_int - can't set wrong type";
71 is_deeply( $foo->hash_int, {}, "hash_int - correct contents" );
73 $foo->set_hash_int( x => 1 );
74 is_deeply( $foo->hash_int, { x => 1 }, "hash_int - correct contents" );
78 dies_ok { $foo->set_h1('foo') } "h1 - can't set onto undef";
81 is_deeply( $foo->h1, {}, "h1 - correct contents" );
83 dies_ok { $foo->set_h1( x => 'foo' ) } "h1 - can't set wrong type";
85 is_deeply( $foo->h1, {}, "h1 - correct contents" );
87 $foo->set_h1( x => 1 );
88 is_deeply( $foo->h1, { x => 1 }, "h1 - correct contents" );
92 dies_ok { $foo->set_h2('foo') } "h2 - can't set onto undef";
95 is_deeply( $foo->h2, {}, "h2 - correct contents" );
97 $foo->set_h2( x => 'foo' );
98 is_deeply( $foo->h2, { x => 'foo' }, "h2 - correct contents" );
100 dies_ok { $foo->set_h2( y => 'bar' ) }
101 "h2 - can't set more than one element";
103 is_deeply( $foo->h2, { x => 'foo' }, "h2 - correct contents" );
107 dies_ok { $foo->set_h3(1) } "h3 - can't set onto undef";
110 is_deeply( $foo->h3, {}, "h3 - correct contents" );
112 dies_ok { $foo->set_h3( x => 'foo' ) } "h3 - can't set non-int";
114 dies_ok { $foo->set_h3( x => 100 ) }
115 "h3 - can't violate overall type constraint";
117 is_deeply( $foo->h3, {}, "h3 - correct contents" );
119 $foo->set_h3( x => 1 );
120 is_deeply( $foo->h3, { x => 1 }, "h3 - correct contents" );
122 dies_ok { $foo->set_h3( x => 100 ) }
123 "h3 - can't violate overall type constraint";
125 is_deeply( $foo->h3, { x => 1 }, "h3 - correct contents" );
127 $foo->set_h3( y => 3 );
128 is_deeply( $foo->h3, { x => 1, y => 3 }, "h3 - correct contents" );