added default {} keyword
[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 => 32;
7 use Test::Exception;
8
9 use Scalar::Util ();
10
11 BEGIN {
12     use_ok('Moose::Util::TypeConstraints');           
13 }
14
15 type Number => where { Scalar::Util::looks_like_number($_) };
16 type String => where { !ref($_) && !Number($_) };
17
18 subtype Natural 
19         => as Number 
20         => where { $_ > 0 };
21
22 subtype NaturalLessThanTen 
23         => as Natural
24         => where { $_ < 10 }
25         => message { "The number '$_' is not less than 10" };
26         
27 Moose::Util::TypeConstraints->export_type_contstraints_as_functions();
28
29 ok(Number(5), '... this is a Num');
30 ok(!defined(Number('Foo')), '... this is not a Num');
31
32 ok(String('Foo'), '... this is a Str');
33 ok(!defined(String(5)), '... this is not a Str');
34
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');
38
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');
43         
44 # anon sub-typing       
45         
46 my $negative = subtype Number => where  { $_ < 0 };
47 ok(defined $negative, '... got a value back from negative');
48 isa_ok($negative, 'Moose::Meta::TypeConstraint');
49
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');
53
54 ok($negative->is_subtype_of('Number'), '... $negative is a subtype of Number');
55 ok(!$negative->is_subtype_of('String'), '... $negative is not a subtype of String');
56
57 # check some meta-details
58
59 my $natural_less_than_ten = find_type_constraint('NaturalLessThanTen');
60 isa_ok($natural_less_than_ten, 'Moose::Meta::TypeConstraint');
61
62 ok($natural_less_than_ten->is_subtype_of('Natural'), '... NaturalLessThanTen is subtype of Natural');
63 ok($natural_less_than_ten->is_subtype_of('Number'), '... NaturalLessThanTen is subtype of Number');
64 ok(!$natural_less_than_ten->is_subtype_of('String'), '... NaturalLessThanTen is not subtype of String');
65
66 ok($natural_less_than_ten->has_message, '... it has a message');
67
68 ok(!defined($natural_less_than_ten->validate(5)), '... validated successfully (no error)');
69
70 is($natural_less_than_ten->validate(15), 
71    "The number '15' is not less than 10", 
72    '... validated unsuccessfully (got error)');
73
74 my $natural = find_type_constraint('Natural');
75 isa_ok($natural, 'Moose::Meta::TypeConstraint');
76
77 ok($natural->is_subtype_of('Number'), '... Natural is a subtype of Number');
78 ok(!$natural->is_subtype_of('String'), '... Natural is not a subtype of String');
79
80 ok(!$natural->has_message, '... it does not have a message');
81
82 ok(!defined($natural->validate(5)), '... validated successfully (no error)');
83
84 is($natural->validate(-5), 
85   "Validation failed for 'Natural' failed", 
86   '... validated unsuccessfully (got error)');
87
88