File: recycled.t

package info (click to toggle)
libobject-extend-perl 0.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 96 kB
  • sloc: perl: 445; makefile: 2
file content (122 lines) | stat: -rw-r--r-- 3,391 bytes parent folder | download
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
#!/usr/bin/env perl

use strict;
use warnings;

use constant {
    BAR         => { bar => sub { 'Bar' } },
    BAZ         => { baz => sub { 'Baz' } },
    NO_RECYCLED => "Can't get a recycled reference",
    ROUNDS      => 1000,
};

use Object::Extend qw(extend SINGLETON);
use Scalar::Util qw(refaddr);
use Test::More tests => 31;

sub foo { 'Foo' }

# try to trigger the reuse of a refaddr (i.e. C pointer)
# and return the first object that uses a recycled refaddr
sub recycle(;%) {
    my %options = @_;
    my ($original_methods, $recycled_methods) = @options{qw(original recycled)};
    my ($recycled, %seen);

    for (1 .. ROUNDS) {
        my $object = bless {};
        my $refaddr = refaddr($object);

        if ($seen{$refaddr}) {
            $recycled = $object;

            if ($recycled_methods) {
                extend $recycled => $recycled_methods;
            }

            last;
        } else {
            if ($original_methods) {
                extend $object => $original_methods;
            }

            $seen{$refaddr} = 1;
            undef $object;
        }
    }

    return $recycled;
}

# sanity check the base case: neither the original nor
# the recycled are extended
SKIP: {
    my $recycled = recycle();
    skip NO_RECYCLED, 5 unless ($recycled);
    isa_ok $recycled, __PACKAGE__;
    can_ok $recycled, 'foo';
    ok !$recycled->isa(SINGLETON);
    ok !$recycled->can('bar');
    ok !$recycled->can('baz');
};

# make sure the unextended recycled isn't contaminated by
# the extended original
SKIP: {
    my $recycled = recycle(original => BAR);
    skip NO_RECYCLED, 5 unless ($recycled);
    isa_ok $recycled, __PACKAGE__;
    can_ok $recycled, 'foo';
    ok !$recycled->isa(SINGLETON);
    ok !$recycled->can('bar');
    ok !$recycled->can('baz');
};

# for completeness, make sure the recycled is sane if the
# original wasn't extended
SKIP: {
    my $recycled = recycle(recycled => BAR);
    skip NO_RECYCLED, 5 unless ($recycled);
    isa_ok $recycled, __PACKAGE__;
    isa_ok $recycled, SINGLETON;
    can_ok $recycled, 'foo';
    can_ok $recycled, 'bar';
    ok !$recycled->can('baz');
};

# make sure there are no surprises if we extend the recycled
# in the same way that we've extended the original
SKIP: {
    my $recycled = recycle(original => BAR, recycled => BAR);
    skip NO_RECYCLED, 5 unless ($recycled);
    isa_ok $recycled, __PACKAGE__;
    isa_ok $recycled, SINGLETON;
    can_ok $recycled, 'foo';
    can_ok $recycled, 'bar';
    ok !$recycled->can('baz');
};

# define bar in the original and redefine it to return a different value
# in recycled. make sure $recycled->bar returns the overridden value
SKIP: {
    my $recycled = recycle(original => BAR, recycled => { bar => sub { 'Bar 2' } });
    skip NO_RECYCLED, 6 unless ($recycled);
    isa_ok $recycled, __PACKAGE__;
    isa_ok $recycled, SINGLETON;
    can_ok $recycled, 'foo';
    can_ok $recycled, 'bar';
    ok !$recycled->can('baz');
    is $recycled->bar, 'Bar 2';
};

# extend the original with bar and the recycled with baz
# and make sure the recycled isn't contaminated by bar
SKIP: {
    my $recycled = recycle(original => BAR, recycled => BAZ);
    skip NO_RECYCLED, 5 unless ($recycled);
    isa_ok $recycled, __PACKAGE__;
    isa_ok $recycled, SINGLETON;
    can_ok $recycled, 'foo';
    can_ok $recycled, 'baz';
    ok !$recycled->can('bar');
};