From: Dave Rolsky Date: Thu, 28 Aug 2008 15:31:07 +0000 (+0000) Subject: Check that class name is actually a valid class name - add a test for this X-Git-Tag: 0.64_07~8 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=b3b7a216ebf4397fcea34f3deabc671d27fa1d01;p=gitmo%2FClass-MOP.git Check that class name is actually a valid class name - add a test for this --- diff --git a/lib/Class/MOP.pm b/lib/Class/MOP.pm index cbe2eae..74ac220 100644 --- a/lib/Class/MOP.pm +++ b/lib/Class/MOP.pm @@ -96,7 +96,10 @@ unless ($ENV{CLASS_MOP_NO_XS}) { sub load_class { my $class = shift; - if (ref($class) || !defined($class) || !length($class)) { + if ( ref($class) + || !defined($class) + || !length($class) + || $class !~ /^\w+(?::\w+)*$/ ) { my $display = defined($class) ? $class : 'undef'; confess "Invalid class name ($display)"; } diff --git a/t/083_load_class.t b/t/083_load_class.t index dc3ed5a..42d8994 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 => 14; +use Test::More tests => 15; use Test::Exception; require Class::MOP; @@ -14,6 +14,7 @@ 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+\)\)/; +throws_ok { Class::MOP::load_class('bogus name') } qr/Invalid class name \(bogus name\)/; my $meta = Class::MOP::load_class('BinaryTree'); ok($meta, "successfully loaded the class BinaryTree"); @@ -40,4 +41,7 @@ throws_ok { use constant foo => "bar"; } -lives_ok { ok(Class::MOP::is_class_loaded("Other")) } "a class with just constants is still a class"; +lives_ok { + ok( Class::MOP::is_class_loaded("Other"), 'is_class_loaded(Other)' ); +} +"a class with just constants is still a class";