Add tests for subtyping issue
[gitmo/Mouse.git] / t / 001_mouse / 039-subtype.t
CommitLineData
c5146d28 1#!/usr/bin/env perl
2use strict;
3use warnings;
f7d4e5d1 4use Test::More tests => 7;
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
19 has name => (
20 is => 'ro',
21 isa => 'NonemptyStr',
22 );
23};
24
25ok(My::Class->new(name => 'foo'));
26
29607c02 27throws_ok { My::Class->new(name => '') } qr/^Attribute \(name\) does not pass the type constraint because: The string is empty!/;
c5146d28 28
f7d4e5d1 29my $st = subtype as 'Str', where{ length };
30
31ok $st->is_a_type_of('Str');
32ok!$st->is_a_type_of('NoemptyStr');
33
34ok $st->check('Foo');
35ok!$st->check(undef);
36ok!$st->check('');
37