Test is_subtype_of on role_type of undefined role
[gitmo/Moose.git] / t / type_constraints / role_type_constraint.t
CommitLineData
620db045 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
a28e50e4 6use Test::More;
b10dde3a 7use Test::Fatal;
620db045 8
28fdde7f 9use Moose::Util::TypeConstraints;
620db045 10
11{
12 package Gorch;
13 use Moose::Role;
14
15 package Bar;
16 use Moose::Role;
17
18 package Foo;
19 use Moose::Role;
20
21 with qw(Bar Gorch);
22
6df3a27e 23 package FooC;
24 use Moose;
25 with qw(Foo);
26
27 package BarC;
28 use Moose;
29 with qw(Bar);
30
620db045 31}
32
b10dde3a 33is( exception { role_type('Boop', message { "${_} is not a Boop" }) }, undef, 'role_type keywork works with message' );
620db045 34
35my $type = find_type_constraint("Foo");
36
37is( $type->role, "Foo", "role attribute" );
38
39ok( $type->is_subtype_of("Gorch"), "subtype of gorch" );
40
41ok( $type->is_subtype_of("Bar"), "subtype of bar" );
42
43ok( $type->is_subtype_of("Object"), "subtype of Object" );
44
4c015454 45ok( !$type->is_subtype_of("ThisTypeDoesNotExist"), "not subtype of unknown type name" );
46ok( !$type->is_a_type_of("ThisTypeDoesNotExist"), "not type of unknown type name" );
47
6df3a27e 48ok( find_type_constraint("Bar")->check(FooC->new), "Foo passes Bar" );
49ok( find_type_constraint("Bar")->check(BarC->new), "Bar passes Bar" );
50ok( !find_type_constraint("Gorch")->check(BarC->new), "but Bar doesn't pass Gorch");
620db045 51
620db045 52my $boop = find_type_constraint("Boop");
53ok( $boop->has_message, 'Boop has a message');
6df3a27e 54my $error = $boop->get_message(FooC->new);
620db045 55like( $error, qr/is not a Boop/, 'boop gives correct error message');
56
57
58ok( $type->equals($type), "equals self" );
59ok( $type->equals(Moose::Meta::TypeConstraint::Role->new( name => "__ANON__", role => "Foo" )), "equals anon constraint of same value" );
60ok( $type->equals(Moose::Meta::TypeConstraint::Role->new( name => "Oink", role => "Foo" )), "equals differently named constraint of same value" );
61ok( !$type->equals(Moose::Meta::TypeConstraint::Role->new( name => "__ANON__", role => "Bar" )), "doesn't equal other anon constraint" );
62ok( $type->is_subtype_of(Moose::Meta::TypeConstraint::Role->new( name => "__ANON__", role => "Bar" )), "subtype of other anon constraint" );
63
7b1df680 64{ # See block comment in t/type_constraints/class_type_constraint.t
65 my $type;
66 is( exception { $type = role_type 'MyExampleRole' }, undef, 'Make initial role_type' );
67 is( exception { is(role_type('MyExampleRole'), $type, 're-running role_type gives same type') }, undef, 'No exception making duplicate role_type' );;
985a8100 68 is( exception { ok( ! $type->is_subtype_of('Bar'), 'MyExampleRole is not a subtype of Bar' ) }, undef, 'No exception for is_subtype_of undefined role' );
7b1df680 69}
70
a28e50e4 71done_testing;