11 use Moose::Util::TypeConstraints;
14 type Number => where { Scalar::Util::looks_like_number($_) };
16 => where { !ref($_) && !Number($_) }
17 => message { "This is not a string ($_)" };
23 subtype NaturalLessThanTen
26 => message { "The number '$_' is not less than 10" };
28 Moose::Util::TypeConstraints->export_type_constraints_as_functions();
30 ok(Number(5), '... this is a Num');
31 ok(!defined(Number('Foo')), '... this is not a Num');
33 my $number_tc = Moose::Util::TypeConstraints::find_type_constraint('Number');
34 is("$number_tc", 'Number', '... type constraint stringifies to name');
37 ok(String('Foo'), '... this is a Str');
38 ok(!defined(String(5)), '... this is not a Str');
40 ok(Natural(5), '... this is a Natural');
41 is(Natural(-5), undef, '... this is not a Natural');
42 is(Natural('Foo'), undef, '... this is not a Natural');
44 ok(NaturalLessThanTen(5), '... this is a NaturalLessThanTen');
45 is(NaturalLessThanTen(12), undef, '... this is not a NaturalLessThanTen');
46 is(NaturalLessThanTen(-5), undef, '... this is not a NaturalLessThanTen');
47 is(NaturalLessThanTen('Foo'), undef, '... this is not a NaturalLessThanTen');
51 my $negative = subtype Number => where { $_ < 0 };
52 ok(defined $negative, '... got a value back from negative');
53 isa_ok($negative, 'Moose::Meta::TypeConstraint');
55 ok($negative->check(-5), '... this is a negative number');
56 ok(!defined($negative->check(5)), '... this is not a negative number');
57 is($negative->check('Foo'), undef, '... this is not a negative number');
59 ok($negative->is_subtype_of('Number'), '... $negative is a subtype of Number');
60 ok(!$negative->is_subtype_of('String'), '... $negative is not a subtype of String');
62 my $negative2 = subtype Number => where { $_ < 0 } => message {"$_ is not a negative number"};
64 ok(defined $negative2, '... got a value back from negative');
65 isa_ok($negative2, 'Moose::Meta::TypeConstraint');
67 ok($negative2->check(-5), '... this is a negative number');
68 ok(!defined($negative2->check(5)), '... this is not a negative number');
69 is($negative2->check('Foo'), undef, '... this is not a negative number');
71 ok($negative2->is_subtype_of('Number'), '... $negative2 is a subtype of Number');
72 ok(!$negative2->is_subtype_of('String'), '... $negative is not a subtype of String');
74 ok($negative2->has_message, '... it has a message');
75 is($negative2->validate(2),
76 '2 is not a negative number',
77 '... validated unsuccessfully (got error)');
79 # check some meta-details
81 my $natural_less_than_ten = find_type_constraint('NaturalLessThanTen');
82 isa_ok($natural_less_than_ten, 'Moose::Meta::TypeConstraint');
84 ok($natural_less_than_ten->is_subtype_of('Natural'), '... NaturalLessThanTen is subtype of Natural');
85 ok($natural_less_than_ten->is_subtype_of('Number'), '... NaturalLessThanTen is subtype of Number');
86 ok(!$natural_less_than_ten->is_subtype_of('String'), '... NaturalLessThanTen is not subtype of String');
88 ok($natural_less_than_ten->has_message, '... it has a message');
90 ok(!defined($natural_less_than_ten->validate(5)), '... validated successfully (no error)');
92 is($natural_less_than_ten->validate(15),
93 "The number '15' is not less than 10",
94 '... validated unsuccessfully (got error)');
96 my $natural = find_type_constraint('Natural');
97 isa_ok($natural, 'Moose::Meta::TypeConstraint');
99 ok($natural->is_subtype_of('Number'), '... Natural is a subtype of Number');
100 ok(!$natural->is_subtype_of('String'), '... Natural is not a subtype of String');
102 ok(!$natural->has_message, '... it does not have a message');
104 ok(!defined($natural->validate(5)), '... validated successfully (no error)');
106 is($natural->validate(-5),
107 "Validation failed for 'Natural' with value -5",
108 '... validated unsuccessfully (got error)');
110 my $string = find_type_constraint('String');
111 isa_ok($string, 'Moose::Meta::TypeConstraint');
113 ok($string->has_message, '... it does have a message');
115 ok(!defined($string->validate("Five")), '... validated successfully (no error)');
117 is($string->validate(5),
118 "This is not a string (5)",
119 '... validated unsuccessfully (got error)');
121 is( exception { Moose::Meta::Attribute->new('bob', isa => 'Spong') }, undef, 'meta-attr construction ok even when type constraint utils loaded first' );
123 # Test type constraint predicate return values.
125 foreach my $predicate (qw/equals is_subtype_of is_a_type_of/) {
126 ok( !defined $string->$predicate('DoesNotExist'), "$predicate predicate returns undef for non existant constraint");
129 # Test adding things which don't look like types to the registry throws an exception
131 my $r = Moose::Util::TypeConstraints->get_type_constraint_registry;
132 like( exception {$r->add_type_constraint()}, qr/not a valid type constraint/, '->add_type_constraint(undef) throws' );
133 like( exception {$r->add_type_constraint('foo')}, qr/not a valid type constraint/, '->add_type_constraint("foo") throws' );
134 like( exception {$r->add_type_constraint(bless {}, 'SomeClass')}, qr/not a valid type constraint/, '->add_type_constraint(SomeClass->new) throws' );
136 # Test some specific things that in the past did not work,
137 # specifically weird variations on anon subtypes.
140 my $subtype = subtype as 'Str';
141 isa_ok( $subtype, 'Moose::Meta::TypeConstraint', 'got an anon subtype' );
142 is( $subtype->parent->name, 'Str', 'parent is Str' );
143 # This test sucks but is the best we can do
144 is( $subtype->constraint->(), 1,
145 'subtype has the null constraint' );
146 ok( ! $subtype->has_message, 'subtype has no message' );
150 my $subtype = subtype as 'ArrayRef[Num|Str]';
151 isa_ok( $subtype, 'Moose::Meta::TypeConstraint', 'got an anon subtype' );
152 is( $subtype->parent->name, 'ArrayRef[Num|Str]', 'parent is ArrayRef[Num|Str]' );
153 ok( ! $subtype->has_message, 'subtype has no message' );
157 my $subtype = subtype 'ArrayRef[Num|Str]' => message { 'foo' };
158 isa_ok( $subtype, 'Moose::Meta::TypeConstraint', 'got an anon subtype' );
159 is( $subtype->parent->name, 'ArrayRef[Num|Str]', 'parent is ArrayRef[Num|Str]' );
160 ok( $subtype->has_message, 'subtype does have a message' );
163 # alternative sugar-less calling style which is documented as legit:
165 my $subtype = subtype( 'MyStr', { as => 'Str' } );
166 isa_ok( $subtype, 'Moose::Meta::TypeConstraint', 'got a subtype' );
167 is( $subtype->name, 'MyStr', 'name is MyStr' );
168 is( $subtype->parent->name, 'Str', 'parent is Str' );
172 my $subtype = subtype( { as => 'Str' } );
173 isa_ok( $subtype, 'Moose::Meta::TypeConstraint', 'got a subtype' );
174 is( $subtype->name, '__ANON__', 'name is __ANON__' );
175 is( $subtype->parent->name, 'Str', 'parent is Str' );
179 my $subtype = subtype( { as => 'Str', where => sub { /X/ } } );
180 isa_ok( $subtype, 'Moose::Meta::TypeConstraint', 'got a subtype' );
181 is( $subtype->name, '__ANON__', 'name is __ANON__' );
182 is( $subtype->parent->name, 'Str', 'parent is Str' );
183 ok( $subtype->check('FooX'), 'constraint accepts FooX' );
184 ok( ! $subtype->check('Foo'), 'constraint reject Foo' );
188 like( exception { subtype 'Foo' }, qr/cannot consist solely of a name/, 'Cannot call subtype with a single string argument' );
191 # Back-compat for being called without sugar. Previously, calling with
192 # sugar was indistinguishable from calling directly.
195 no warnings 'redefine';
196 *Moose::Deprecated::deprecated = sub { return };
200 my $type = type( 'Number2', sub { Scalar::Util::looks_like_number($_) } );
202 ok( $type->check(5), '... this is a Num' );
203 ok( ! $type->check('Foo'), '... this is not a Num' );
208 my $subtype = subtype( 'Number2', sub { $_ > 0 } );
210 ok( $subtype->check(5), '... this is a Natural');
211 ok( ! $subtype->check(-5), '... this is not a Natural');
212 ok( ! $subtype->check('Foo'), '... this is not a Natural');
216 my $subtype = subtype( 'Natural2', 'Number2', sub { $_ > 0 } );
218 ok( $subtype->check(5), '... this is a Natural');
219 ok( ! $subtype->check(-5), '... this is not a Natural');
220 ok( ! $subtype->check('Foo'), '... this is not a Natural');
224 my $subtype = subtype( 'Natural3', 'Number2' );
226 ok( $subtype->check(5), '... this is a Natural');
227 ok( $subtype->check(-5), '... this is a Natural');
228 ok( ! $subtype->check('Foo'), '... this is not a Natural');