use strict;
use warnings;
-use Test::More tests => 8;
-use Test::Exception;
+use Test::More;
+use Test::Fatal;
{
package Animal;
use Moose;
- BEGIN {
- ::use_ok("Moose::Util::TypeConstraints");
- }
+ use Moose::Util::TypeConstraints;
- subtype 'Natural'
- => as 'Int'
- => where { $_ > 0 }
- => message { "This number ($_) is not a positive integer!" };
+ 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!" };
+ subtype 'NaturalLessThanTen' => as 'Natural' => where { $_ < 10 } =>
+ message {"This number ($_) is not less than ten!"};
has leg_count => (
- is => 'rw',
- isa => 'NaturalLessThanTen',
+ is => 'rw',
+ isa => 'NaturalLessThanTen',
+ lazy => 1,
+ default => 0,
);
}
-lives_ok { my $goat = Animal->new(leg_count => 4) };
-lives_ok { my $spider = Animal->new(leg_count => 8) };
+is( exception { my $goat = Animal->new( leg_count => 4 ) }, undef, '... no errors thrown, value is good' );
+is( exception { my $spider = Animal->new( leg_count => 8 ) }, undef, '... no errors thrown, value is good' );
-throws_ok { my $fern = Animal->new(leg_count => 0) }
- qr/^This number \(0\) is not a positive integer!/,
- "gave custom supertype error message on new";
+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' );
-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";
+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' );
my $chimera;
-lives_ok { $chimera = Animal->new(leg_count => 4) };
+is( exception { $chimera = Animal->new( leg_count => 4 ) }, undef, '... no errors thrown, value is good' );
-# first we remove the lion's legs..
-throws_ok { $chimera->leg_count(0) }
- qr/^This number \(0\) is not a positive integer!/,
- "gave custom supertype error message on set_value";
+like( exception { $chimera->leg_count(0) }, qr/This number \(0\) is not less than ten!/, 'gave custom supertype error message on set to 0' );
-# mix in a few octopodes
-throws_ok { $chimera->leg_count(16) }
- qr/^This number \(16\) is not less than ten!/,
- "gave custom subtype error message on set_value";
+like( exception { $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' );
+
+like( exception { $gimp->leg_count }, qr/This number \(0\) is not less than ten!/, 'gave custom supertype error message on lazy set to 0' );
+
+done_testing;