adding ->parent_registry to the TC registry object
[gitmo/Moose.git] / t / 040_type_constraints / 001_util_type_constraints.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 38;
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 
17     => where { !ref($_) && !Number($_) }
18     => message { "This is not a string ($_)" };
19
20 subtype Natural 
21         => as Number 
22         => where { $_ > 0 };
23
24 subtype NaturalLessThanTen 
25         => as Natural
26         => where { $_ < 10 }
27         => message { "The number '$_' is not less than 10" };
28         
29 Moose::Util::TypeConstraints->export_type_contstraints_as_functions();
30
31 ok(Number(5), '... this is a Num');
32 ok(!defined(Number('Foo')), '... this is not a Num');
33 {
34     my $number_tc = Moose::Util::TypeConstraints::find_type_constraint('Number');
35     is("$number_tc", 'Number', '... type constraint stringifies to name');
36 }
37
38 ok(String('Foo'), '... this is a Str');
39 ok(!defined(String(5)), '... this is not a Str');
40
41 ok(Natural(5), '... this is a Natural');
42 is(Natural(-5), undef, '... this is not a Natural');
43 is(Natural('Foo'), undef, '... this is not a Natural');
44
45 ok(NaturalLessThanTen(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 # anon sub-typing       
51         
52 my $negative = subtype Number => where  { $_ < 0 };
53 ok(defined $negative, '... got a value back from negative');
54 isa_ok($negative, 'Moose::Meta::TypeConstraint');
55
56 ok($negative->check(-5), '... this is a negative number');
57 ok(!defined($negative->check(5)), '... this is not a negative number');
58 is($negative->check('Foo'), undef, '... this is not a negative number');
59
60 ok($negative->is_subtype_of('Number'), '... $negative is a subtype of Number');
61 ok(!$negative->is_subtype_of('String'), '... $negative is not a subtype of String');
62
63 # check some meta-details
64
65 my $natural_less_than_ten = find_type_constraint('NaturalLessThanTen');
66 isa_ok($natural_less_than_ten, 'Moose::Meta::TypeConstraint');
67
68 ok($natural_less_than_ten->is_subtype_of('Natural'), '... NaturalLessThanTen is subtype of Natural');
69 ok($natural_less_than_ten->is_subtype_of('Number'), '... NaturalLessThanTen is subtype of Number');
70 ok(!$natural_less_than_ten->is_subtype_of('String'), '... NaturalLessThanTen is not subtype of String');
71
72 ok($natural_less_than_ten->has_message, '... it has a message');
73
74 ok(!defined($natural_less_than_ten->validate(5)), '... validated successfully (no error)');
75
76 is($natural_less_than_ten->validate(15), 
77    "The number '15' is not less than 10", 
78    '... validated unsuccessfully (got error)');
79
80 my $natural = find_type_constraint('Natural');
81 isa_ok($natural, 'Moose::Meta::TypeConstraint');
82
83 ok($natural->is_subtype_of('Number'), '... Natural is a subtype of Number');
84 ok(!$natural->is_subtype_of('String'), '... Natural is not a subtype of String');
85
86 ok(!$natural->has_message, '... it does not have a message');
87
88 ok(!defined($natural->validate(5)), '... validated successfully (no error)');
89
90 is($natural->validate(-5), 
91   "Validation failed for 'Natural' failed", 
92   '... validated unsuccessfully (got error)');
93
94 my $string = find_type_constraint('String');
95 isa_ok($string, 'Moose::Meta::TypeConstraint');
96
97 ok($string->has_message, '... it does have a message');
98
99 ok(!defined($string->validate("Five")), '... validated successfully (no error)');
100
101 is($string->validate(5), 
102 "This is not a string (5)", 
103 '... validated unsuccessfully (got error)');
104
105 lives_ok { Moose::Meta::Attribute->new('bob', isa => 'Spong') }
106   'meta-attr construction ok even when type constraint utils loaded first';