Commit | Line | Data |
f5f2d482 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
0e9e37e5 |
6 | use Test::More tests => 18; |
f5f2d482 |
7 | use Test::Exception; |
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 | |
0e9e37e5 |
28 | run_tests(); |
29 | Animal->meta->make_immutable; |
30 | run_tests(); |
31 | |
32 | sub run_tests { |
33 | lives_ok { my $goat = Animal->new( leg_count => 4 ) } |
34 | '... no errors thrown, value is good'; |
35 | lives_ok { my $spider = Animal->new( leg_count => 8 ) } |
36 | '... no errors thrown, value is good'; |
f5f2d482 |
37 | |
0e9e37e5 |
38 | throws_ok { my $fern = Animal->new( leg_count => 0 ) } |
39 | qr/This number \(0\) is not less than ten!/, |
40 | 'gave custom supertype error message on new'; |
f5f2d482 |
41 | |
0e9e37e5 |
42 | throws_ok { my $centipede = Animal->new( leg_count => 30 ) } |
43 | qr/This number \(30\) is not less than ten!/, |
44 | 'gave custom subtype error message on new'; |
f5f2d482 |
45 | |
0e9e37e5 |
46 | my $chimera; |
47 | lives_ok { $chimera = Animal->new( leg_count => 4 ) } |
48 | '... no errors thrown, value is good'; |
f5f2d482 |
49 | |
0e9e37e5 |
50 | throws_ok { $chimera->leg_count(0) } |
51 | qr/This number \(0\) is not less than ten!/, |
52 | 'gave custom supertype error message on set to 0'; |
f5f2d482 |
53 | |
0e9e37e5 |
54 | throws_ok { $chimera->leg_count(16) } |
55 | qr/This number \(16\) is not less than ten!/, |
56 | 'gave custom subtype error message on set to 16'; |
f5f2d482 |
57 | |
0e9e37e5 |
58 | my $gimp = eval { Animal->new() }; |
59 | is( $@, '', '... no errors thrown, value is good' ); |
62bf83c7 |
60 | |
0e9e37e5 |
61 | throws_ok { $gimp->leg_count } |
62 | qr/This number \(0\) is not less than ten!/, |
63 | 'gave custom supertype error message on lazy set to 0'; |
64 | } |