From: Dave Rolsky Date: Thu, 28 Aug 2008 15:52:45 +0000 (+0000) Subject: When checking for a valid class name, actually use the right regex X-Git-Tag: 0.64_07~6 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=2c0fb06489b9f2facbb37d2ebdc825f034ec0c5f;p=gitmo%2FClass-MOP.git When checking for a valid class name, actually use the right regex (doh). Split out valid name checking for easier testing, and added more tests for this. --- diff --git a/lib/Class/MOP.pm b/lib/Class/MOP.pm index 74ac220..948d5ef 100644 --- a/lib/Class/MOP.pm +++ b/lib/Class/MOP.pm @@ -96,10 +96,7 @@ unless ($ENV{CLASS_MOP_NO_XS}) { sub load_class { my $class = shift; - if ( ref($class) - || !defined($class) - || !length($class) - || $class !~ /^\w+(?::\w+)*$/ ) { + unless ( _is_valid_class_name($class) ) { my $display = defined($class) ? $class : 'undef'; confess "Invalid class name ($display)"; } @@ -120,6 +117,18 @@ sub load_class { return get_metaclass_by_name($class) if defined wantarray; } +sub _is_valid_class_name { + my $class = shift; + + return 0 if ref($class); + return 0 unless defined($class); + return 0 unless length($class); + + return 1 if $class =~ /^\w+(?:::\w+)*$/; + + return 0; +} + sub is_class_loaded { my $class = shift; diff --git a/t/083_load_class.t b/t/083_load_class.t index 42d8994..b248686 100644 --- a/t/083_load_class.t +++ b/t/083_load_class.t @@ -1,7 +1,7 @@ #!/usr/bin/env perl use strict; use warnings; -use Test::More tests => 15; +use Test::More tests => 19; use Test::Exception; require Class::MOP; @@ -11,9 +11,13 @@ ok(!Class::MOP::is_class_loaded(), "is_class_loaded with no argument returns fal ok(!Class::MOP::is_class_loaded(''), "can't load the empty class"); ok(!Class::MOP::is_class_loaded(\"foo"), "can't load a class name reference??"); -throws_ok { Class::MOP::load_class() } qr/Invalid class name \(undef\)/; -throws_ok { Class::MOP::load_class('') } qr/Invalid class name \(\)/; -throws_ok { Class::MOP::load_class(\"foo") } qr/Invalid class name \(SCALAR\(\w+\)\)/; +ok(!Class::MOP::_is_valid_class_name(undef), 'undef is not a valid class name'); +ok(!Class::MOP::_is_valid_class_name(''), 'empty string is not a valid class name'); +ok(!Class::MOP::_is_valid_class_name(\"foo"), 'a reference is not a valid class name'); +ok(!Class::MOP::_is_valid_class_name('bogus name'), q{'bogus name' is not a valid class name}); +ok(Class::MOP::_is_valid_class_name('Foo'), q{'Foo' is a valid class name}); +ok(Class::MOP::_is_valid_class_name('Foo::Bar'), q{'Foo::Bar' is a valid class name}); +ok(Class::MOP::_is_valid_class_name('Foo_::Bar2'), q{'Foo_::Bar2' is a valid class name}); throws_ok { Class::MOP::load_class('bogus name') } qr/Invalid class name \(bogus name\)/; my $meta = Class::MOP::load_class('BinaryTree'); @@ -42,6 +46,6 @@ throws_ok { } lives_ok { - ok( Class::MOP::is_class_loaded("Other"), 'is_class_loaded(Other)' ); + ok(Class::MOP::is_class_loaded("Other"), 'is_class_loaded(Other)'); } "a class with just constants is still a class";