merge trunk to pluggable errors
[gitmo/Moose.git] / t / 040_type_constraints / 001_util_type_constraints.t
CommitLineData
a15dff8d 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
e606ae5f 6use Test::More tests => 44;
a15dff8d 7use Test::Exception;
8
9use Scalar::Util ();
10
11BEGIN {
12 use_ok('Moose::Util::TypeConstraints');
13}
14
5a4c5493 15type Number => where { Scalar::Util::looks_like_number($_) };
c899258b 16type String
17 => where { !ref($_) && !Number($_) }
18 => message { "This is not a string ($_)" };
a15dff8d 19
20subtype Natural
5a4c5493 21 => as Number
a15dff8d 22 => where { $_ > 0 };
23
24subtype NaturalLessThanTen
25 => as Natural
76d37e5a 26 => where { $_ < 10 }
27 => message { "The number '$_' is not less than 10" };
182134e8 28
d9b40005 29Moose::Util::TypeConstraints->export_type_constraints_as_functions();
a15dff8d 30
5a4c5493 31ok(Number(5), '... this is a Num');
32ok(!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 38ok(String('Foo'), '... this is a Str');
a15dff8d 39ok(!defined(String(5)), '... this is not a Str');
40
5a4c5493 41ok(Natural(5), '... this is a Natural');
a15dff8d 42is(Natural(-5), undef, '... this is not a Natural');
43is(Natural('Foo'), undef, '... this is not a Natural');
44
5a4c5493 45ok(NaturalLessThanTen(5), '... this is a NaturalLessThanTen');
a15dff8d 46is(NaturalLessThanTen(12), undef, '... this is not a NaturalLessThanTen');
47is(NaturalLessThanTen(-5), undef, '... this is not a NaturalLessThanTen');
48is(NaturalLessThanTen('Foo'), undef, '... this is not a NaturalLessThanTen');
a15dff8d 49
50# anon sub-typing
51
5a4c5493 52my $negative = subtype Number => where { $_ < 0 };
a15dff8d 53ok(defined $negative, '... got a value back from negative');
66811d63 54isa_ok($negative, 'Moose::Meta::TypeConstraint');
a15dff8d 55
5a4c5493 56ok($negative->check(-5), '... this is a negative number');
a27aa600 57ok(!defined($negative->check(5)), '... this is not a negative number');
58is($negative->check('Foo'), undef, '... this is not a negative number');
59
cce8198b 60ok($negative->is_subtype_of('Number'), '... $negative is a subtype of Number');
61ok(!$negative->is_subtype_of('String'), '... $negative is not a subtype of String');
62
76d37e5a 63# check some meta-details
64
65my $natural_less_than_ten = find_type_constraint('NaturalLessThanTen');
66isa_ok($natural_less_than_ten, 'Moose::Meta::TypeConstraint');
67
cce8198b 68ok($natural_less_than_ten->is_subtype_of('Natural'), '... NaturalLessThanTen is subtype of Natural');
69ok($natural_less_than_ten->is_subtype_of('Number'), '... NaturalLessThanTen is subtype of Number');
70ok(!$natural_less_than_ten->is_subtype_of('String'), '... NaturalLessThanTen is not subtype of String');
71
76d37e5a 72ok($natural_less_than_ten->has_message, '... it has a message');
73
74ok(!defined($natural_less_than_ten->validate(5)), '... validated successfully (no error)');
75
76is($natural_less_than_ten->validate(15),
77 "The number '15' is not less than 10",
78 '... validated unsuccessfully (got error)');
79
80my $natural = find_type_constraint('Natural');
81isa_ok($natural, 'Moose::Meta::TypeConstraint');
82
cce8198b 83ok($natural->is_subtype_of('Number'), '... Natural is a subtype of Number');
84ok(!$natural->is_subtype_of('String'), '... Natural is not a subtype of String');
85
76d37e5a 86ok(!$natural->has_message, '... it does not have a message');
87
88ok(!defined($natural->validate(5)), '... validated successfully (no error)');
89
90is($natural->validate(-5),
688fcdda 91 "Validation failed for 'Natural' failed with value -5",
76d37e5a 92 '... validated unsuccessfully (got error)');
a27aa600 93
c899258b 94my $string = find_type_constraint('String');
95isa_ok($string, 'Moose::Meta::TypeConstraint');
a27aa600 96
c899258b 97ok($string->has_message, '... it does have a message');
98
99ok(!defined($string->validate("Five")), '... validated successfully (no error)');
100
101is($string->validate(5),
102"This is not a string (5)",
103'... validated unsuccessfully (got error)');
8c4acc60 104
105lives_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
110foreach 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
116my $r = Moose::Util::TypeConstraints->get_type_constraint_registry;
117throws_ok {$r->add_type_constraint()} qr/not a valid type constraint/, '->add_type_constraint(undef) throws';
118throws_ok {$r->add_type_constraint('foo')} qr/not a valid type constraint/, '->add_type_constraint("foo") throws';
119throws_ok {$r->add_type_constraint(bless {}, 'SomeClass')} qr/not a valid type constraint/, '->add_type_constraint(SomeClass->new) throws';
120