Comments are quite but completely useless
[gitmo/Moose.git] / t / 040_type_constraints / 022_custom_type_errors.t
CommitLineData
f5f2d482 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
599c5541 6use Test::More tests => 10;
f5f2d482 7use 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 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';
f5f2d482 35
62bf83c7 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";
f5f2d482 39
62bf83c7 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";
f5f2d482 43
44my $chimera;
62bf83c7 45lives_ok { $chimera = Animal->new( leg_count => 4 ) }
46'... no errors thrown, value is good';
f5f2d482 47
f5f2d482 48throws_ok { $chimera->leg_count(0) }
62bf83c7 49qr/This number \(0\) is not less than ten!/,
50 "gave custom supertype error message on set_value";
f5f2d482 51
f5f2d482 52throws_ok { $chimera->leg_count(16) }
62bf83c7 53qr/This number \(16\) is not less than ten!/,
54 "gave custom subtype error message on set_value";
f5f2d482 55
599c5541 56my $gimp;
62bf83c7 57lives_ok { my $gimp = Animal->new() } '... no errors thrown, value is good';
599c5541 58throws_ok { $gimp->leg_count }
62bf83c7 59qr/This number \(0\) is not less than ten!/,
60 "gave custom supertype error message on set_value";
61