6 use Test::More tests => 36;
12 use_ok('Moose::Util::TypeConstraints');
15 type Number => where { Scalar::Util::looks_like_number($_) };
17 => where { !ref($_) && !Number($_) }
18 => message { "This is not a string ($_)" };
24 subtype NaturalLessThanTen
27 => message { "The number '$_' is not less than 10" };
29 Moose::Util::TypeConstraints->export_type_contstraints_as_functions();
31 ok(Number(5), '... this is a Num');
32 ok(!defined(Number('Foo')), '... this is not a Num');
34 ok(String('Foo'), '... this is a Str');
35 ok(!defined(String(5)), '... this is not a Str');
37 ok(Natural(5), '... this is a Natural');
38 is(Natural(-5), undef, '... this is not a Natural');
39 is(Natural('Foo'), undef, '... this is not a Natural');
41 ok(NaturalLessThanTen(5), '... this is a NaturalLessThanTen');
42 is(NaturalLessThanTen(12), undef, '... this is not a NaturalLessThanTen');
43 is(NaturalLessThanTen(-5), undef, '... this is not a NaturalLessThanTen');
44 is(NaturalLessThanTen('Foo'), undef, '... this is not a NaturalLessThanTen');
48 my $negative = subtype Number => where { $_ < 0 };
49 ok(defined $negative, '... got a value back from negative');
50 isa_ok($negative, 'Moose::Meta::TypeConstraint');
52 ok($negative->check(-5), '... this is a negative number');
53 ok(!defined($negative->check(5)), '... this is not a negative number');
54 is($negative->check('Foo'), undef, '... this is not a negative number');
56 ok($negative->is_subtype_of('Number'), '... $negative is a subtype of Number');
57 ok(!$negative->is_subtype_of('String'), '... $negative is not a subtype of String');
59 # check some meta-details
61 my $natural_less_than_ten = find_type_constraint('NaturalLessThanTen');
62 isa_ok($natural_less_than_ten, 'Moose::Meta::TypeConstraint');
64 ok($natural_less_than_ten->is_subtype_of('Natural'), '... NaturalLessThanTen is subtype of Natural');
65 ok($natural_less_than_ten->is_subtype_of('Number'), '... NaturalLessThanTen is subtype of Number');
66 ok(!$natural_less_than_ten->is_subtype_of('String'), '... NaturalLessThanTen is not subtype of String');
68 ok($natural_less_than_ten->has_message, '... it has a message');
70 ok(!defined($natural_less_than_ten->validate(5)), '... validated successfully (no error)');
72 is($natural_less_than_ten->validate(15),
73 "The number '15' is not less than 10",
74 '... validated unsuccessfully (got error)');
76 my $natural = find_type_constraint('Natural');
77 isa_ok($natural, 'Moose::Meta::TypeConstraint');
79 ok($natural->is_subtype_of('Number'), '... Natural is a subtype of Number');
80 ok(!$natural->is_subtype_of('String'), '... Natural is not a subtype of String');
82 ok(!$natural->has_message, '... it does not have a message');
84 ok(!defined($natural->validate(5)), '... validated successfully (no error)');
86 is($natural->validate(-5),
87 "Validation failed for 'Natural' failed",
88 '... validated unsuccessfully (got error)');
90 my $string = find_type_constraint('String');
91 isa_ok($string, 'Moose::Meta::TypeConstraint');
93 ok($string->has_message, '... it does have a message');
95 ok(!defined($string->validate("Five")), '... validated successfully (no error)');
97 is($string->validate(5),
98 "This is not a string (5)",
99 '... validated unsuccessfully (got error)');