cleaning up
[gitmo/Moose.git] / t / 050_util_type_constraints.t
CommitLineData
a15dff8d 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
f658cac8 6use Test::More tests => 17;
a15dff8d 7use Test::Exception;
8
9use Scalar::Util ();
10
11BEGIN {
12 use_ok('Moose::Util::TypeConstraints');
13}
14
15type Num => where { Scalar::Util::looks_like_number($_) };
16type String => where { !ref($_) && !Num($_) };
17
18subtype Natural
19 => as Num
20 => where { $_ > 0 };
21
22subtype NaturalLessThanTen
23 => as Natural
24 => where { $_ < 10 };
182134e8 25
26Moose::Util::TypeConstraints::export_type_contstraints_as_functions();
a15dff8d 27
28is(Num(5), 5, '... this is a Num');
29ok(!defined(Num('Foo')), '... this is not a Num');
30
a15dff8d 31is(String('Foo'), 'Foo', '... this is a Str');
32ok(!defined(String(5)), '... this is not a Str');
33
a15dff8d 34is(Natural(5), 5, '... this is a Natural');
35is(Natural(-5), undef, '... this is not a Natural');
36is(Natural('Foo'), undef, '... this is not a Natural');
37
a15dff8d 38is(NaturalLessThanTen(5), 5, '... this is a NaturalLessThanTen');
39is(NaturalLessThanTen(12), undef, '... this is not a NaturalLessThanTen');
40is(NaturalLessThanTen(-5), undef, '... this is not a NaturalLessThanTen');
41is(NaturalLessThanTen('Foo'), undef, '... this is not a NaturalLessThanTen');
a15dff8d 42
43# anon sub-typing
44
45my $negative = subtype Num => where { $_ < 0 };
46ok(defined $negative, '... got a value back from negative');
66811d63 47isa_ok($negative, 'Moose::Meta::TypeConstraint');
a15dff8d 48
66811d63 49is($negative->_compiled_type_constraint->(-5), -5, '... this is a negative number');
50ok(!defined($negative->_compiled_type_constraint->(5)), '... this is not a negative number');
51is($negative->_compiled_type_constraint->('Foo'), undef, '... this is not a negative number');