give the actual reason for this TODO
[gitmo/Moose.git] / t / bugs / memory_leaks.t
1 use strict;
2 use warnings;
3
4 use Test::Requires {
5     'Test::LeakTrace'     => '0.01',
6     'Test::Memory::Cycle' => '0',
7 };
8
9 use Test::More;
10
11 use Moose ();
12 use Moose::Util qw( apply_all_roles );
13
14 {
15     package MyRole;
16     use Moose::Role;
17     sub myname { "I'm a role" }
18 }
19
20 no_leaks_ok(
21     sub {
22         Moose::Meta::Class->create_anon_class->new_object;
23     },
24     'anonymous class with no roles is leak-free'
25 );
26
27 no_leaks_ok(
28     sub {
29         Moose::Meta::Role->initialize('MyRole2');
30     },
31     'Moose::Meta::Role->initialize is leak-free'
32 );
33
34 no_leaks_ok(
35     sub {
36         Moose::Meta::Class->create('MyClass2')->new_object;
37     },
38     'creating named class is leak-free'
39 );
40
41 {
42     local $TODO
43         = 'role application leaks because we end up applying the role more than once to the meta object';
44     no_leaks_ok(
45         sub {
46             Moose::Meta::Class->create( 'MyClass', roles => ['MyRole'] );
47         },
48         'named class with roles is leak-free'
49     );
50
51     no_leaks_ok(
52         sub {
53             Moose::Meta::Role->create( 'MyRole2', roles => ['MyRole'] );
54         },
55         'named role with roles is leak-free'
56     );
57 }
58
59 no_leaks_ok(
60     sub {
61         my $object = Moose::Meta::Class->create('MyClass2')->new_object;
62         apply_all_roles( $object, 'MyRole' );
63     },
64     'applying role to an instance is leak-free'
65 );
66
67 no_leaks_ok(
68     sub {
69         Moose::Meta::Role->create_anon_role;
70     },
71     'anonymous role is leak-free'
72 );
73
74 {
75     # fixing this leak currently triggers a bug in Carp
76     # we can un-TODO once that fix goes in allowing the leak
77     # in Eval::Closure to be fixed
78     local $TODO = 'Eval::Closure leaks a bit at the moment';
79     no_leaks_ok(
80         sub {
81             my $meta = Moose::Meta::Class->create_anon_class;
82             $meta->make_immutable;
83         },
84         'making an anon class immutable is leak-free'
85     );
86 }
87
88 {
89     my $meta3 = Moose::Meta::Class->create('MyClass3');
90     memory_cycle_ok( $meta3, 'named metaclass object is cycle-free' );
91     memory_cycle_ok( $meta3->new_object, 'MyClass3 object is cycle-free' );
92
93     my $anon_class = Moose::Meta::Class->create_anon_class;
94     memory_cycle_ok($anon_class, 'anon metaclass object is cycle-free' );
95     memory_cycle_ok( $anon_class->new_object, 'object from anon metaclass is cycle-free' );
96
97     $anon_class->make_immutable;
98     memory_cycle_ok($anon_class, 'immutable anon metaclass object is cycle-free' );
99     memory_cycle_ok( $anon_class->new_object, 'object from immutable anon metaclass is cycle-free' );
100
101     my $anon_role = Moose::Meta::Role->create_anon_role;
102     memory_cycle_ok($anon_role, 'anon role meta object is cycle-free' );
103 }
104
105 done_testing;