6 use Test::More tests => 85;
13 use Mouse::Util::TypeConstraints;
16 type Number => where { Scalar::Util::looks_like_number($_) };
18 => where { !ref($_) && !Number($_) }
19 => message { "This is not a string ($_)" };
25 subtype NaturalLessThanTen
28 => message { "The number '$_' is not less than 10" };
30 Mouse::Util::TypeConstraints->export_type_constraints_as_functions();
32 ok(Number(5), '... this is a Num');
33 ok(!defined(Number('Foo')), '... this is not a Num');
35 my $number_tc = Mouse::Util::TypeConstraints::find_type_constraint('Number');
36 is("$number_tc", 'Number', '... type constraint stringifies to name');
39 ok(String('Foo'), '... this is a Str');
40 ok(!defined(String(5)), '... this is not a Str');
42 ok(Natural(5), '... this is a Natural');
43 is(Natural(-5), undef, '... this is not a Natural');
44 is(Natural('Foo'), undef, '... this is not a Natural');
46 ok(NaturalLessThanTen(5), '... this is a NaturalLessThanTen');
47 is(NaturalLessThanTen(12), undef, '... this is not a NaturalLessThanTen');
48 is(NaturalLessThanTen(-5), undef, '... this is not a NaturalLessThanTen');
49 is(NaturalLessThanTen('Foo'), undef, '... this is not a NaturalLessThanTen');
53 my $negative = subtype Number => where { $_ < 0 };
54 ok(defined $negative, '... got a value back from negative');
55 isa_ok($negative, 'Mouse::Meta::TypeConstraint');
57 ok($negative->check(-5), '... this is a negative number');
58 ok(!defined($negative->check(5)), '... this is not a negative number');
59 is($negative->check('Foo'), undef, '... this is not a negative number');
61 ok($negative->is_subtype_of('Number'), '... $negative is a subtype of Number');
62 ok(!$negative->is_subtype_of('String'), '... $negative is not a subtype of String');
64 my $negative2 = subtype Number => where { $_ < 0 } => message {"$_ is not a negative number"};
66 ok(defined $negative2, '... got a value back from negative');
67 isa_ok($negative2, 'Mouse::Meta::TypeConstraint');
69 ok($negative2->check(-5), '... this is a negative number');
70 ok(!defined($negative2->check(5)), '... this is not a negative number');
71 is($negative2->check('Foo'), undef, '... this is not a negative number');
73 ok($negative2->is_subtype_of('Number'), '... $negative2 is a subtype of Number');
74 ok(!$negative2->is_subtype_of('String'), '... $negative is not a subtype of String');
76 ok($negative2->has_message, '... it has a message');
77 is($negative2->validate(2),
78 '2 is not a negative number',
79 '... validated unsuccessfully (got error)');
81 # check some meta-details
83 my $natural_less_than_ten = find_type_constraint('NaturalLessThanTen');
84 isa_ok($natural_less_than_ten, 'Mouse::Meta::TypeConstraint');
86 ok($natural_less_than_ten->is_subtype_of('Natural'), '... NaturalLessThanTen is subtype of Natural');
87 ok($natural_less_than_ten->is_subtype_of('Number'), '... NaturalLessThanTen is subtype of Number');
88 ok(!$natural_less_than_ten->is_subtype_of('String'), '... NaturalLessThanTen is not subtype of String');
90 ok($natural_less_than_ten->has_message, '... it has a message');
92 ok(!defined($natural_less_than_ten->validate(5)), '... validated successfully (no error)');
94 is($natural_less_than_ten->validate(15),
95 "The number '15' is not less than 10",
96 '... validated unsuccessfully (got error)');
98 my $natural = find_type_constraint('Natural');
99 isa_ok($natural, 'Mouse::Meta::TypeConstraint');
101 ok($natural->is_subtype_of('Number'), '... Natural is a subtype of Number');
102 ok(!$natural->is_subtype_of('String'), '... Natural is not a subtype of String');
104 ok(!$natural->has_message, '... it does not have a message');
106 ok(!defined($natural->validate(5)), '... validated successfully (no error)');
108 is($natural->validate(-5),
109 "Validation failed for 'Natural' failed with value -5",
110 '... validated unsuccessfully (got error)');
112 my $string = find_type_constraint('String');
113 isa_ok($string, 'Mouse::Meta::TypeConstraint');
115 ok($string->has_message, '... it does have a message');
117 ok(!defined($string->validate("Five")), '... validated successfully (no error)');
119 is($string->validate(5),
120 "This is not a string (5)",
121 '... validated unsuccessfully (got error)');
123 lives_ok { Mouse::Meta::Attribute->new('bob', isa => 'Spong') }
124 'meta-attr construction ok even when type constraint utils loaded first';
126 # Test type constraint predicate return values.
128 foreach 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");
132 # Test adding things which don't look like types to the registry throws an exception
134 my $r = Mouse::Util::TypeConstraints->get_type_constraint_registry;
135 throws_ok {$r->add_type_constraint()} qr/not a valid type constraint/, '->add_type_constraint(undef) throws';
136 throws_ok {$r->add_type_constraint('foo')} qr/not a valid type constraint/, '->add_type_constraint("foo") throws';
137 throws_ok {$r->add_type_constraint(bless {}, 'SomeClass')} qr/not a valid type constraint/, '->add_type_constraint(SomeClass->new) throws';
139 # Test some specific things that in the past did not work,
140 # specifically weird variations on anon subtypes.
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' );
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' );
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' );
166 # alternative sugar-less calling style which is documented as legit:
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' );
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' );
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' );
191 throws_ok { subtype 'Foo' } qr/cannot consist solely of a name/,
192 'Cannot call subtype with a single string argument';
195 # Back-compat for being called without sugar. Previously, calling with
196 # sugar was indistinguishable from calling directly.
199 my $type = type( 'Number2', sub { Scalar::Util::looks_like_number($_) } );
201 ok( $type->check(5), '... this is a Num' );
202 ok( ! $type->check('Foo'), '... this is not a Num' );
207 my $subtype = subtype( 'Number2', sub { $_ > 0 } );
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');
215 my $subtype = subtype( 'Natural2', 'Number2', sub { $_ > 0 } );
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');
223 my $subtype = subtype( 'Natural3', 'Number2' );
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');