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