X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F040_type_constraints%2F001_util_type_constraints.t;h=b09ada8f59542d49b4f0fa04566b48ebe8d44e9d;hb=6b83828f50414e18c31109c8c790158b7c0a7820;hp=20ceae6de3789058fcd83dfb410ef9dbc579ffd1;hpb=9758af8af0c362eebb20cf2501eac3f734d7fef3;p=gitmo%2FMoose.git diff --git a/t/040_type_constraints/001_util_type_constraints.t b/t/040_type_constraints/001_util_type_constraints.t index 20ceae6..b09ada8 100644 --- a/t/040_type_constraints/001_util_type_constraints.t +++ b/t/040_type_constraints/001_util_type_constraints.t @@ -3,14 +3,13 @@ use strict; use warnings; -use Test::More tests => 44; +use Test::More tests => 52; use Test::Exception; use Scalar::Util (); -BEGIN { - use_ok('Moose::Util::TypeConstraints'); -} +use Moose::Util::TypeConstraints; + type Number => where { Scalar::Util::looks_like_number($_) }; type String @@ -18,14 +17,14 @@ type String => message { "This is not a string ($_)" }; subtype Natural - => as Number - => where { $_ > 0 }; + => as Number + => where { $_ > 0 }; subtype NaturalLessThanTen - => as Natural - => where { $_ < 10 } - => message { "The number '$_' is not less than 10" }; - + => as Natural + => where { $_ < 10 } + => message { "The number '$_' is not less than 10" }; + Moose::Util::TypeConstraints->export_type_constraints_as_functions(); ok(Number(5), '... this is a Num'); @@ -46,10 +45,10 @@ ok(NaturalLessThanTen(5), '... this is a NaturalLessThanTen'); is(NaturalLessThanTen(12), undef, '... this is not a NaturalLessThanTen'); is(NaturalLessThanTen(-5), undef, '... this is not a NaturalLessThanTen'); is(NaturalLessThanTen('Foo'), undef, '... this is not a NaturalLessThanTen'); - -# anon sub-typing - -my $negative = subtype Number => where { $_ < 0 }; + +# anon sub-typing + +my $negative = subtype Number => where { $_ < 0 }; ok(defined $negative, '... got a value back from negative'); isa_ok($negative, 'Moose::Meta::TypeConstraint'); @@ -60,6 +59,23 @@ is($negative->check('Foo'), undef, '... this is not a negative number'); ok($negative->is_subtype_of('Number'), '... $negative is a subtype of Number'); ok(!$negative->is_subtype_of('String'), '... $negative is not a subtype of String'); +my $negative2 = subtype Number => where { $_ < 0 } => message {"$_ is not a negative number"}; + +ok(defined $negative2, '... got a value back from negative'); +isa_ok($negative2, 'Moose::Meta::TypeConstraint'); + +ok($negative2->check(-5), '... this is a negative number'); +ok(!defined($negative2->check(5)), '... this is not a negative number'); +is($negative2->check('Foo'), undef, '... this is not a negative number'); + +ok($negative2->is_subtype_of('Number'), '... $negative2 is a subtype of Number'); +ok(!$negative2->is_subtype_of('String'), '... $negative is not a subtype of String'); + +ok($negative2->has_message, '... it has a message'); +is($negative2->validate(2), + '2 is not a negative number', + '... validated unsuccessfully (got error)'); + # check some meta-details my $natural_less_than_ten = find_type_constraint('NaturalLessThanTen');