6 use Test::More tests => 22;
12 use_ok('Moose::Util::TypeConstraints');
15 type Num => where { Scalar::Util::looks_like_number($_) };
16 type String => where { !ref($_) && !Num($_) };
22 subtype NaturalLessThanTen
26 is(Num(5), 5, '... this is a Num');
27 ok(!defined(Num('Foo')), '... this is not a Num');
29 is(&Num, &Num, '... the type w/out arguments just returns itself');
30 is(Num(), Num(), '... the type w/out arguments just returns itself');
32 is(String('Foo'), 'Foo', '... this is a Str');
33 ok(!defined(String(5)), '... this is not a Str');
35 is(&String, &String, '... the type w/out arguments just returns itself');
37 is(Natural(5), 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 is(&Natural, &Natural, '... the type w/out arguments just returns itself');
43 is(NaturalLessThanTen(5), 5, '... this is a NaturalLessThanTen');
44 is(NaturalLessThanTen(12), undef, '... this is not a NaturalLessThanTen');
45 is(NaturalLessThanTen(-5), undef, '... this is not a NaturalLessThanTen');
46 is(NaturalLessThanTen('Foo'), undef, '... this is not a NaturalLessThanTen');
48 is(&NaturalLessThanTen, &NaturalLessThanTen,
49 '... the type w/out arguments just returns itself');
53 my $negative = subtype Num => where { $_ < 0 };
54 ok(defined $negative, '... got a value back from negative');
55 is(ref($negative), 'CODE', '... got a type constraint back from negative');
57 is($negative->(-5), -5, '... this is a negative number');
58 ok(!defined($negative->(5)), '... this is not a negative number');
59 is($negative->('Foo'), undef, '... this is not a negative number');