162ed5d378bbecc12b4575c6ce58692761d78e9f
[gitmo/Moo.git] / xt / moox-types.t
1 use strictures 1;
2 use Test::More;
3
4 {
5   package TypeOMatic;
6
7   use Moo::Role;
8   use Sub::Quote;
9   use MooX::Types::MooseLike::Base qw(Str);
10   use MooX::Types::MooseLike::Numeric qw(PositiveInt);
11
12   has named_type => (
13     is => 'ro',
14     isa => Str,
15   );
16
17   has named_external_type => (
18     is => 'ro',
19     isa => PositiveInt,
20   );
21
22   package TypeOMatic::Consumer;
23
24   # do this as late as possible to simulate "real" behaviour
25   use Moo::HandleMoose;
26   use Moose;
27   with 'TypeOMatic';
28 }
29
30 my $meta = Class::MOP::class_of('TypeOMatic::Consumer');
31
32 my ($str, $positive_int)
33   = map $meta->get_attribute($_)->type_constraint->name,
34       qw(named_type named_external_type);
35
36 is($str, 'Str', 'Built-in Moose type ok');
37 is(
38   $positive_int, 'MooseX::Types::Common::Numeric::PositiveInt',
39   'External (MooseX::Types type) ok'
40 );
41
42 local $@;
43 eval q {
44   package Fooble;
45   use Moo;
46   my $isa = sub { 1 };
47   $Moo::HandleMoose::TYPE_MAP{$isa} = sub { $isa };
48   has barble => (is => "ro", isa => $isa);
49   __PACKAGE__->meta->get_attribute("barble");
50 };
51
52 like(
53   $@,
54   qr/^error inflating attribute 'barble' for package 'Fooble': \$TYPE_MAP\{CODE\(\w+?\)\} did not return a valid type constraint/,
55   'error message for incorrect type constraint inflation',
56 );
57
58 done_testing;