extracted XS code from Devel::GlobalDestruction
[p5sagit/Devel-GlobalDestruction-XS.git] / t / 01_basic.t
CommitLineData
101e5667 1use strict;
2use warnings;
3
4BEGIN {
5 package Test::Scope::Guard;
6 sub new { my ($class, $code) = @_; bless [$code], $class; }
7 sub DESTROY { my $self = shift; $self->[0]->() }
8}
9
10print "1..8\n";
11
12our $had_error;
13
14# try to ensure this is the last-most END so we capture future tests
15# running in other ENDs
16require B;
17my $reinject_retries = my $max_retry = 5;
18my $end_worker;
19$end_worker = sub {
20 my $tail = (B::end_av()->ARRAY)[-1];
21 if (!defined $tail or $tail == $end_worker) {
22 $? = $had_error || 0;
23 $reinject_retries = 0;
24 }
25 elsif ($reinject_retries--) {
26 push @{B::end_av()->object_2svref}, $end_worker;
27 }
28 else {
29 print STDERR "\n\nSomething is racing with @{[__FILE__]} for final END block definition - can't win after $max_retry iterations :(\n\n";
30 require POSIX;
31 POSIX::_exit( 255 );
32 }
33};
34END { push @{B::end_av()->object_2svref}, $end_worker }
35
36sub ok ($$) {
37 $had_error++, print "not " if !$_[0];
38 print "ok";
39 print " - $_[1]" if defined $_[1];
40 print "\n";
41}
42
43END {
44 ok( ! Devel::GlobalDestruction::XS::in_global_destruction(), 'Not yet in GD while in END block 2' )
45}
46
47ok( eval "use Devel::GlobalDestruction::XS; 1", "use Devel::GlobalDestruction::XS" );
48
49ok( defined prototype \&Devel::GlobalDestruction::XS::in_global_destruction, "defined prototype" );
50
51ok( prototype \&Devel::GlobalDestruction::XS::in_global_destruction eq "", "empty prototype" );
52
53ok( ! Devel::GlobalDestruction::XS::in_global_destruction(), "Runtime is not GD" );
54
55our $sg1 = Test::Scope::Guard->new(sub { ok( Devel::GlobalDestruction::XS::in_global_destruction(), "Final cleanup object destruction properly in GD" ) });
56
57END {
58 ok( ! Devel::GlobalDestruction::XS::in_global_destruction(), 'Not yet in GD while in END block 1' )
59}
60
61our $sg2 = Test::Scope::Guard->new(sub { ok( ! Devel::GlobalDestruction::XS::in_global_destruction(), "Object destruction in END not considered GD" ) });
62END { undef $sg2 }