Add assert_valid() to Meta::TypeConstraint
[gitmo/Mouse.git] / t / 040_type_constraints / failing / 001_util_type_constraints.t
CommitLineData
b2b106d7 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More tests => 85;
7use Test::Exception;
8
9use Scalar::Util ();
10
11use lib 't/lib';
12use Test::Mouse;
13use Mouse::Util::TypeConstraints;
14
15
16type Number => where { Scalar::Util::looks_like_number($_) };
17type String
18 => where { !ref($_) && !Number($_) }
19 => message { "This is not a string ($_)" };
20
21subtype Natural
22 => as Number
23 => where { $_ > 0 };
24
25subtype NaturalLessThanTen
26 => as Natural
27 => where { $_ < 10 }
28 => message { "The number '$_' is not less than 10" };
29
30Mouse::Util::TypeConstraints->export_type_constraints_as_functions();
31
32ok(Number(5), '... this is a Num');
33ok(!defined(Number('Foo')), '... this is not a Num');
34{
35 my $number_tc = Mouse::Util::TypeConstraints::find_type_constraint('Number');
36 is("$number_tc", 'Number', '... type constraint stringifies to name');
37}
38
39ok(String('Foo'), '... this is a Str');
40ok(!defined(String(5)), '... this is not a Str');
41
42ok(Natural(5), '... this is a Natural');
43is(Natural(-5), undef, '... this is not a Natural');
44is(Natural('Foo'), undef, '... this is not a Natural');
45
46ok(NaturalLessThanTen(5), '... this is a NaturalLessThanTen');
47is(NaturalLessThanTen(12), undef, '... this is not a NaturalLessThanTen');
48is(NaturalLessThanTen(-5), undef, '... this is not a NaturalLessThanTen');
49is(NaturalLessThanTen('Foo'), undef, '... this is not a NaturalLessThanTen');
50
51# anon sub-typing
52
53my $negative = subtype Number => where { $_ < 0 };
54ok(defined $negative, '... got a value back from negative');
55isa_ok($negative, 'Mouse::Meta::TypeConstraint');
56
57ok($negative->check(-5), '... this is a negative number');
58ok(!defined($negative->check(5)), '... this is not a negative number');
59is($negative->check('Foo'), undef, '... this is not a negative number');
60
61ok($negative->is_subtype_of('Number'), '... $negative is a subtype of Number');
62ok(!$negative->is_subtype_of('String'), '... $negative is not a subtype of String');
63
64my $negative2 = subtype Number => where { $_ < 0 } => message {"$_ is not a negative number"};
65
66ok(defined $negative2, '... got a value back from negative');
67isa_ok($negative2, 'Mouse::Meta::TypeConstraint');
68
69ok($negative2->check(-5), '... this is a negative number');
70ok(!defined($negative2->check(5)), '... this is not a negative number');
71is($negative2->check('Foo'), undef, '... this is not a negative number');
72
73ok($negative2->is_subtype_of('Number'), '... $negative2 is a subtype of Number');
74ok(!$negative2->is_subtype_of('String'), '... $negative is not a subtype of String');
75
76ok($negative2->has_message, '... it has a message');
77is($negative2->validate(2),
78 '2 is not a negative number',
79 '... validated unsuccessfully (got error)');
80
81# check some meta-details
82
83my $natural_less_than_ten = find_type_constraint('NaturalLessThanTen');
84isa_ok($natural_less_than_ten, 'Mouse::Meta::TypeConstraint');
85
86ok($natural_less_than_ten->is_subtype_of('Natural'), '... NaturalLessThanTen is subtype of Natural');
87ok($natural_less_than_ten->is_subtype_of('Number'), '... NaturalLessThanTen is subtype of Number');
88ok(!$natural_less_than_ten->is_subtype_of('String'), '... NaturalLessThanTen is not subtype of String');
89
90ok($natural_less_than_ten->has_message, '... it has a message');
91
92ok(!defined($natural_less_than_ten->validate(5)), '... validated successfully (no error)');
93
94is($natural_less_than_ten->validate(15),
95 "The number '15' is not less than 10",
96 '... validated unsuccessfully (got error)');
97
98my $natural = find_type_constraint('Natural');
99isa_ok($natural, 'Mouse::Meta::TypeConstraint');
100
101ok($natural->is_subtype_of('Number'), '... Natural is a subtype of Number');
102ok(!$natural->is_subtype_of('String'), '... Natural is not a subtype of String');
103
104ok(!$natural->has_message, '... it does not have a message');
105
106ok(!defined($natural->validate(5)), '... validated successfully (no error)');
107
108is($natural->validate(-5),
109 "Validation failed for 'Natural' failed with value -5",
110 '... validated unsuccessfully (got error)');
111
112my $string = find_type_constraint('String');
113isa_ok($string, 'Mouse::Meta::TypeConstraint');
114
115ok($string->has_message, '... it does have a message');
116
117ok(!defined($string->validate("Five")), '... validated successfully (no error)');
118
119is($string->validate(5),
120"This is not a string (5)",
121'... validated unsuccessfully (got error)');
122
123lives_ok { Mouse::Meta::Attribute->new('bob', isa => 'Spong') }
124 'meta-attr construction ok even when type constraint utils loaded first';
125
126# Test type constraint predicate return values.
127
128foreach my $predicate (qw/equals is_subtype_of is_a_type_of/) {
129 ok( !defined $string->$predicate('DoesNotExist'), "$predicate predicate returns undef for non existant constraint");
130}
131
132# Test adding things which don't look like types to the registry throws an exception
133
134my $r = Mouse::Util::TypeConstraints->get_type_constraint_registry;
135throws_ok {$r->add_type_constraint()} qr/not a valid type constraint/, '->add_type_constraint(undef) throws';
136throws_ok {$r->add_type_constraint('foo')} qr/not a valid type constraint/, '->add_type_constraint("foo") throws';
137throws_ok {$r->add_type_constraint(bless {}, 'SomeClass')} qr/not a valid type constraint/, '->add_type_constraint(SomeClass->new) throws';
138
139# Test some specific things that in the past did not work,
140# specifically weird variations on anon subtypes.
141
142{
143 my $subtype = subtype as 'Str';
144 isa_ok( $subtype, 'Mouse::Meta::TypeConstraint', 'got an anon subtype' );
145 is( $subtype->parent->name, 'Str', 'parent is Str' );
146 # This test sucks but is the best we can do
147 is( $subtype->constraint->(), 1,
148 'subtype has the null constraint' );
149 ok( ! $subtype->has_message, 'subtype has no message' );
150}
151
152{
153 my $subtype = subtype as 'ArrayRef[Num|Str]';
154 isa_ok( $subtype, 'Mouse::Meta::TypeConstraint', 'got an anon subtype' );
155 is( $subtype->parent->name, 'ArrayRef[Num|Str]', 'parent is ArrayRef[Num|Str]' );
156 ok( ! $subtype->has_message, 'subtype has no message' );
157}
158
159{
160 my $subtype = subtype 'ArrayRef[Num|Str]' => message { 'foo' };
161 isa_ok( $subtype, 'Mouse::Meta::TypeConstraint', 'got an anon subtype' );
162 is( $subtype->parent->name, 'ArrayRef[Num|Str]', 'parent is ArrayRef[Num|Str]' );
163 ok( $subtype->has_message, 'subtype does have a message' );
164}
165
166# alternative sugar-less calling style which is documented as legit:
167{
168 my $subtype = subtype( 'MyStr', { as => 'Str' } );
169 isa_ok( $subtype, 'Mouse::Meta::TypeConstraint', 'got a subtype' );
170 is( $subtype->name, 'MyStr', 'name is MyStr' );
171 is( $subtype->parent->name, 'Str', 'parent is Str' );
172}
173
174{
175 my $subtype = subtype( { as => 'Str' } );
176 isa_ok( $subtype, 'Mouse::Meta::TypeConstraint', 'got a subtype' );
177 is( $subtype->name, '__ANON__', 'name is __ANON__' );
178 is( $subtype->parent->name, 'Str', 'parent is Str' );
179}
180
181{
182 my $subtype = subtype( { as => 'Str', where => sub { /X/ } } );
183 isa_ok( $subtype, 'Mouse::Meta::TypeConstraint', 'got a subtype' );
184 is( $subtype->name, '__ANON__', 'name is __ANON__' );
185 is( $subtype->parent->name, 'Str', 'parent is Str' );
186 ok( $subtype->check('FooX'), 'constraint accepts FooX' );
187 ok( ! $subtype->check('Foo'), 'constraint reject Foo' );
188}
189
190{
191 throws_ok { subtype 'Foo' } qr/cannot consist solely of a name/,
192 'Cannot call subtype with a single string argument';
193}
194
195# Back-compat for being called without sugar. Previously, calling with
196# sugar was indistinguishable from calling directly.
197
198{
199 my $type = type( 'Number2', sub { Scalar::Util::looks_like_number($_) } );
200
201 ok( $type->check(5), '... this is a Num' );
202 ok( ! $type->check('Foo'), '... this is not a Num' );
203}
204
205{
206 # anon subtype
207 my $subtype = subtype( 'Number2', sub { $_ > 0 } );
208
209 ok( $subtype->check(5), '... this is a Natural');
210 ok( ! $subtype->check(-5), '... this is not a Natural');
211 ok( ! $subtype->check('Foo'), '... this is not a Natural');
212}
213
214{
215 my $subtype = subtype( 'Natural2', 'Number2', sub { $_ > 0 } );
216
217 ok( $subtype->check(5), '... this is a Natural');
218 ok( ! $subtype->check(-5), '... this is not a Natural');
219 ok( ! $subtype->check('Foo'), '... this is not a Natural');
220}
221
222{
223 my $subtype = subtype( 'Natural3', 'Number2' );
224
225 ok( $subtype->check(5), '... this is a Natural');
226 ok( $subtype->check(-5), '... this is a Natural');
227 ok( ! $subtype->check('Foo'), '... this is not a Natural');
228}
229