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