Commit | Line | Data |
a15dff8d |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
e606ae5f |
6 | use Test::More tests => 44; |
a15dff8d |
7 | use Test::Exception; |
8 | |
9 | use Scalar::Util (); |
10 | |
11 | BEGIN { |
12 | use_ok('Moose::Util::TypeConstraints'); |
13 | } |
14 | |
5a4c5493 |
15 | type Number => where { Scalar::Util::looks_like_number($_) }; |
c899258b |
16 | type String |
17 | => where { !ref($_) && !Number($_) } |
18 | => message { "This is not a string ($_)" }; |
a15dff8d |
19 | |
20 | subtype Natural |
5a4c5493 |
21 | => as Number |
a15dff8d |
22 | => where { $_ > 0 }; |
23 | |
24 | subtype NaturalLessThanTen |
25 | => as Natural |
76d37e5a |
26 | => where { $_ < 10 } |
27 | => message { "The number '$_' is not less than 10" }; |
182134e8 |
28 | |
d9b40005 |
29 | Moose::Util::TypeConstraints->export_type_constraints_as_functions(); |
a15dff8d |
30 | |
5a4c5493 |
31 | ok(Number(5), '... this is a Num'); |
32 | ok(!defined(Number('Foo')), '... this is not a Num'); |
900466d6 |
33 | { |
34 | my $number_tc = Moose::Util::TypeConstraints::find_type_constraint('Number'); |
b644e331 |
35 | is("$number_tc", 'Number', '... type constraint stringifies to name'); |
900466d6 |
36 | } |
a15dff8d |
37 | |
5a4c5493 |
38 | ok(String('Foo'), '... this is a Str'); |
a15dff8d |
39 | ok(!defined(String(5)), '... this is not a Str'); |
40 | |
5a4c5493 |
41 | ok(Natural(5), '... this is a Natural'); |
a15dff8d |
42 | is(Natural(-5), undef, '... this is not a Natural'); |
43 | is(Natural('Foo'), undef, '... this is not a Natural'); |
44 | |
5a4c5493 |
45 | ok(NaturalLessThanTen(5), '... this is a NaturalLessThanTen'); |
a15dff8d |
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'); |
a15dff8d |
49 | |
50 | # anon sub-typing |
51 | |
5a4c5493 |
52 | my $negative = subtype Number => where { $_ < 0 }; |
a15dff8d |
53 | ok(defined $negative, '... got a value back from negative'); |
66811d63 |
54 | isa_ok($negative, 'Moose::Meta::TypeConstraint'); |
a15dff8d |
55 | |
5a4c5493 |
56 | ok($negative->check(-5), '... this is a negative number'); |
a27aa600 |
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 | |
cce8198b |
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 | |
76d37e5a |
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 | |
cce8198b |
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 | |
76d37e5a |
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 | |
cce8198b |
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 | |
76d37e5a |
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), |
688fcdda |
91 | "Validation failed for 'Natural' failed with value -5", |
76d37e5a |
92 | '... validated unsuccessfully (got error)'); |
a27aa600 |
93 | |
c899258b |
94 | my $string = find_type_constraint('String'); |
95 | isa_ok($string, 'Moose::Meta::TypeConstraint'); |
a27aa600 |
96 | |
c899258b |
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)'); |
8c4acc60 |
104 | |
105 | lives_ok { Moose::Meta::Attribute->new('bob', isa => 'Spong') } |
106 | 'meta-attr construction ok even when type constraint utils loaded first'; |
e606ae5f |
107 | |
108 | # Test type constraint predicate return values. |
109 | |
110 | foreach my $predicate (qw/equals is_subtype_of is_a_type_of/) { |
111 | ok( !defined $string->$predicate('DoesNotExist'), "$predicate predicate returns undef for non existant constraint"); |
112 | } |
113 | |
114 | # Test adding things which don't look like types to the registry throws an exception |
115 | |
116 | my $r = Moose::Util::TypeConstraints->get_type_constraint_registry; |
117 | throws_ok {$r->add_type_constraint()} qr/not a valid type constraint/, '->add_type_constraint(undef) throws'; |
118 | throws_ok {$r->add_type_constraint('foo')} qr/not a valid type constraint/, '->add_type_constraint("foo") throws'; |
119 | throws_ok {$r->add_type_constraint(bless {}, 'SomeClass')} qr/not a valid type constraint/, '->add_type_constraint(SomeClass->new) throws'; |
120 | |