6 use Test::More tests => 25;
12 use_ok('Moose::Util::TypeConstraints');
15 type Number => where { Scalar::Util::looks_like_number($_) };
16 type String => where { !ref($_) && !Num($_) };
22 subtype NaturalLessThanTen
25 => message { "The number '$_' is not less than 10" };
27 Moose::Util::TypeConstraints->export_type_contstraints_as_functions();
29 ok(Number(5), '... this is a Num');
30 ok(!defined(Number('Foo')), '... this is not a Num');
32 ok(String('Foo'), '... this is a Str');
33 ok(!defined(String(5)), '... this is not a Str');
35 ok(Natural(5), '... this is a Natural');
36 is(Natural(-5), undef, '... this is not a Natural');
37 is(Natural('Foo'), undef, '... this is not a Natural');
39 ok(NaturalLessThanTen(5), '... this is a NaturalLessThanTen');
40 is(NaturalLessThanTen(12), undef, '... this is not a NaturalLessThanTen');
41 is(NaturalLessThanTen(-5), undef, '... this is not a NaturalLessThanTen');
42 is(NaturalLessThanTen('Foo'), undef, '... this is not a NaturalLessThanTen');
46 my $negative = subtype Number => where { $_ < 0 };
47 ok(defined $negative, '... got a value back from negative');
48 isa_ok($negative, 'Moose::Meta::TypeConstraint');
50 ok($negative->check(-5), '... this is a negative number');
51 ok(!defined($negative->check(5)), '... this is not a negative number');
52 is($negative->check('Foo'), undef, '... this is not a negative number');
54 # check some meta-details
56 my $natural_less_than_ten = find_type_constraint('NaturalLessThanTen');
57 isa_ok($natural_less_than_ten, 'Moose::Meta::TypeConstraint');
59 ok($natural_less_than_ten->has_message, '... it has a message');
61 ok(!defined($natural_less_than_ten->validate(5)), '... validated successfully (no error)');
63 is($natural_less_than_ten->validate(15),
64 "The number '15' is not less than 10",
65 '... validated unsuccessfully (got error)');
67 my $natural = find_type_constraint('Natural');
68 isa_ok($natural, 'Moose::Meta::TypeConstraint');
70 ok(!$natural->has_message, '... it does not have a message');
72 ok(!defined($natural->validate(5)), '... validated successfully (no error)');
74 is($natural->validate(-5),
75 "Validation failed for 'Natural' failed.",
76 '... validated unsuccessfully (got error)');