File: type_constraint_messages.t

package info (click to toggle)
libmoose-perl 2.4000-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,408 kB
  • sloc: perl: 21,275; ansic: 291; makefile: 10
file content (65 lines) | stat: -rw-r--r-- 1,562 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use strict;
use warnings;

use Test::More;
use Test::Fatal;


# RT #37569

{
    package MyObject;
    use Moose;

    package Foo;
    use Moose;
    use Moose::Util::TypeConstraints;

    subtype 'MyArrayRef'
       => as 'ArrayRef'
       => where { defined $_->[0] }
       => message { ref $_ ? "ref: ". ref $_ : 'scalar' }  # stringy
    ;

    subtype 'MyObjectType'
       => as 'Object'
       => where { $_->isa('MyObject') }
       => message {
          if ( $_->isa('SomeObject') ) {
            return 'More detailed error message';
          }
          elsif ( blessed $_ ) {
            return 'Well it is an object';
          }
          else {
            return 'Doh!';
          }
       }
    ;

    type 'NewType'
       => where { $_->isa('MyObject') }
       => message { blessed $_ ? 'blessed' : 'scalar' }
    ;

    has 'obj' => ( is => 'rw', isa => 'MyObjectType' );
    has 'ar'  => ( is => 'rw', isa => 'MyArrayRef' );
    has 'nt'  => ( is => 'rw', isa => 'NewType' );
}

my $foo = Foo->new;
my $obj = MyObject->new;

like( exception {
    $foo->ar( [] );
}, qr/Attribute \(ar\) does not pass the type constraint because: ref: ARRAY/, '... got the right error message' );

like( exception {
    $foo->obj($foo);    # Doh!
}, qr/Attribute \(obj\) does not pass the type constraint because: Well it is an object/, '... got the right error message' );

like( exception {
    $foo->nt($foo);     # scalar
}, qr/Attribute \(nt\) does not pass the type constraint because: blessed/, '... got the right error message' );

done_testing;