Commit | Line | Data |
f5f2d482 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
599c5541 |
6 | use Test::More tests => 10; |
f5f2d482 |
7 | use Test::Exception; |
8 | |
9 | { |
10 | package Animal; |
11 | use Moose; |
62bf83c7 |
12 | |
f5f2d482 |
13 | BEGIN { |
14 | ::use_ok("Moose::Util::TypeConstraints"); |
15 | } |
16 | |
62bf83c7 |
17 | subtype 'Natural' => as 'Int' => where { $_ > 0 } => |
18 | message {"This number ($_) is not a positive integer!"}; |
f5f2d482 |
19 | |
62bf83c7 |
20 | subtype 'NaturalLessThanTen' => as 'Natural' => where { $_ < 10 } => |
21 | message {"This number ($_) is not less than ten!"}; |
f5f2d482 |
22 | |
23 | has leg_count => ( |
62bf83c7 |
24 | is => 'rw', |
25 | isa => 'NaturalLessThanTen', |
26 | lazy => 1, |
599c5541 |
27 | default => 0, |
f5f2d482 |
28 | ); |
29 | } |
30 | |
62bf83c7 |
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'; |
f5f2d482 |
35 | |
62bf83c7 |
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"; |
f5f2d482 |
39 | |
62bf83c7 |
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"; |
f5f2d482 |
43 | |
44 | my $chimera; |
62bf83c7 |
45 | lives_ok { $chimera = Animal->new( leg_count => 4 ) } |
46 | '... no errors thrown, value is good'; |
f5f2d482 |
47 | |
f5f2d482 |
48 | throws_ok { $chimera->leg_count(0) } |
62bf83c7 |
49 | qr/This number \(0\) is not less than ten!/, |
50 | "gave custom supertype error message on set_value"; |
f5f2d482 |
51 | |
f5f2d482 |
52 | throws_ok { $chimera->leg_count(16) } |
62bf83c7 |
53 | qr/This number \(16\) is not less than ten!/, |
54 | "gave custom subtype error message on set_value"; |
f5f2d482 |
55 | |
599c5541 |
56 | my $gimp; |
62bf83c7 |
57 | lives_ok { my $gimp = Animal->new() } '... no errors thrown, value is good'; |
599c5541 |
58 | throws_ok { $gimp->leg_count } |
62bf83c7 |
59 | qr/This number \(0\) is not less than ten!/, |
60 | "gave custom supertype error message on set_value"; |
61 | |