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