load all custom metaclasses automatically + tests 0_38
Guillermo Roditi [Tue, 29 May 2007 21:41:26 +0000 (21:41 +0000)]
Changes
lib/metaclass.pm
t/045_metaclass_loads_classes.t [new file with mode: 0644]
t/lib/MyMetaClass/Attribute.pm [new file with mode: 0644]
t/lib/MyMetaClass/Instance.pm [new file with mode: 0644]
t/lib/MyMetaClass/Method.pm [new file with mode: 0644]

diff --git a/Changes b/Changes
index 8b2686c..241561c 100644 (file)
--- 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 ~~
index 2e215c4..0b6ba57 100644 (file)
@@ -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 (file)
index 0000000..e6bcc12
--- /dev/null
@@ -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 (file)
index 0000000..dfb277c
--- /dev/null
@@ -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 (file)
index 0000000..1a4a99b
--- /dev/null
@@ -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 (file)
index 0000000..4d606a3
--- /dev/null
@@ -0,0 +1,9 @@
+
+package MyMetaClass::Method;
+
+use strict;
+use warnings;
+
+use base 'Class::MOP::Method';
+
+1;