7 if (eval "require Scalar::Util; 1") {
11 plan skip_all => "Scalar::Util required for this test";
35 $destroyed{ $self->type }++;
39 my $self = Class->new(type => 'accessor');
42 my $self2 = Class->new(type => 'middle');
43 my $self3 = Class->new(type => 'constructor', self => $self2);
46 for my $object ($self, $self2, $self3) {
47 ok(Scalar::Util::isweak($object->{self}), "weak reference");
48 ok($object->self->self->self->self, "we've got circularity");
52 is($destroyed{accessor}, 1, "destroyed from the accessor");
53 is($destroyed{constructor}, 1, "destroyed from the constructor");
54 is($destroyed{middle}, 1, "casuality of war");
56 ok(!Class->meta->get_attribute('type')->is_weak_ref, "type is not a weakref");
57 ok(Class->meta->get_attribute('self')->is_weak_ref, "self IS a weakref");
70 ok(Class2->meta->get_attribute('value')->is_weak_ref, "value IS a weakref");
73 my $obj = Class2->new;
74 is($obj->value, 10, "weak_ref doesn't apply to non-refs");
77 my $obj2 = Class2->new;
82 is_deeply($obj2->value, undef, "weakened the reference even with a nonref default");
90 default => sub { {} },
92 predicate => 'has_hashref',
96 my $obj = Class3->new;
97 is($obj->hashref, undef, "hashref collected immediately because refcount=0");
98 ok($obj->has_hashref, 'attribute is turned into undef, not deleted from instance');
100 $obj->hashref({1 => 1});
101 is($obj->hashref, undef, "hashref collected between set and get because refcount=0");
102 ok($obj->has_hashref, 'attribute is turned into undef, not deleted from instance');
104 ok(Class3->meta->get_attribute('hashref')->is_weak_ref, "hashref IS a weakref");