Be on the safe side
[p5sagit/Devel-GlobalDestruction.git] / t / 01_basic.t
index 3c3ffbd..c739a63 100644 (file)
@@ -3,13 +3,19 @@ use warnings;
 
 BEGIN {
   if ($ENV{DEVEL_GLOBALDESTRUCTION_PP_TEST}) {
-    require DynaLoader;
+    no strict 'refs';
     no warnings 'redefine';
-    my $orig = \&DynaLoader::bootstrap;
-    *DynaLoader::bootstrap = sub {
-      die 'no XS' if $_[0] eq 'Devel::GlobalDestruction';
-      goto $orig;
-    };
+
+    for my $f (qw(DynaLoader::bootstrap XSLoader::load)) {
+      my ($mod) = $f =~ /^ (.+) \:\: [^:]+ $/x;
+      eval "require $mod" or die $@;
+
+      my $orig = \&$f;
+      *$f = sub {
+        die 'no XS' if ($_[0]||'') eq 'Devel::GlobalDestruction';
+        goto $orig;
+      };
+    }
   }
 }
 
@@ -19,7 +25,7 @@ BEGIN {
   sub DESTROY { my $self = shift; $self->[0]->() }
 }
 
-print "1..6\n";
+print "1..9\n";
 
 our $had_error;
 
@@ -52,6 +58,10 @@ sub ok ($$) {
   print "\n";
 }
 
+END {
+  ok( ! in_global_destruction(), 'Not yet in GD while in END block 2' )
+}
+
 ok( eval "use Devel::GlobalDestruction; 1", "use Devel::GlobalDestruction" );
 
 ok( defined &in_global_destruction, "exported" );
@@ -60,6 +70,13 @@ ok( defined prototype \&in_global_destruction, "defined prototype" );
 
 ok( prototype \&in_global_destruction eq "", "empty prototype" );
 
-ok( !in_global_destruction(), "not in GD" );
+ok( ! in_global_destruction(), "Runtime is not GD" );
+
+our $sg1 = Test::Scope::Guard->new(sub { ok( in_global_destruction(), "Final cleanup object destruction properly in GD" ) });
+
+END {
+  ok( ! in_global_destruction(), 'Not yet in GD while in END block 1' )
+}
 
-our $sg = Test::Scope::Guard->new(sub { ok( in_global_destruction(), "in GD" ) });
+our $sg2 = Test::Scope::Guard->new(sub { ok( ! in_global_destruction(), "Object destruction in END not considered GD" ) });
+END { undef $sg2 }