From: Dave Rolsky Date: Wed, 25 Feb 2009 19:55:15 +0000 (+0000) Subject: Catch calls to subtype that just provide a name (and no parent) and barf on them. X-Git-Tag: 0.72_01~96 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=f75f625dfbf7c765bdfa127a59b49a4503344298;p=gitmo%2FMoose.git Catch calls to subtype that just provide a name (and no parent) and barf on them. --- diff --git a/lib/Moose/Util/TypeConstraints.pm b/lib/Moose/Util/TypeConstraints.pm index 5cd340d..9dd4ff1 100644 --- a/lib/Moose/Util/TypeConstraints.pm +++ b/lib/Moose/Util/TypeConstraints.pm @@ -288,6 +288,10 @@ sub subtype { return _create_type_constraint(@_); } + if ( @_ == 1 && ! ref $_[0] ) { + __PACKAGE__->_throw_error('A subtype cannot consist solely of a name, it must have a parent'); + } + # The blessed check is mostly to accommodate MooseX::Types, which # uses an object which overloads stringification as a type name. my $name = ref $_[0] && ! blessed $_[0] ? undef : shift; diff --git a/t/040_type_constraints/001_util_type_constraints.t b/t/040_type_constraints/001_util_type_constraints.t index b9e044c..b57a4e5 100644 --- a/t/040_type_constraints/001_util_type_constraints.t +++ b/t/040_type_constraints/001_util_type_constraints.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 84; +use Test::More tests => 85; use Test::Exception; use Scalar::Util (); @@ -148,7 +148,7 @@ throws_ok {$r->add_type_constraint(bless {}, 'SomeClass')} qr/not a valid type c } { - my $subtype = subtype 'ArrayRef[Num|Str]'; + my $subtype = subtype as 'ArrayRef[Num|Str]'; isa_ok( $subtype, 'Moose::Meta::TypeConstraint', 'got an anon subtype' ); is( $subtype->parent->name, 'ArrayRef[Num|Str]', 'parent is ArrayRef[Num|Str]' ); ok( ! $subtype->has_message, 'subtype has no message' ); @@ -185,6 +185,10 @@ throws_ok {$r->add_type_constraint(bless {}, 'SomeClass')} qr/not a valid type c ok( ! $subtype->check('Foo'), 'constraint reject Foo' ); } +{ + throws_ok { subtype 'Foo' } qr/cannot consist solely of a name/, + 'Cannot call subtype with a single string argument'; +} # Back-compat for being called without sugar. Previously, calling with # sugar was indistinguishable from calling directly.