5 use Test::More tests => 31;
26 $destroyed{ $self->type }++;
32 my $self = Class->new(type => 'accessor');
35 my $self2 = Class->new(type => 'middle');
36 my $self3 = Class->new(type => 'constructor', self => $self2);
39 for my $object ($self, $self2, $self3) {
40 ok(Scalar::Util::isweak($object->{self}), "weak reference");
41 ok($object->self->self->self->self, "we've got circularity");
47 is($destroyed{accessor}, 1, "destroyed from the accessor");
48 is($destroyed{constructor}, 1, "destroyed from the constructor");
49 is($destroyed{middle}, 1, "casuality of war");
51 Class->meta->make_immutable();
52 ok(Class->meta->is_immutable, 'make_immutable made it immutable');
55 is($destroyed{accessor}, 2, "destroyed from the accessor (after make_immutable)");
56 is($destroyed{constructor}, 2, "destroyed from the constructor (after make_immutable)");
57 is($destroyed{middle}, 2, "casuality of war (after make_immutable)");
60 ok(!Class->meta->get_attribute('type')->is_weak_ref, "type is not a weakref");
61 ok(Class->meta->get_attribute('self')->is_weak_ref, "self IS a weakref");
74 ok(Class2->meta->get_attribute('value')->is_weak_ref, "value IS a weakref");
77 my $obj = Class2->new;
78 is($obj->value, 10, "weak_ref doesn't apply to non-refs");
81 my $obj2 = Class2->new;
86 is_deeply($obj2->value, undef, "weakened the reference even with a nonref default");
94 default => sub { {} },
96 predicate => 'has_hashref',
100 my $obj = Class3->new;
101 is($obj->hashref, undef, "hashref collected immediately because refcount=0");
102 ok($obj->has_hashref, 'attribute is turned into undef, not deleted from instance');
104 $obj->hashref({1 => 1});
105 is($obj->hashref, undef, "hashref collected between set and get because refcount=0");
106 ok($obj->has_hashref, 'attribute is turned into undef, not deleted from instance');
108 ok(Class3->meta->get_attribute('hashref')->is_weak_ref, "hashref IS a weakref");