Initial fix for ensure_class_loaded, and added some tests for same
Justin Guenther [Sun, 18 Jun 2006 23:41:32 +0000 (23:41 +0000)]
lib/DBIx/Class/Componentised.pm
t/90ensure_class_loaded.t
t/lib/DBICTest/Schema/TwoKeyTreeLike.pm
t/lib/DBICTest/SyntaxErrorComponent.pm [new file with mode: 0644]

index 109ad36..42dec25 100644 (file)
@@ -66,11 +66,8 @@ sub _load_components {
 #              ->has_many('rel', 'Some::Schema::Class'...)
 sub ensure_class_loaded {
   my ($class, $f_class) = @_;
-  eval "require $f_class";
-  my $err = $@;
-  Class::Inspector->loaded($f_class)
-    or $class->throw_exception($err || "`require $f_class' was successful".
-                                       "but the package is not defined");
+  return if Class::Inspector->loaded($f_class);
+  require $f_class;
 }
 
 # Returns true if the specified class is installed or already loaded, false
index 672450b..4da8a75 100644 (file)
@@ -13,7 +13,7 @@ BEGIN {
 
 my $schema = DBICTest->init_schema();
 
-plan tests => 17;
+plan tests => 19;
 
 # Test ensure_class_found
 ok( $schema->ensure_class_found('DBIx::Class::Schema'),
@@ -37,7 +37,8 @@ $retval = eval { $schema->load_optional_class('DBICTest::OptionalComponent') };
 ok( !$@, 'load_optional_class on an existing class did not throw' );
 ok( $retval, 'DBICTest::OptionalComponent loaded' );
 eval { $schema->load_optional_class('DBICTest::ErrorComponent') };
-like( $@, qr/did not return a true value/, 'DBICTest::ErrorComponent threw ok' );
+like( $@, qr/did not return a true value/,
+      'DBICTest::ErrorComponent threw ok' );
 
 # Test ensure_class_loaded
 ok( Class::Inspector->loaded('TestPackage::A'), 'anonymous package exists' );
@@ -53,4 +54,19 @@ ok( !$@, 'ensure_class_loaded detected an existing but non-loaded class' );
 ok( Class::Inspector->loaded('DBICTest::FakeComponent'),
    'DBICTest::FakeComponent now loaded' );
 
+{
+  # Squash warnings about syntax errors in SytaxErrorComponent.pm
+  local $SIG{__WARN__} = sub {
+    my $warning = shift;
+    warn $warning unless (
+      $warning =~ /String found where operator expected/ or
+      $warning =~ /Missing operator before/
+    );
+  };
+  eval { $schema->load_optional_class('DBICTest::SyntaxErrorComponent') };
+  like( $@, qr/syntax error/, 'DBICTest::ErrorComponent threw ok' );
+  eval { $schema->ensure_class_loaded('DBICTest::SyntaxErrorComponent') };
+  like( $@, qr/syntax error/, 'DBICTest::ErrorComponent threw ok' );
+}
+
 1;
index 1c1b7b9..89d8e0a 100644 (file)
@@ -15,7 +15,7 @@ __PACKAGE__->add_columns(
 );
 __PACKAGE__->set_primary_key(qw/id1 id2/);
 __PACKAGE__->add_unique_constraint('tktlnameunique' => ['name']);
-__PACKAGE__->belongs_to('parent', 'TwoKeyTreeLike',
+__PACKAGE__->belongs_to('parent', 'DBICTest::Schema::TwoKeyTreeLike',
                           { 'foreign.id1' => 'self.parent1', 'foreign.id2' => 'self.parent2'});
 
 1;
diff --git a/t/lib/DBICTest/SyntaxErrorComponent.pm b/t/lib/DBICTest/SyntaxErrorComponent.pm
new file mode 100644 (file)
index 0000000..27f7ad8
--- /dev/null
@@ -0,0 +1,9 @@
+#   belongs to t/run/90ensure_class_loaded.tl
+package # hide from PAUSE 
+    DBICTest::SyntaxErrorComponent;
+use warnings;
+use strict;
+
+my $str ''; # syntax error
+
+1;