types are no string, you can export if you want
[gitmo/Moose.git] / t / 050_util_type_constraints.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 22;
7 use Test::Exception;
8
9 use Scalar::Util ();
10
11 BEGIN {
12     use_ok('Moose::Util::TypeConstraints');           
13 }
14
15 type Num => where { Scalar::Util::looks_like_number($_) };
16 type String => where { !ref($_) && !Num($_) };
17
18 subtype Natural 
19         => as Num 
20         => where { $_ > 0 };
21
22 subtype NaturalLessThanTen 
23         => as Natural
24         => where { $_ < 10 };
25         
26 Moose::Util::TypeConstraints::export_type_contstraints_as_functions();
27
28 is(Num(5), 5, '... this is a Num');
29 ok(!defined(Num('Foo')), '... this is not a Num');
30
31 is(&Num, &Num, '... the type w/out arguments just returns itself');
32 is(Num(), Num(), '... the type w/out arguments just returns itself');
33
34 is(String('Foo'), 'Foo', '... this is a Str');
35 ok(!defined(String(5)), '... this is not a Str');
36
37 is(&String, &String, '... the type w/out arguments just returns itself');
38
39 is(Natural(5), 5, '... this is a Natural');
40 is(Natural(-5), undef, '... this is not a Natural');
41 is(Natural('Foo'), undef, '... this is not a Natural');
42
43 is(&Natural, &Natural, '... the type w/out arguments just returns itself');
44
45 is(NaturalLessThanTen(5), 5, '... this is a NaturalLessThanTen');
46 is(NaturalLessThanTen(12), undef, '... this is not a NaturalLessThanTen');
47 is(NaturalLessThanTen(-5), undef, '... this is not a NaturalLessThanTen');
48 is(NaturalLessThanTen('Foo'), undef, '... this is not a NaturalLessThanTen');
49
50 is(&NaturalLessThanTen, &NaturalLessThanTen, 
51         '... the type w/out arguments just returns itself');
52         
53 # anon sub-typing       
54         
55 my $negative = subtype Num => where     { $_ < 0 };
56 ok(defined $negative, '... got a value back from negative');
57 is(ref($negative), 'CODE', '... got a type constraint back from negative');
58
59 is($negative->(-5), -5, '... this is a negative number');
60 ok(!defined($negative->(5)), '... this is not a negative number');
61 is($negative->('Foo'), undef, '... this is not a negative number');