Mouse::Util::does_role() respects $thing->does() method
[gitmo/Mouse.git] / t / 040_type_constraints / 022_custom_type_errors.t
1 #!/usr/bin/perl
2 # This is automatically generated by author/import-moose-test.pl.
3 # DO NOT EDIT THIS FILE. ANY CHANGES WILL BE LOST!!!
4 use t::lib::MooseCompat;
5
6 use strict;
7 use warnings;
8
9 use Test::More;
10 use Test::Exception;
11
12 {
13     package Animal;
14     use Mouse;
15     use Mouse::Util::TypeConstraints;
16
17     subtype 'Natural' => as 'Int' => where { $_ > 0 } =>
18         message {"This number ($_) is not a positive integer!"};
19
20     subtype 'NaturalLessThanTen' => as 'Natural' => where { $_ < 10 } =>
21         message {"This number ($_) is not less than ten!"};
22
23     has leg_count => (
24         is      => 'rw',
25         isa     => 'NaturalLessThanTen',
26         lazy    => 1,
27         default => 0,
28     );
29 }
30
31 lives_ok { my $goat = Animal->new( leg_count => 4 ) }
32 '... no errors thrown, value is good';
33 lives_ok { my $spider = Animal->new( leg_count => 8 ) }
34 '... no errors thrown, value is good';
35
36 throws_ok { my $fern = Animal->new( leg_count => 0 ) }
37 qr/This number \(0\) is not less than ten!/,
38     'gave custom supertype error message on new';
39
40 throws_ok { my $centipede = Animal->new( leg_count => 30 ) }
41 qr/This number \(30\) is not less than ten!/,
42     'gave custom subtype error message on new';
43
44 my $chimera;
45 lives_ok { $chimera = Animal->new( leg_count => 4 ) }
46 '... no errors thrown, value is good';
47
48 throws_ok { $chimera->leg_count(0) }
49 qr/This number \(0\) is not less than ten!/,
50     'gave custom supertype error message on set to 0';
51
52 throws_ok { $chimera->leg_count(16) }
53 qr/This number \(16\) is not less than ten!/,
54     'gave custom subtype error message on set to 16';
55
56 my $gimp = eval { Animal->new() };
57 is( $@, '', '... no errors thrown, value is good' );
58
59 throws_ok { $gimp->leg_count }
60 qr/This number \(0\) is not less than ten!/,
61     'gave custom supertype error message on lazy set to 0';
62
63 done_testing;