File: 021-weak-ref.t

package info (click to toggle)
libmouse-perl 2.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,156 kB
  • sloc: perl: 14,569; ansic: 218; makefile: 8
file content (108 lines) | stat: -rw-r--r-- 2,669 bytes parent folder | download | duplicates (8)
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
#!/usr/bin/env perl
use strict;
use warnings;

use Test::More tests => 31;
use Test::Exception;

my %destroyed;

do {
    do {
        package Class;
        use Mouse;

        has self => (
            is       => 'rw',
            weak_ref => 1,
        );

        has type => (
            is => 'rw',
        );

        sub DEMOLISH {
            my $self = shift;
            $destroyed{ $self->type }++;
        }
    };
};

sub do_test{
    my $self = Class->new(type => 'accessor');
    $self->self($self);

    my $self2 = Class->new(type => 'middle');
    my $self3 = Class->new(type => 'constructor', self => $self2);
    $self2->self($self3);

    for my $object ($self, $self2, $self3) {
        ok(Scalar::Util::isweak($object->{self}), "weak reference");
        ok($object->self->self->self->self, "we've got circularity");
    }
}

do_test();

is($destroyed{accessor}, 1, "destroyed from the accessor");
is($destroyed{constructor}, 1, "destroyed from the constructor");
is($destroyed{middle}, 1, "casuality of war");

Class->meta->make_immutable();
ok(Class->meta->is_immutable, 'make_immutable made it immutable');
do_test();

is($destroyed{accessor}, 2, "destroyed from the accessor (after make_immutable)");
is($destroyed{constructor}, 2, "destroyed from the constructor (after make_immutable)");
is($destroyed{middle}, 2, "casuality of war (after make_immutable)");


ok(!Class->meta->get_attribute('type')->is_weak_ref, "type is not a weakref");
ok(Class->meta->get_attribute('self')->is_weak_ref, "self IS a weakref");

do {
    package Class2;
    use Mouse;

    has value => (
        is => 'rw',
        default => 10,
        weak_ref => 1,
    );
};

ok(Class2->meta->get_attribute('value')->is_weak_ref, "value IS a weakref");

lives_ok {
    my $obj = Class2->new;
    is($obj->value, 10, "weak_ref doesn't apply to non-refs");
};

my $obj2 = Class2->new;
lives_ok {
    $obj2->value({});
};

is_deeply($obj2->value, undef, "weakened the reference even with a nonref default");

do {
    package Class3;
    use Mouse;

    has hashref => (
        is        => 'rw',
        default   => sub { {} },
        weak_ref  => 1,
        predicate => 'has_hashref',
    );
};

my $obj = Class3->new;
is($obj->hashref, undef, "hashref collected immediately because refcount=0");
ok($obj->has_hashref, 'attribute is turned into undef, not deleted from instance');

$obj->hashref({1 => 1});
is($obj->hashref, undef, "hashref collected between set and get because refcount=0");
ok($obj->has_hashref, 'attribute is turned into undef, not deleted from instance');

ok(Class3->meta->get_attribute('hashref')->is_weak_ref, "hashref IS a weakref");