add some tests relating to destruction
Jesse Luehrs [Sun, 3 May 2009 06:26:29 +0000 (01:26 -0500)]
t/010_basics/019-destruction.t [new file with mode: 0644]
t/010_basics/020-global-destruction.t [new file with mode: 0644]

diff --git a/t/010_basics/019-destruction.t b/t/010_basics/019-destruction.t
new file mode 100644 (file)
index 0000000..fecdad8
--- /dev/null
@@ -0,0 +1,51 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More tests => 3;
+
+our @demolished;
+package Foo;
+use Moose;
+
+sub DEMOLISH {
+    my $self = shift;
+    push @::demolished, __PACKAGE__;
+}
+
+package Foo::Sub;
+use Moose;
+extends 'Foo';
+
+sub DEMOLISH {
+    my $self = shift;
+    push @::demolished, __PACKAGE__;
+}
+
+package Foo::Sub::Sub;
+use Moose;
+extends 'Foo::Sub';
+
+sub DEMOLISH {
+    my $self = shift;
+    push @::demolished, __PACKAGE__;
+}
+
+package main;
+{
+    my $foo = Foo->new;
+}
+is_deeply(\@demolished, ['Foo'], "Foo demolished properly");
+@demolished = ();
+{
+    my $foo_sub = Foo::Sub->new;
+}
+is_deeply(\@demolished, ['Foo::Sub', 'Foo'], "Foo::Sub demolished properly");
+@demolished = ();
+{
+    my $foo_sub_sub = Foo::Sub::Sub->new;
+}
+is_deeply(\@demolished, ['Foo::Sub::Sub', 'Foo::Sub', 'Foo'],
+          "Foo::Sub::Sub demolished properly");
+@demolished = ();
diff --git a/t/010_basics/020-global-destruction.t b/t/010_basics/020-global-destruction.t
new file mode 100644 (file)
index 0000000..f4b6877
--- /dev/null
@@ -0,0 +1,34 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More tests => 2;
+
+our $expected_igd = 0;
+package Foo;
+use Moose;
+
+sub DEMOLISH {
+    my $self = shift;
+    my ($igd) = @_;
+    ::is($igd, $::expected_igd,
+         "in_global_destruction state is passed to DEMOLISH properly");
+}
+
+package main;
+{
+    my $foo = Foo->new;
+}
+$expected_igd = 1;
+# Test::Builder checks for a valid plan at END time, which is before global
+# destruction, so need to test that in a subprocess
+unless (fork) {
+    our $foo = Foo->new;
+    exit;
+}
+wait;
+# but stuff that happens in a subprocess doesn't update Test::Builder's state
+# in this process, so do that manually here
+my $builder = Test::More->builder;
+$builder->current_test($builder->current_test + 1);