Standardize use of Test::Exception before converting to Test::Fatal
[gitmo/Moose.git] / t / 100_bugs / 017_type_constraint_messages.t
CommitLineData
d5598ade 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
a28e50e4 6use Test::More;
53a4d826 7use Test::Exception;
d5598ade 8
7ff56534 9
d5598ade 10# RT #37569
11
12{
13 package MyObject;
14 use Moose;
15
16 package Foo;
17 use Moose;
18 use Moose::Util::TypeConstraints;
19
20 subtype 'MyArrayRef'
21 => as 'ArrayRef'
22 => where { defined $_->[0] }
d03bd989 23 => message { ref $_ ? "ref: ". ref $_ : 'scalar' } # stringy
d5598ade 24 ;
25
26 subtype 'MyObjectType'
27 => as 'Object'
28 => where { $_->isa('MyObject') }
29 => message {
30 if ( $_->isa('SomeObject') ) {
31 return 'More detailed error message';
32 }
33 elsif ( blessed $_ ) {
34 return 'Well it is an object';
35 }
36 else {
37 return 'Doh!';
38 }
39 }
40 ;
41
42 type 'NewType'
43 => where { $_->isa('MyObject') }
44 => message { blessed $_ ? 'blessed' : 'scalar' }
45 ;
46
47 has 'obj' => ( is => 'rw', isa => 'MyObjectType' );
48 has 'ar' => ( is => 'rw', isa => 'MyArrayRef' );
49 has 'nt' => ( is => 'rw', isa => 'NewType' );
50}
51
52my $foo = Foo->new;
53my $obj = MyObject->new;
54
53a4d826 55throws_ok {
21256595 56 $foo->ar( [] );
53a4d826 57}
21256595 58qr/Attribute \(ar\) does not pass the type constraint because: ref: ARRAY/,
59 '... got the right error message';
d5598ade 60
53a4d826 61throws_ok {
21256595 62 $foo->obj($foo); # Doh!
53a4d826 63}
21256595 64qr/Attribute \(obj\) does not pass the type constraint because: Well it is an object/,
65 '... got the right error message';
d5598ade 66
53a4d826 67throws_ok {
21256595 68 $foo->nt($foo); # scalar
53a4d826 69}
21256595 70qr/Attribute \(nt\) does not pass the type constraint because: blessed/,
71 '... got the right error message';
d5598ade 72
a28e50e4 73done_testing;