From: Guillermo Roditi Date: Tue, 29 May 2007 21:41:26 +0000 (+0000) Subject: load all custom metaclasses automatically + tests X-Git-Tag: 0_38^0 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=f226618136a635cf84379ac8f4d2de298826fccf;p=gitmo%2FClass-MOP.git load all custom metaclasses automatically + tests --- diff --git a/Changes b/Changes index 8b2686c..241561c 100644 --- a/Changes +++ b/Changes @@ -28,8 +28,8 @@ Revision history for Perl extension Class-MOP. - updated docs to explain this * metaclass - - load custom metaclass automatically. - - modified tests to test this behavior + - load custom metaclasses automatically. + - added tests for this behavior 0.37 Sat. March 10, 2007 ~~ Many, many documentation updates ~~ diff --git a/lib/metaclass.pm b/lib/metaclass.pm index 2e215c4..0b6ba57 100644 --- a/lib/metaclass.pm +++ b/lib/metaclass.pm @@ -20,11 +20,15 @@ sub import { } else { $metaclass = shift; + #make sure the custom metaclass gets loaded Class::MOP::load_class($metaclass); ($metaclass->isa('Class::MOP::Class')) || confess "The metaclass ($metaclass) must be derived from Class::MOP::Class"; } my %options = @_; + #make sure the custom metaclasses get loaded + map{ Class::MOP::load_class($options{$_}) } + grep{ /^(attribute|method|instance)_metaclass/ } keys %options; my $package = caller(); # create a meta object so we can install &meta diff --git a/t/045_metaclass_loads_classes.t b/t/045_metaclass_loads_classes.t new file mode 100644 index 0000000..e6bcc12 --- /dev/null +++ b/t/045_metaclass_loads_classes.t @@ -0,0 +1,43 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use FindBin; +use File::Spec::Functions; + +use Test::More tests => 9; + +BEGIN { + use_ok('Class::MOP'); +} + +use lib catdir($FindBin::Bin, 'lib'); + +{ + package Foo; + + use strict; + use warnings; + + use metaclass 'MyMetaClass' => ( + 'attribute_metaclass' => 'MyMetaClass::Attribute', + 'instance_metaclass' => 'MyMetaClass::Instance', + 'method_metaclass' => 'MyMetaClass::Method', + 'random_metaclass' => 'MyMetaClass::Random', + ); +} + +my $meta = Foo->meta; + +isa_ok($meta, 'MyMetaClass', '... Correct metaclass'); +ok(Class::MOP::is_class_loaded('MyMetaClass'), '... metaclass loaded'); + +is($meta->attribute_metaclass, 'MyMetaClass::Attribute', '... Correct attribute metaclass'); +ok(Class::MOP::is_class_loaded('MyMetaClass::Attribute'), '... attribute metaclass loaded'); + +is($meta->instance_metaclass, 'MyMetaClass::Instance', '... Correct instance metaclass'); +ok(Class::MOP::is_class_loaded('MyMetaClass::Instance'), '... instance metaclass loaded'); + +is($meta->method_metaclass, 'MyMetaClass::Method', '... Correct method metaclass'); +ok(Class::MOP::is_class_loaded('MyMetaClass::Method'), '... method metaclass loaded'); diff --git a/t/lib/MyMetaClass/Attribute.pm b/t/lib/MyMetaClass/Attribute.pm new file mode 100644 index 0000000..dfb277c --- /dev/null +++ b/t/lib/MyMetaClass/Attribute.pm @@ -0,0 +1,9 @@ + +package MyMetaClass::Attribute; + +use strict; +use warnings; + +use base 'Class::MOP::Attribute'; + +1; diff --git a/t/lib/MyMetaClass/Instance.pm b/t/lib/MyMetaClass/Instance.pm new file mode 100644 index 0000000..1a4a99b --- /dev/null +++ b/t/lib/MyMetaClass/Instance.pm @@ -0,0 +1,9 @@ + +package MyMetaClass::Instance; + +use strict; +use warnings; + +use base 'Class::MOP::Instance'; + +1; diff --git a/t/lib/MyMetaClass/Method.pm b/t/lib/MyMetaClass/Method.pm new file mode 100644 index 0000000..4d606a3 --- /dev/null +++ b/t/lib/MyMetaClass/Method.pm @@ -0,0 +1,9 @@ + +package MyMetaClass::Method; + +use strict; +use warnings; + +use base 'Class::MOP::Method'; + +1;