4 use Test::More tests => 21;
6 use Scalar::Util 'isweak';
26 $destroyed{ $self->type }++;
30 my $self = Class->new(type => 'accessor');
33 my $self2 = Class->new(type => 'middle');
34 my $self3 = Class->new(type => 'constructor', self => $self2);
37 for my $object ($self, $self2, $self3) {
38 ok(isweak($object->{self}), "weak reference");
39 ok($object->self->self->self->self, "we've got circularity");
43 is($destroyed{accessor}, 1, "destroyed from the accessor");
44 is($destroyed{constructor}, 1, "destroyed from the constructor");
45 is($destroyed{middle}, 1, "casuality of war");
47 ok(!Class->meta->get_attribute('type')->is_weak_ref, "type is not a weakref");
48 ok(Class->meta->get_attribute('self')->is_weak_ref, "self IS a weakref");
61 ok(Class2->meta->get_attribute('value')->is_weak_ref, "value IS a weakref");
64 my $obj = Class2->new;
65 is($obj->value, 10, "weak_ref doesn't apply to non-refs");
68 my $obj2 = Class2->new;
73 is_deeply($obj2->value, undef, "weakened the reference even with a nonref default");
81 default => sub { {} },
83 predicate => 'has_hashref',
87 my $obj = Class3->new;
88 is($obj->hashref, undef, "hashref collected immediately because refcount=0");
89 ok($obj->has_hashref, 'attribute is turned into undef, not deleted from instance');
91 $obj->hashref({1 => 1});
92 is($obj->hashref, undef, "hashref collected between set and get because refcount=0");
93 ok($obj->has_hashref, 'attribute is turned into undef, not deleted from instance');
95 ok(Class3->meta->get_attribute('hashref')->is_weak_ref, "hashref IS a weakref");