Commit | Line | Data |
f5f2d482 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
a28e50e4 |
6 | use Test::More; |
b10dde3a |
7 | use Test::Fatal; |
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 | |
b10dde3a |
28 | is( exception { my $goat = Animal->new( leg_count => 4 ) }, undef, '... no errors thrown, value is good' ); |
29 | is( exception { my $spider = Animal->new( leg_count => 8 ) }, undef, '... no errors thrown, value is good' ); |
0e9e37e5 |
30 | |
b10dde3a |
31 | 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' ); |
f5f2d482 |
32 | |
b10dde3a |
33 | 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' ); |
f5f2d482 |
34 | |
ded5f696 |
35 | my $chimera; |
b10dde3a |
36 | is( exception { $chimera = Animal->new( leg_count => 4 ) }, undef, '... no errors thrown, value is good' ); |
f5f2d482 |
37 | |
b10dde3a |
38 | like( exception { $chimera->leg_count(0) }, qr/This number \(0\) is not less than ten!/, 'gave custom supertype error message on set to 0' ); |
f5f2d482 |
39 | |
b10dde3a |
40 | like( exception { $chimera->leg_count(16) }, qr/This number \(16\) is not less than ten!/, 'gave custom subtype error message on set to 16' ); |
f5f2d482 |
41 | |
ded5f696 |
42 | my $gimp = eval { Animal->new() }; |
43 | is( $@, '', '... no errors thrown, value is good' ); |
f5f2d482 |
44 | |
b10dde3a |
45 | like( exception { $gimp->leg_count }, qr/This number \(0\) is not less than ten!/, 'gave custom supertype error message on lazy set to 0' ); |
62bf83c7 |
46 | |
a28e50e4 |
47 | done_testing; |