From: Shawn M Moore Date: Wed, 7 Jan 2009 14:24:28 +0000 (+0000) Subject: Implement get_meta, begin writing tests for it X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=a92964808b74587979927fcc15b4cd18817f9aaa;p=gitmo%2FClass-MOP.git Implement get_meta, begin writing tests for it --- diff --git a/lib/Class/MOP.pm b/lib/Class/MOP.pm index 1efe34e..20ae1bf 100644 --- a/lib/Class/MOP.pm +++ b/lib/Class/MOP.pm @@ -9,7 +9,7 @@ use 5.008; use MRO::Compat; use Carp 'confess'; -use Scalar::Util 'weaken'; +use Scalar::Util qw/weaken blessed/; use Class::MOP::Class; @@ -64,7 +64,7 @@ sub _try_load_xs { die $e if $e && $e !~ /object version|loadable object/; return $e ? 0 : 1; -} + } sub _load_pure_perl { require Sub::Identify; @@ -93,6 +93,8 @@ sub _load_pure_perl { sub does_metaclass_exist { exists $METAS{$_[0]} && defined $METAS{$_[0]} } sub remove_metaclass_by_name { $METAS{$_[0]} = undef } + sub get_meta { $METAS{ blessed($_[0]) || $_[0] } } + # NOTE: # We only cache metaclasses, meaning instances of # Class::MOP::Class. We do not cache instance of diff --git a/t/084_get_meta.t b/t/084_get_meta.t new file mode 100644 index 0000000..e0ae31d --- /dev/null +++ b/t/084_get_meta.t @@ -0,0 +1,12 @@ +use strict; +use warnings; +use Test::More tests => 4; + +require Class::MOP; + +is(Class::MOP::get_meta('Does::Not::Exist'), undef, "... get_meta on a nonexistent class returns undef"); +is(Class::MOP::get_meta(bless {}, 'Does::Not::Exist'), undef, "... get_meta on an instance of a nonexistent class returns undef"); + +is(Class::MOP::get_meta('Class::MOP::Class'), Class::MOP::Class->meta, "... get_meta on Class::MOP::Class returns its metaclass"); +is(Class::MOP::get_meta(bless {}, 'Class::MOP::Class'), Class::MOP::Class->meta, "... get_meta on an instance of Class::MOP::Class returns its metaclass"); +