only set use_namespaces=1 if naming is set for dynamic schemas
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / Utils.pm
index 8b52e17..22a2194 100644 (file)
@@ -3,9 +3,12 @@ package # hide from PAUSE
 
 use strict;
 use warnings;
+use Data::Dumper ();
+use Test::More;
+use namespace::clean;
 use Exporter 'import';
 
-our @EXPORT_OK = qw/split_name dumper dumper_squashed eval_without_redefine_warnings/;
+our @EXPORT_OK = qw/split_name dumper dumper_squashed eval_package_without_redefine_warnings class_path warnings_exist warnings_exist_silent/;
 
 use constant BY_CASE_TRANSITION =>
     qr/(?<=[[:lower:]\d])[\W_]*(?=[[:upper:]])|[\W_]+/;
@@ -19,8 +22,6 @@ sub split_name($) {
     split $name =~ /[[:upper:]]/ && $name =~ /[[:lower:]]/ ? BY_CASE_TRANSITION : BY_NON_ALPHANUM, $name;
 }
 
-# Stolen from Data::Dumper::Concise
-
 sub dumper($) {
     my $val = shift;
 
@@ -37,17 +38,84 @@ sub dumper_squashed($) {
     return $dd->Values([ $val ])->Dump;
 }
 
-sub eval_without_redefine_warnings {
-    my $code = shift;
+sub eval_package_without_redefine_warnings {
+    my ($pkg, $code) = @_;
 
     my $warn_handler = $SIG{__WARN__} || sub { warn @_ };
+
     local $SIG{__WARN__} = sub {
         $warn_handler->(@_)
             unless $_[0] =~ /^Subroutine \S+ redefined/;
     };
-    eval $code;
-    die $@ if $@;
+
+    # This hairiness is to handle people using "use warnings FATAL => 'all';"
+    # in their custom or external content.
+    my @delete_syms;
+    my $try_again = 1;
+
+    while ($try_again) {
+        eval $code;
+
+        if (my ($sym) = $@ =~ /^Subroutine (\S+) redefined/) {
+            delete $INC{ +class_path($pkg) };
+            push @delete_syms, $sym;
+
+            foreach my $sym (@delete_syms) {
+                no strict 'refs';
+                undef *{"${pkg}::${sym}"};
+            }
+        }
+        elsif ($@) {
+            die $@ if $@;
+        }
+        else {
+            $try_again = 0;
+        }
+    }
+}
+
+sub class_path {
+    my $class = shift;
+
+    my $class_path = $class;
+    $class_path =~ s{::}{/}g;
+    $class_path .= '.pm';
+
+    return $class_path;
+}
+
+sub warnings_exist(&$$) {
+    my ($code, $re, $test_name) = @_;
+
+    my $matched = 0;
+
+    my $warn_handler = $SIG{__WARN__} || sub { warn @_ };
+    local $SIG{__WARN__} = sub {
+        if ($_[0] =~ $re) {
+            $matched = 1;
+        }
+        else {
+            $warn_handler->(@_)
+        }
+    };
+
+    $code->();
+
+    ok $matched, $test_name;
 }
 
+sub warnings_exist_silent(&$$) {
+    my ($code, $re, $test_name) = @_;
+
+    my $matched = 0;
+
+    local $SIG{__WARN__} = sub { $matched = 1 if $_[0] =~ $re; };
+
+    $code->();
+
+    ok $matched, $test_name;
+}
+
+
 1;
 # vim:et sts=4 sw=4 tw=0: