Mouse::Util::does_role() respects $thing->does() method
[gitmo/Mouse.git] / t / 010_basics / 011_moose_respects_type_constraints.t
CommitLineData
60ad2cb7 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
9864f0e4 6use Test::More tests => 7;
60ad2cb7 7use Test::Exception;
8
9use Mouse::Util::TypeConstraints;
10
11=pod
12
13This tests demonstrates that Mouse will not override
14a preexisting type constraint of the same name when
15making constraints for a Mouse-class.
16
17It also tests that an attribute which uses a 'Foo' for
18it's isa option will get the subtype Foo, and not a
19type representing the Foo moose class.
20
21=cut
22
23BEGIN {
24 # create this subtype first (in BEGIN)
25 subtype Foo
26 => as 'Value'
27 => where { $_ eq 'Foo' };
28}
29
30{ # now seee if Mouse will override it
31 package Foo;
32 use Mouse;
33}
34
35my $foo_constraint = find_type_constraint('Foo');
36isa_ok($foo_constraint, 'Mouse::Meta::TypeConstraint');
37
38is($foo_constraint->parent->name, 'Value', '... got the Value subtype for Foo');
39
40ok($foo_constraint->check('Foo'), '... my constraint passed correctly');
41ok(!$foo_constraint->check('Bar'), '... my constraint failed correctly');
42
43{
44 package Bar;
45 use Mouse;
46
47 has 'foo' => (is => 'rw', isa => 'Foo');
48}
49
50my $bar = Bar->new;
51isa_ok($bar, 'Bar');
52
53lives_ok {
54 $bar->foo('Foo');
55} '... checked the type constraint correctly';
56
57dies_ok {
58 $bar->foo(Foo->new);
59} '... checked the type constraint correctly';
60
9864f0e4 61
62