1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
|
use strict;
use warnings;
use Test::More;
BEGIN {
plan skip_all => 'Leak tests fail under perl 5.21.[6-9]'
if "$]" >= '5.021006' and "$]" <= '5.021011';
plan skip_all => 'Leak tests fail under Devel::Cover' if $INC{'Devel/Cover.pm'};
}
use Test::LeakTrace;
use Test::Memory::Cycle;
use Moose ();
use Moose::Util qw( apply_all_roles );
use Moose::Util::TypeConstraints;
{
package MyRole;
use Moose::Role;
sub myname { "I'm a role" }
}
{
package Fake::DateTime;
use Moose;
has 'string_repr' => ( is => 'ro' );
package Mortgage;
use Moose;
use Moose::Util::TypeConstraints;
coerce 'Fake::DateTime' => from 'Str' =>
via { Fake::DateTime->new( string_repr => $_ ) };
has 'closing_date' => (
is => 'rw',
isa => 'Fake::DateTime',
coerce => 1,
trigger => sub {
my ( $self, $val ) = @_;
::pass('... trigger is being called');
::isa_ok( $self->closing_date, 'Fake::DateTime' );
::isa_ok( $val, 'Fake::DateTime' );
}
);
}
{
package Man;
use Moose;
my @actions;
sub live {
push @actions, 'live';
}
sub create {
push @actions, 'create';
}
sub breathe {
push @actions, 'breathe';
}
package Earth;
use Moose;
use Moose::Util::TypeConstraints;
has man => (
isa => 'Man',
handles => [qw( live create breathe )],
);
}
{
local $TODO = 'anonymous classes leak on 5.8' if "$]" < 5.010;
no_leaks_ok(
sub {
Moose::Meta::Class->create_anon_class->new_object;
},
'anonymous class with no roles is leak-free'
);
}
no_leaks_ok(
sub {
Moose::Meta::Role->initialize('MyRole2');
},
'Moose::Meta::Role->initialize is leak-free'
);
no_leaks_ok(
sub {
Moose::Meta::Class->create('MyClass2')->new_object;
},
'creating named class is leak-free'
);
{
local $TODO
= 'role application leaks because we end up applying the role more than once to the meta object';
no_leaks_ok(
sub {
Moose::Meta::Class->create( 'MyClass', roles => ['MyRole'] );
},
'named class with roles is leak-free'
);
no_leaks_ok(
sub {
Moose::Meta::Role->create( 'MyRole2', roles => ['MyRole'] );
},
'named role with roles is leak-free'
);
}
no_leaks_ok(
sub {
my $object = Moose::Meta::Class->create('MyClass2')->new_object;
apply_all_roles( $object, 'MyRole' );
},
'applying role to an instance is leak-free'
);
no_leaks_ok(
sub {
Moose::Meta::Role->create_anon_role;
},
'anonymous role is leak-free'
);
{
# fixing this leak currently triggers a bug in Carp
# we can un-TODO once that fix goes in allowing the leak
# in Eval::Closure to be fixed
local $TODO = 'Eval::Closure leaks a bit at the moment';
no_leaks_ok(
sub {
my $meta = Moose::Meta::Class->create_anon_class;
$meta->make_immutable;
},
'making an anon class immutable is leak-free'
);
}
{
my $meta3 = Moose::Meta::Class->create('MyClass3');
memory_cycle_ok( $meta3, 'named metaclass object is cycle-free' );
memory_cycle_ok( $meta3->new_object, 'MyClass3 object is cycle-free' );
my $anon_class = Moose::Meta::Class->create_anon_class;
memory_cycle_ok($anon_class, 'anon metaclass object is cycle-free' );
memory_cycle_ok( $anon_class->new_object, 'object from anon metaclass is cycle-free' );
$anon_class->make_immutable;
memory_cycle_ok($anon_class, 'immutable anon metaclass object is cycle-free' );
memory_cycle_ok( $anon_class->new_object, 'object from immutable anon metaclass is cycle-free' );
my $anon_role = Moose::Meta::Role->create_anon_role;
memory_cycle_ok($anon_role, 'anon role meta object is cycle-free' );
}
{
my $Str = find_type_constraint('Str');
my $Undef = find_type_constraint('Undef');
my $Str_or_Undef = Moose::Meta::TypeConstraint::Union->new(
type_constraints => [ $Str, $Undef ] );
memory_cycle_ok($Str_or_Undef, 'union types do not leak');
}
{
my $mtg = Mortgage->new( closing_date => 'yesterday' );
$mtg->closing_date;
Mortgage->meta->make_immutable;
memory_cycle_ok($mtg->meta, 'meta (triggers/coerce) is cycle-free');
}
{
local $TODO = 'meta cycles exist at the moment';
memory_cycle_ok(Earth->new->meta, 'meta (handles) is cycle-free');
memory_cycle_ok(Earth->meta, 'meta (class) is cycle-free');
}
{
my $Point = Class::MOP::Class->create('Point' => (
version => '0.01',
attributes => [
Class::MOP::Attribute->new('x' => (
reader => 'x',
init_arg => 'x'
)),
Class::MOP::Attribute->new('y' => (
accessor => 'y',
init_arg => 'y'
)),
],
methods => {
'new' => sub {
my $class = shift;
my $instance = $class->meta->new_object(@_);
bless $instance => $class;
},
'clear' => sub {
my $self = shift;
$self->{'x'} = 0;
$self->{'y'} = 0;
}
}
));
my $Point3D = Class::MOP::Class->create('Point3D' => (
version => '0.01',
superclasses => [ 'Point' ],
attributes => [
Class::MOP::Attribute->new('z' => (
default => 123
)),
],
methods => {
'clear' => sub {
my $self = shift;
$self->{'z'} = 0;
$self->SUPER::clear();
}
}
));
local $TODO = 'CMOP cycles exist at the moment';
memory_cycle_ok($Point3D, 'Point3D is cycle-free');
memory_cycle_ok($Point, 'Point is cycle-free');
memory_cycle_ok($Point3D->meta, 'Point3D meta is cycle-free');
memory_cycle_ok($Point->meta, 'Point meta is cycle-free');
}
done_testing;
|