Add assert_valid() to Meta::TypeConstraint
[gitmo/Mouse.git] / t / 040_type_constraints / failing / 030_class_subtypes.t
CommitLineData
b2b106d7 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More tests => 26;
7use Test::Exception;
8
9use Mouse::Util::TypeConstraints;
10use Mouse::Meta::TypeConstraint;
11
12
13## Create a subclass with a custom method
14
15{
16 package Test::Mouse::Meta::TypeConstraint::AnySubType;
17 use Mouse;
18 extends 'Mouse::Meta::TypeConstraint';
19
20 sub my_custom_method {
21 return 1;
22 }
23}
24
25my $Int = find_type_constraint('Int');
26ok $Int, 'Got a good type contstraint';
27
28my $parent = Test::Mouse::Meta::TypeConstraint::AnySubType->new({
29 name => "Test::Mouse::Meta::TypeConstraint::AnySubType" ,
30 parent => $Int,
31});
32
33ok $parent, 'Created type constraint';
34ok $parent->check(1), 'Correctly passed';
35ok ! $parent->check('a'), 'correctly failed';
36ok $parent->my_custom_method, 'found the custom method';
37
38my $subtype1 = subtype 'another_subtype' => as $parent;
39
40ok $subtype1, 'Created type constraint';
41ok $subtype1->check(1), 'Correctly passed';
42ok ! $subtype1->check('a'), 'correctly failed';
43ok $subtype1->my_custom_method, 'found the custom method';
44
45
46my $subtype2 = subtype 'another_subtype' => as $subtype1 => where { $_ < 10 };
47
48ok $subtype2, 'Created type constraint';
49ok $subtype2->check(1), 'Correctly passed';
50ok ! $subtype2->check('a'), 'correctly failed';
51ok ! $subtype2->check(100), 'correctly failed';
52
53ok $subtype2->my_custom_method, 'found the custom method';
54
55
56{
57 package Foo;
58
59 use Mouse;
60}
61
62{
63 package Bar;
64
65 use Mouse;
66
67 extends 'Foo';
68}
69
70{
71 package Baz;
72
73 use Mouse;
74}
75
76my $foo = class_type 'Foo';
77my $isa_foo = subtype 'IsaFoo' => as $foo;
78
79ok $isa_foo, 'Created subtype of Foo type';
80ok $isa_foo->check( Foo->new ), 'Foo passes check';
81ok $isa_foo->check( Bar->new ), 'Bar passes check';
82ok ! $isa_foo->check( Baz->new ), 'Baz does not pass check';
83like $foo->get_message( Baz->new ), qr/^Validation failed for 'Foo' failed with value Baz=HASH\(0x\w+\) \(not isa Foo\)/, 'Better validation message';
84
85# Maybe in the future this *should* inherit?
86like $isa_foo->get_message( Baz->new ), qr/^Validation failed for 'IsaFoo' failed with value Baz=HASH\(0x\w+\)$/, "Subtypes do not automatically inherit parent type's message";
87
88
89# Implicit types
90{
91 package Quux;
92
93 use Mouse;
94
95 has age => (
96 isa => 'Positive',
97 is => 'bare',
98 );
99}
100
101throws_ok {
102 Quux->new(age => 3)
103} qr/^Attribute \(age\) does not pass the type constraint because: Validation failed for 'Positive' failed with value 3 \(not isa Positive\)/;
104
105lives_ok {
106 Quux->new(age => (bless {}, 'Positive'));
107};
108
109eval "
110 package Positive;
111 use Mouse;
112";
113
114throws_ok {
115 Quux->new(age => 3)
116} qr/^Attribute \(age\) does not pass the type constraint because: Validation failed for 'Positive' failed with value 3 \(not isa Positive\)/;
117
118lives_ok {
119 Quux->new(age => Positive->new)
120};
121
122class_type 'Negative' => message { "$_ is not a Negative Nancy" };
123
124{
125 package Quux::Ier;
126
127 use Mouse;
128
129 has age => (
130 isa => 'Negative',
131 is => 'bare',
132 );
133}
134
135throws_ok {
136 Quux::Ier->new(age => 3)
137} qr/^Attribute \(age\) does not pass the type constraint because: 3 is not a Negative Nancy /;
138
139lives_ok {
140 Quux::Ier->new(age => (bless {}, 'Negative'))
141};