Change the signature of verify_type_constraint_error [IMCOMPATIBLE]
[gitmo/Mouse.git] / t / 001_mouse / 039-subtype.t
CommitLineData
c5146d28 1#!/usr/bin/env perl
2use strict;
3use warnings;
4c0e2aa7 4use Test::More tests => 11;
c5146d28 5use Test::Exception;
6
f7d4e5d1 7use Mouse::Util::TypeConstraints;
8
c5146d28 9do {
10 package My::Class;
11 use Mouse;
12 use Mouse::Util::TypeConstraints;
13
14 subtype 'NonemptyStr'
15 => as 'Str'
16 => where { length $_ }
17 => message { "The string is empty!" };
18
4c0e2aa7 19 subtype 'MyClass'
20 => as 'Object'
21 => where { $_->isa(__PACKAGE__) };
22
c5146d28 23 has name => (
24 is => 'ro',
25 isa => 'NonemptyStr',
26 );
4c0e2aa7 27
28
c5146d28 29};
30
31ok(My::Class->new(name => 'foo'));
32
29607c02 33throws_ok { My::Class->new(name => '') } qr/^Attribute \(name\) does not pass the type constraint because: The string is empty!/;
c5146d28 34
f7d4e5d1 35my $st = subtype as 'Str', where{ length };
36
37ok $st->is_a_type_of('Str');
38ok!$st->is_a_type_of('NoemptyStr');
39
40ok $st->check('Foo');
41ok!$st->check(undef);
42ok!$st->check('');
43
4c0e2aa7 44lives_and{
45 my $tc = find_type_constraint('MyClass');
46 ok $tc->check(My::Class->new());
47 ok!$tc->check('My::Class');
48 ok!$tc->check([]);
49 ok!$tc->check(undef);
50};