Commit | Line | Data |
c5146d28 |
1 | #!/usr/bin/env perl |
2 | use strict; |
3 | use warnings; |
f7d4e5d1 |
4 | use Test::More tests => 7; |
c5146d28 |
5 | use Test::Exception; |
6 | |
f7d4e5d1 |
7 | use Mouse::Util::TypeConstraints; |
8 | |
c5146d28 |
9 | do { |
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 | |
25 | ok(My::Class->new(name => 'foo')); |
26 | |
29607c02 |
27 | throws_ok { My::Class->new(name => '') } qr/^Attribute \(name\) does not pass the type constraint because: The string is empty!/; |
c5146d28 |
28 | |
f7d4e5d1 |
29 | my $st = subtype as 'Str', where{ length }; |
30 | |
31 | ok $st->is_a_type_of('Str'); |
32 | ok!$st->is_a_type_of('NoemptyStr'); |
33 | |
34 | ok $st->check('Foo'); |
35 | ok!$st->check(undef); |
36 | ok!$st->check(''); |
37 | |