5 use Test::LeakTrace 0.01;
6 use Test::Memory::Cycle;
9 use Moose::Util qw( apply_all_roles );
10 use Moose::Util::TypeConstraints;
15 sub myname { "I'm a role" }
20 Moose::Meta::Class->create_anon_class->new_object;
22 'anonymous class with no roles is leak-free'
27 Moose::Meta::Role->initialize('MyRole2');
29 'Moose::Meta::Role->initialize is leak-free'
34 Moose::Meta::Class->create('MyClass2')->new_object;
36 'creating named class is leak-free'
41 = 'role application leaks because we end up applying the role more than once to the meta object';
44 Moose::Meta::Class->create( 'MyClass', roles => ['MyRole'] );
46 'named class with roles is leak-free'
51 Moose::Meta::Role->create( 'MyRole2', roles => ['MyRole'] );
53 'named role with roles is leak-free'
59 my $object = Moose::Meta::Class->create('MyClass2')->new_object;
60 apply_all_roles( $object, 'MyRole' );
62 'applying role to an instance is leak-free'
67 Moose::Meta::Role->create_anon_role;
69 'anonymous role is leak-free'
73 # fixing this leak currently triggers a bug in Carp
74 # we can un-TODO once that fix goes in allowing the leak
75 # in Eval::Closure to be fixed
76 local $TODO = 'Eval::Closure leaks a bit at the moment';
79 my $meta = Moose::Meta::Class->create_anon_class;
80 $meta->make_immutable;
82 'making an anon class immutable is leak-free'
87 my $meta3 = Moose::Meta::Class->create('MyClass3');
88 memory_cycle_ok( $meta3, 'named metaclass object is cycle-free' );
89 memory_cycle_ok( $meta3->new_object, 'MyClass3 object is cycle-free' );
91 my $anon_class = Moose::Meta::Class->create_anon_class;
92 memory_cycle_ok($anon_class, 'anon metaclass object is cycle-free' );
93 memory_cycle_ok( $anon_class->new_object, 'object from anon metaclass is cycle-free' );
95 $anon_class->make_immutable;
96 memory_cycle_ok($anon_class, 'immutable anon metaclass object is cycle-free' );
97 memory_cycle_ok( $anon_class->new_object, 'object from immutable anon metaclass is cycle-free' );
99 my $anon_role = Moose::Meta::Role->create_anon_role;
100 memory_cycle_ok($anon_role, 'anon role meta object is cycle-free' );
104 my $Str = find_type_constraint('Str');
105 my $Undef = find_type_constraint('Undef');
106 my $Str_or_Undef = Moose::Meta::TypeConstraint::Union->new(
107 type_constraints => [ $Str, $Undef ] );
108 memory_cycle_ok($Str_or_Undef, 'union types do not leak');