Allow requiring a version with is_class_loaded, load_class and load_first_existing_class.
[gitmo/Class-MOP.git] / t / 083_load_class.t
index 22a10af..b2a41ef 100644 (file)
@@ -1,12 +1,15 @@
 use strict;
 use warnings;
-use Test::More tests => 34;
+use Test::More;
 use Test::Exception;
 
 require Class::MOP;
 use lib 't/lib';
 
-ok(!Class::MOP::is_class_loaded(), "is_class_loaded with no argument returns false");
+dies_ok {
+    Class::MOP::is_class_loaded()
+} "is_class_loaded with no argument dies";
+
 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??");
 
@@ -23,9 +26,7 @@ throws_ok {
     Class::MOP::load_class('__PACKAGE__')
 } qr/__PACKAGE__\.pm.*\@INC/, 'errors sanely on __PACKAGE__.pm';
 
-my $meta = Class::MOP::load_class('BinaryTree');
-ok($meta, "successfully loaded the class BinaryTree");
-is($meta->name, "BinaryTree", "load_class returns the metaclass");
+Class::MOP::load_class('BinaryTree');
 can_ok('BinaryTree' => 'traverse');
 
 do {
@@ -34,9 +35,11 @@ do {
 };
 
 
-my $ret = Class::MOP::load_class('Class');
-ok($ret, "this should not die!");
-is( $ret, "Class", "class name returned" );
+{
+    local $@;
+    eval { Class::MOP::load_class('Class') };
+    ok( ! $@, 'load_class does not die if the package is already defined' );
+}
 
 ok( !Class::MOP::does_metaclass_exist("Class"), "no metaclass for non MOP class" );
 
@@ -62,8 +65,8 @@ qr/Missing right curly/,
 throws_ok {
     Class::MOP::load_class('This::Does::Not::Exist');
 }
-qr/Could not load class \(This::Does::Not::Exist\) because :/,
-    'Many Moose tests rely on the exact formatting of this error';
+qr{Can't locate This/Does/Not/Exist\.pm in \@INC},
+    'load_first_existing_class throws a familiar error for a single module';
 
 {
     package Other;
@@ -80,8 +83,6 @@ lives_ok {
     use metaclass;
 }
 
-isa_ok( Class::MOP::load_class("Lala"), "Class::MOP::Class", "when an object has a metaclass it is returned" );
-
 lives_ok {
     is(Class::MOP::load_first_existing_class("Lala", "Does::Not::Exist"), "Lala", 'load_first_existing_class 1/2 params ok, class name returned');
     is(Class::MOP::load_first_existing_class("Does::Not::Exist", "Lala"), "Lala", 'load_first_existing_class 2/2 params ok, class name returned');
@@ -89,7 +90,7 @@ lives_ok {
 
 throws_ok {
     Class::MOP::load_first_existing_class("Does::Not::Exist", "Also::Does::Not::Exist")
-} qr/Could not load class \(Does::Not::Exist.*Could not load class \(Also::Does::Not::Exist/s, 'Multiple non-existant classes cause exception';
+} qr/Does::Not::Exist.*Also::Does::Not::Exist/s, 'Multiple non-existant classes cause exception';
 
 {
     sub whatever {
@@ -123,3 +124,57 @@ throws_ok {
     ok( Class::MOP::is_class_loaded('TestClassLoaded3'),
         'We see that TestClassLoaded3 is loaded after requiring it (it has an @ISA but no methods or $VERSION)' );
 }
+
+{
+    {
+        package Not::Loaded;
+        our @ISA;
+    }
+
+    ok( ! Class::MOP::is_class_loaded('Not::Loaded'),
+        'the mere existence of an @ISA for a package does not mean a class is loaded' );
+}
+
+{
+    {
+        package Loaded::Ish;
+        our @ISA = 'Foo';
+    }
+
+    ok( Class::MOP::is_class_loaded('Loaded::Ish'),
+        'an @ISA with members does mean a class is loaded' );
+}
+
+{
+    {
+        package Class::WithVersion;
+        our $VERSION = 23;
+    };
+
+    ok( Class::MOP::is_class_loaded('Class::WithVersion', { -version => 13 }),
+        'version 23 satisfies version requirement 13' );
+
+    ok( !Class::MOP::is_class_loaded('Class::WithVersion', { -version => 42 }),
+        'version 23 does not satisfy version requirement 42' );
+
+    throws_ok {
+        Class::MOP::load_first_existing_class('Affe', 'Tiger', 'Class::WithVersion' => { -version => 42 });
+    } qr/Class::WithVersion version 42 required--this is only version 23/,
+    'load_first_existing_class gives correct exception on old version';
+
+    lives_ok {
+        Class::MOP::load_first_existing_class('Affe', 'Tiger', 'Class::WithVersion' => { -version => 13 });
+    } 'loading class with required version with load_first_existing_class';
+
+    throws_ok {
+        Class::MOP::load_class('Class::WithVersion' => { -version => 42 });
+    } qr/Class::WithVersion version 42 required--this is only version 23/,
+    'load_class gives correct exception on old version';
+
+    lives_ok {
+        Class::MOP::load_class('Class::WithVersion' => { -version => 13 });
+    } 'loading class with required version with load_class';
+
+}
+
+done_testing;