12 use Moose::Util::TypeConstraints;
14 subtype 'Natural' => as 'Int' => where { $_ > 0 } =>
15 message {"This number ($_) is not a positive integer!"};
17 subtype 'NaturalLessThanTen' => as 'Natural' => where { $_ < 10 } =>
18 message {"This number ($_) is not less than ten!"};
22 isa => 'NaturalLessThanTen',
28 is( exception { my $goat = Animal->new( leg_count => 4 ) }, undef, '... no errors thrown, value is good' );
29 is( exception { my $spider = Animal->new( leg_count => 8 ) }, undef, '... no errors thrown, value is good' );
31 like( exception { my $fern = Animal->new( leg_count => 0 ) }, qr/This number \(0\) is not less than ten!/, 'gave custom supertype error message on new' );
33 like( exception { my $centipede = Animal->new( leg_count => 30 ) }, qr/This number \(30\) is not less than ten!/, 'gave custom subtype error message on new' );
36 is( exception { $chimera = Animal->new( leg_count => 4 ) }, undef, '... no errors thrown, value is good' );
38 like( exception { $chimera->leg_count(0) }, qr/This number \(0\) is not less than ten!/, 'gave custom supertype error message on set to 0' );
40 like( exception { $chimera->leg_count(16) }, qr/This number \(16\) is not less than ten!/, 'gave custom subtype error message on set to 16' );
42 my $gimp = eval { Animal->new() };
43 is( $@, '', '... no errors thrown, value is good' );
45 like( exception { $gimp->leg_count }, qr/This number \(0\) is not less than ten!/, 'gave custom supertype error message on lazy set to 0' );