Mouse::Util::does_role() respects $thing->does() method
[gitmo/Mouse.git] / t / 040_type_constraints / 022_custom_type_errors.t
CommitLineData
b2b106d7 1#!/usr/bin/perl
5a592ad7 2# This is automatically generated by author/import-moose-test.pl.
3# DO NOT EDIT THIS FILE. ANY CHANGES WILL BE LOST!!!
4use t::lib::MooseCompat;
b2b106d7 5
6use strict;
7use warnings;
8
5a592ad7 9use Test::More;
b2b106d7 10use 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
31lives_ok { my $goat = Animal->new( leg_count => 4 ) }
32'... no errors thrown, value is good';
33lives_ok { my $spider = Animal->new( leg_count => 8 ) }
34'... no errors thrown, value is good';
35
36throws_ok { my $fern = Animal->new( leg_count => 0 ) }
37qr/This number \(0\) is not less than ten!/,
38 'gave custom supertype error message on new';
39
40throws_ok { my $centipede = Animal->new( leg_count => 30 ) }
41qr/This number \(30\) is not less than ten!/,
42 'gave custom subtype error message on new';
43
44my $chimera;
45lives_ok { $chimera = Animal->new( leg_count => 4 ) }
46'... no errors thrown, value is good';
47
48throws_ok { $chimera->leg_count(0) }
49qr/This number \(0\) is not less than ten!/,
50 'gave custom supertype error message on set to 0';
51
52throws_ok { $chimera->leg_count(16) }
53qr/This number \(16\) is not less than ten!/,
54 'gave custom subtype error message on set to 16';
55
56my $gimp = eval { Animal->new() };
57is( $@, '', '... no errors thrown, value is good' );
58
59throws_ok { $gimp->leg_count }
60qr/This number \(0\) is not less than ten!/,
61 'gave custom supertype error message on lazy set to 0';
62
5a592ad7 63done_testing;