Revert most of the conversion to Test::Fatal so we can redo it
[gitmo/Moose.git] / t / 040_type_constraints / 022_custom_type_errors.t
CommitLineData
f5f2d482 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
a28e50e4 6use Test::More;
53a4d826 7use Test::Exception;
f5f2d482 8
9{
10 package Animal;
11 use Moose;
0e9e37e5 12 use Moose::Util::TypeConstraints;
f5f2d482 13
62bf83c7 14 subtype 'Natural' => as 'Int' => where { $_ > 0 } =>
15 message {"This number ($_) is not a positive integer!"};
f5f2d482 16
62bf83c7 17 subtype 'NaturalLessThanTen' => as 'Natural' => where { $_ < 10 } =>
18 message {"This number ($_) is not less than ten!"};
f5f2d482 19
20 has leg_count => (
62bf83c7 21 is => 'rw',
22 isa => 'NaturalLessThanTen',
23 lazy => 1,
599c5541 24 default => 0,
f5f2d482 25 );
26}
27
53a4d826 28lives_ok { my $goat = Animal->new( leg_count => 4 ) }
ded5f696 29'... no errors thrown, value is good';
53a4d826 30lives_ok { my $spider = Animal->new( leg_count => 8 ) }
ded5f696 31'... no errors thrown, value is good';
0e9e37e5 32
53a4d826 33throws_ok { my $fern = Animal->new( leg_count => 0 ) }
ded5f696 34qr/This number \(0\) is not less than ten!/,
35 'gave custom supertype error message on new';
f5f2d482 36
53a4d826 37throws_ok { my $centipede = Animal->new( leg_count => 30 ) }
ded5f696 38qr/This number \(30\) is not less than ten!/,
39 'gave custom subtype error message on new';
f5f2d482 40
ded5f696 41my $chimera;
53a4d826 42lives_ok { $chimera = Animal->new( leg_count => 4 ) }
ded5f696 43'... no errors thrown, value is good';
f5f2d482 44
53a4d826 45throws_ok { $chimera->leg_count(0) }
ded5f696 46qr/This number \(0\) is not less than ten!/,
47 'gave custom supertype error message on set to 0';
f5f2d482 48
53a4d826 49throws_ok { $chimera->leg_count(16) }
ded5f696 50qr/This number \(16\) is not less than ten!/,
51 'gave custom subtype error message on set to 16';
f5f2d482 52
ded5f696 53my $gimp = eval { Animal->new() };
54is( $@, '', '... no errors thrown, value is good' );
f5f2d482 55
53a4d826 56throws_ok { $gimp->leg_count }
ded5f696 57qr/This number \(0\) is not less than ten!/,
58 'gave custom supertype error message on lazy set to 0';
62bf83c7 59
a28e50e4 60done_testing;