File: 02_object.t

package info (click to toggle)
libdata-clone-perl 0.006-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 208 kB
  • sloc: perl: 669; makefile: 8; ansic: 5
file content (124 lines) | stat: -rw-r--r-- 2,570 bytes parent folder | download | duplicates (5)
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
123
124
#!perl -w

use strict;
use warnings FATAL => 'all';

use Test::More;

use Data::Clone;

{
    package MyBase;

    sub new {
        my $class = shift;
        return bless {@_}, $class;
    }

    package MyNoclonable;
    our @ISA = qw(MyBase);

    package MyClonable;
    use Data::Clone;
    our @ISA = qw(MyBase);

    package MyCustomClonable;
    use Data::Clone qw(data_clone);
    our @ISA = qw(MyBase);

    sub clone {
        my $cloned = data_clone(@_);
        $cloned->{bar} = 42;
        return $cloned;
    }

    package FatalClonable;
    our @ISA = qw(MyBase);

    sub clone {
        die 'FATAL';
    }
}

for(1 .. 2){ # do it twice to test internal data
    note($_);

    my($o, $c);

    $o = MyNoclonable->new(foo => 10);

    eval {
        local $Data::Clone::ObjectCallback = sub{ die 'Non-clonable object' };
        $c = clone($o);
    };
    like $@, qr/Non-clonable object/, 'die on non-clonables';
    is $c, undef;

    eval {
        $c = clone($o);
    };

    is $@, '';
    is $c, $o;
    $c->{foo}++;
    is $o->{foo}, 11, 'noclonable with surface copy';

    $o = MyClonable->new(foo => 10);
    $c = clone($o);
    isnt $c, $o;
    $c->{foo}++;
    is $o->{foo}, 10, 'clonable';

    $o = MyCustomClonable->new(foo => 10);
    $c = clone($o);
    isnt $c, $o;

    $c->{foo}++;
    is $o->{foo}, 10, 'clonable';
    is_deeply $c, { foo => 11, bar => 42 }, 'custom clone()';

    $o = MyClonable->new(
        aaa => [[42], MyCustomClonable->new(value => 100)],
        bbb => [[42], MyCustomClonable->new(value => 200)],
    );
    $c = clone($o);

    $c->{aaa}[1]{value}++;
    $c->{bbb}[1]{value}++;

    is $o->{aaa}[1]{value}, 100, 'clone() is reentrant';
    is $c->{aaa}[1]{value}, 101;
    is $c->{aaa}[1]{bar},    42;

    is $o->{bbb}[1]{value}, 200, 'clone() is reentrant';
    is $c->{bbb}[1]{value}, 201;
    is $c->{bbb}[1]{bar},    42;

    $o = MyCustomClonable->new();
    $o->{ccc} = [MyCustomClonable->new(value => 300)];
    $o->{ddd} = $o->{ccc};

    $c = clone($o);
    $c->{ccc}[0]{value}++;
    $c->{ddd}[0]{value}++;

    is $o->{ccc}[0]{value}, 300;
    is $c->{ccc}[0]{value}, 302;
    is $c->{ccc}[0]{bar},   42,  'clone methods in clone()';

    $o = FatalClonable->new(foo => 10);
    eval{
        clone($o);
    };
    like $@, qr/^FATAL \b/xms, 'FATAL in clone()';
    is $o->{foo}, 10;

    $o = MyCustomClonable->new(value => FatalClonable->new(foo => 10));
    eval{
        clone($o);
    };
    like $@, qr/^FATAL \b/xms, 'FATAL in clone()';
    is $o->{value}{foo}, 10;
}

done_testing;