add leak test
Jesse Luehrs [Sat, 13 Nov 2010 15:38:09 +0000 (09:38 -0600)]
dist.ini
t/20-leaks.t [new file with mode: 0644]

index 9203c25..94ac0fa 100644 (file)
--- a/dist.ini
+++ b/dist.ini
@@ -14,6 +14,7 @@ Scalar::Util = 0
 [Prereqs / TestRequires]
 Test::Fatal = 0
 Test::More = 0.88
+Test::Requires = 0
 
 ; we maintain a Makefile.PL in the repository to be able to work without dzil,
 ; but for the distribution we let dzil generate a Makefile.PL with the proper
diff --git a/t/20-leaks.t b/t/20-leaks.t
new file mode 100644 (file)
index 0000000..124e49e
--- /dev/null
@@ -0,0 +1,143 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use Test::More;
+use Test::Fatal;
+use Test::Requires 'Test::LeakTrace';
+
+use Package::Stash;
+use Symbol;
+
+{
+    package Bar;
+}
+
+{
+    package Baz;
+    our $foo;
+    sub bar { }
+    use constant baz => 1;
+    our %quux = (a => 'b');
+}
+
+{
+    no_leaks_ok {
+        Package::Stash->new('Foo');
+    } "object construction doesn't leak";
+}
+
+{
+    no_leaks_ok {
+        Package::Stash->new('Bar');
+    } "object construction doesn't leak, with an existing package";
+}
+
+{
+    no_leaks_ok {
+        Package::Stash->new('Baz');
+    } "object construction doesn't leak, with an existing package with things in it";
+}
+
+{
+    my $foo = Package::Stash->new('Foo');
+    no_leaks_ok {
+        $foo->name;
+        $foo->namespace;
+    } "accessors don't leak";
+}
+
+{
+    my $foo = Package::Stash->new('Foo');
+    leaks_cmp_ok {
+        $foo->add_package_symbol('$scalar');
+        $foo->add_package_symbol('@array');
+        $foo->add_package_symbol('%hash');
+        $foo->add_package_symbol('io');
+    } '==', 4, "add_package_symbol doesn't leak";
+}
+
+{
+    my $foo = Package::Stash->new('Foo');
+    leaks_cmp_ok {
+        $foo->add_package_symbol('$scalar_init' => 1);
+        $foo->add_package_symbol('@array_init' => []);
+        $foo->add_package_symbol('%hash_init' => {});
+        # hmmm, wonder why this coderef isn't treated as a leak
+        $foo->add_package_symbol('&code_init' => sub { "foo" });
+        $foo->add_package_symbol('io_init' => Symbol::geniosym);
+    } '==', 9, "add_package_symbol doesn't leak";
+    is(exception {
+        is(Foo->code_init, 'foo', "sub installed correctly")
+    }, undef, "code_init exists");
+}
+
+{
+    my $foo = Package::Stash->new('Foo');
+    no_leaks_ok {
+        $foo->remove_package_symbol('$scalar_init');
+        $foo->remove_package_symbol('@array_init');
+        $foo->remove_package_symbol('%hash_init');
+        $foo->remove_package_symbol('&code_init');
+        $foo->remove_package_symbol('io_init');
+    } "remove_package_symbol doesn't leak";
+}
+
+{
+    my $foo = Package::Stash->new('Foo');
+    $foo->add_package_symbol("${_}glob") for ('$', '@', '%', '&', '');
+    no_leaks_ok {
+        $foo->remove_package_glob('glob');
+    } "remove_package_glob doesn't leak";
+}
+
+{
+    my $foo = Package::Stash->new('Foo');
+    no_leaks_ok {
+        $foo->has_package_symbol('io');
+        $foo->has_package_symbol('%hash');
+        $foo->has_package_symbol('@array_init');
+        $foo->has_package_symbol('$glob');
+        $foo->has_package_symbol('&something_else');
+    } "has_package_symbol doesn't leak";
+}
+
+{
+    my $foo = Package::Stash->new('Foo');
+    no_leaks_ok {
+        $foo->get_package_symbol('io');
+        $foo->get_package_symbol('%hash');
+        $foo->get_package_symbol('@array_init');
+        $foo->get_package_symbol('$glob');
+        $foo->get_package_symbol('&something_else');
+    } "get_package_symbol doesn't leak";
+}
+
+{
+    my $foo = Package::Stash->new('Foo');
+    ok(!$foo->has_package_symbol('$glob'));
+    ok(!$foo->has_package_symbol('@array_init'));
+    no_leaks_ok {
+        $foo->get_or_add_package_symbol('io');
+        $foo->get_or_add_package_symbol('%hash');
+        # and why are these not leaks either?
+        $foo->get_or_add_package_symbol('@array_init');
+        $foo->get_or_add_package_symbol('$glob');
+    } "get_or_add_package_symbol doesn't leak";
+    ok($foo->has_package_symbol('$glob'));
+    is(ref($foo->get_package_symbol('$glob')), 'SCALAR');
+    ok($foo->has_package_symbol('@array_init'));
+    is(ref($foo->get_package_symbol('@array_init')), 'ARRAY');
+}
+
+{
+    my $foo = Package::Stash->new('Foo');
+    my $baz = Package::Stash->new('Baz');
+    no_leaks_ok {
+        $foo->list_all_package_symbols;
+        $foo->list_all_package_symbols('SCALAR');
+        $foo->list_all_package_symbols('CODE');
+        $baz->list_all_package_symbols('CODE');
+    } "list_all_package_symbols doesn't leak";
+}
+
+done_testing;