X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMouse.git;a=blobdiff_plain;f=t%2F040_type_constraints%2F022_custom_type_errors.t;fp=t%2F040_type_constraints%2F022_custom_type_errors.t;h=38757e7d1ec5190fb75209747eb67bd8ed4b948f;hp=0000000000000000000000000000000000000000;hb=45eea07f4090ae7ed7a6556bfa5897f34155b489;hpb=785080642e584152fc8d7e87d77f11c7483d3f14 diff --git a/t/040_type_constraints/022_custom_type_errors.t b/t/040_type_constraints/022_custom_type_errors.t new file mode 100644 index 0000000..38757e7 --- /dev/null +++ b/t/040_type_constraints/022_custom_type_errors.t @@ -0,0 +1,59 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 9; +use Test::Exception; + +{ + package Animal; + use Mouse; + use Mouse::Util::TypeConstraints; + + subtype 'Natural' => as 'Int' => where { $_ > 0 } => + message {"This number ($_) is not a positive integer!"}; + + subtype 'NaturalLessThanTen' => as 'Natural' => where { $_ < 10 } => + message {"This number ($_) is not less than ten!"}; + + has leg_count => ( + is => 'rw', + isa => 'NaturalLessThanTen', + lazy => 1, + default => 0, + ); +} + +lives_ok { my $goat = Animal->new( leg_count => 4 ) } +'... no errors thrown, value is good'; +lives_ok { my $spider = Animal->new( leg_count => 8 ) } +'... no errors thrown, value is good'; + +throws_ok { my $fern = Animal->new( leg_count => 0 ) } +qr/This number \(0\) is not less than ten!/, + 'gave custom supertype error message on new'; + +throws_ok { my $centipede = Animal->new( leg_count => 30 ) } +qr/This number \(30\) is not less than ten!/, + 'gave custom subtype error message on new'; + +my $chimera; +lives_ok { $chimera = Animal->new( leg_count => 4 ) } +'... no errors thrown, value is good'; + +throws_ok { $chimera->leg_count(0) } +qr/This number \(0\) is not less than ten!/, + 'gave custom supertype error message on set to 0'; + +throws_ok { $chimera->leg_count(16) } +qr/This number \(16\) is not less than ten!/, + 'gave custom subtype error message on set to 16'; + +my $gimp = eval { Animal->new() }; +is( $@, '', '... no errors thrown, value is good' ); + +throws_ok { $gimp->leg_count } +qr/This number \(0\) is not less than ten!/, + 'gave custom supertype error message on lazy set to 0'; +