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=72cfc3c39ee1c4bf80c5e6316e14f80d2e7d86de;hpb=43a41edeb57c090ba7f7f2ad44e7d31c09edbefc;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 72cfc3c..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 => 38; +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,21 +17,21 @@ 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'); ok(!defined(Number('Foo')), '... this is not a Num'); { my $number_tc = Moose::Util::TypeConstraints::find_type_constraint('Number'); - is($number_tc->name, 'Number', '... type constraint stringifies to name'); + is("$number_tc", 'Number', '... type constraint stringifies to name'); } ok(String('Foo'), '... this is a Str'); @@ -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'); @@ -88,7 +104,7 @@ ok(!$natural->has_message, '... it does not have a message'); ok(!defined($natural->validate(5)), '... validated successfully (no error)'); is($natural->validate(-5), - "Validation failed for 'Natural' failed", + "Validation failed for 'Natural' failed with value -5", '... validated unsuccessfully (got error)'); my $string = find_type_constraint('String'); @@ -104,3 +120,17 @@ is($string->validate(5), lives_ok { Moose::Meta::Attribute->new('bob', isa => 'Spong') } 'meta-attr construction ok even when type constraint utils loaded first'; + +# Test type constraint predicate return values. + +foreach my $predicate (qw/equals is_subtype_of is_a_type_of/) { + ok( !defined $string->$predicate('DoesNotExist'), "$predicate predicate returns undef for non existant constraint"); +} + +# Test adding things which don't look like types to the registry throws an exception + +my $r = Moose::Util::TypeConstraints->get_type_constraint_registry; +throws_ok {$r->add_type_constraint()} qr/not a valid type constraint/, '->add_type_constraint(undef) throws'; +throws_ok {$r->add_type_constraint('foo')} qr/not a valid type constraint/, '->add_type_constraint("foo") throws'; +throws_ok {$r->add_type_constraint(bless {}, 'SomeClass')} qr/not a valid type constraint/, '->add_type_constraint(SomeClass->new) throws'; +