Fix minor spelling error
[gitmo/Moose.git] / t / 050_util_type_constraints.t
CommitLineData
a15dff8d 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
c899258b 6use Test::More tests => 36;
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
7c13858b 29Moose::Util::TypeConstraints->export_type_contstraints_as_functions();
a15dff8d 30
5a4c5493 31ok(Number(5), '... this is a Num');
32ok(!defined(Number('Foo')), '... this is not a Num');
a15dff8d 33
5a4c5493 34ok(String('Foo'), '... this is a Str');
a15dff8d 35ok(!defined(String(5)), '... this is not a Str');
36
5a4c5493 37ok(Natural(5), '... this is a Natural');
a15dff8d 38is(Natural(-5), undef, '... this is not a Natural');
39is(Natural('Foo'), undef, '... this is not a Natural');
40
5a4c5493 41ok(NaturalLessThanTen(5), '... this is a NaturalLessThanTen');
a15dff8d 42is(NaturalLessThanTen(12), undef, '... this is not a NaturalLessThanTen');
43is(NaturalLessThanTen(-5), undef, '... this is not a NaturalLessThanTen');
44is(NaturalLessThanTen('Foo'), undef, '... this is not a NaturalLessThanTen');
a15dff8d 45
46# anon sub-typing
47
5a4c5493 48my $negative = subtype Number => where { $_ < 0 };
a15dff8d 49ok(defined $negative, '... got a value back from negative');
66811d63 50isa_ok($negative, 'Moose::Meta::TypeConstraint');
a15dff8d 51
5a4c5493 52ok($negative->check(-5), '... this is a negative number');
a27aa600 53ok(!defined($negative->check(5)), '... this is not a negative number');
54is($negative->check('Foo'), undef, '... this is not a negative number');
55
cce8198b 56ok($negative->is_subtype_of('Number'), '... $negative is a subtype of Number');
57ok(!$negative->is_subtype_of('String'), '... $negative is not a subtype of String');
58
76d37e5a 59# check some meta-details
60
61my $natural_less_than_ten = find_type_constraint('NaturalLessThanTen');
62isa_ok($natural_less_than_ten, 'Moose::Meta::TypeConstraint');
63
cce8198b 64ok($natural_less_than_ten->is_subtype_of('Natural'), '... NaturalLessThanTen is subtype of Natural');
65ok($natural_less_than_ten->is_subtype_of('Number'), '... NaturalLessThanTen is subtype of Number');
66ok(!$natural_less_than_ten->is_subtype_of('String'), '... NaturalLessThanTen is not subtype of String');
67
76d37e5a 68ok($natural_less_than_ten->has_message, '... it has a message');
69
70ok(!defined($natural_less_than_ten->validate(5)), '... validated successfully (no error)');
71
72is($natural_less_than_ten->validate(15),
73 "The number '15' is not less than 10",
74 '... validated unsuccessfully (got error)');
75
76my $natural = find_type_constraint('Natural');
77isa_ok($natural, 'Moose::Meta::TypeConstraint');
78
cce8198b 79ok($natural->is_subtype_of('Number'), '... Natural is a subtype of Number');
80ok(!$natural->is_subtype_of('String'), '... Natural is not a subtype of String');
81
76d37e5a 82ok(!$natural->has_message, '... it does not have a message');
83
84ok(!defined($natural->validate(5)), '... validated successfully (no error)');
85
86is($natural->validate(-5),
451c8248 87 "Validation failed for 'Natural' failed",
76d37e5a 88 '... validated unsuccessfully (got error)');
a27aa600 89
c899258b 90my $string = find_type_constraint('String');
91isa_ok($string, 'Moose::Meta::TypeConstraint');
a27aa600 92
c899258b 93ok($string->has_message, '... it does have a message');
94
95ok(!defined($string->validate("Five")), '... validated successfully (no error)');
96
97is($string->validate(5),
98"This is not a string (5)",
99'... validated unsuccessfully (got error)');