it-works
[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 => 17;
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(String('Foo'), 'Foo', '... this is a Str');
32 ok(!defined(String(5)), '... this is not a Str');
33
34 is(Natural(5), 5, '... this is a Natural');
35 is(Natural(-5), undef, '... this is not a Natural');
36 is(Natural('Foo'), undef, '... this is not a Natural');
37
38 is(NaturalLessThanTen(5), 5, '... this is a NaturalLessThanTen');
39 is(NaturalLessThanTen(12), undef, '... this is not a NaturalLessThanTen');
40 is(NaturalLessThanTen(-5), undef, '... this is not a NaturalLessThanTen');
41 is(NaturalLessThanTen('Foo'), undef, '... this is not a NaturalLessThanTen');
42         
43 # anon sub-typing       
44         
45 my $negative = subtype Num => where     { $_ < 0 };
46 ok(defined $negative, '... got a value back from negative');
47 isa_ok($negative, 'Moose::Meta::TypeConstraint');
48
49 is($negative->_compiled_type_constraint->(-5), -5, '... this is a negative number');
50 ok(!defined($negative->_compiled_type_constraint->(5)), '... this is not a negative number');
51 is($negative->_compiled_type_constraint->('Foo'), undef, '... this is not a negative number');