3 # $Id: recurse.t,v 1.0.1.3 2001/02/17 12:28:33 ram Exp $
5 # Copyright (c) 1995-2000, Raphael Manfredi
7 # You may redistribute only under the same terms as Perl 5, as specified
8 # in the README file that comes with the distribution.
11 # Revision 1.0.1.3 2001/02/17 12:28:33 ram
12 # patch8: ensure blessing occurs ASAP, specially designed for hooks
14 # Revision 1.0.1.2 2000/11/05 17:22:05 ram
15 # patch6: stress hook a little more with refs to lexicals
18 # Revision 1.0.1.1 2000/09/17 16:48:05 ram
19 # patch1: added test case for store hook bug
22 # Revision 1.0 2000/09/01 19:40:42 ram
23 # Baseline for first official release.
29 @INC = ('.', '../lib', '../ext/Storable/t');
33 require Config; import Config;
34 if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bStorable\b/) {
35 print "1..0 # Skip: Storable was not built\n";
43 use Storable qw(freeze thaw dclone);
49 use Storable qw(freeze thaw);
53 sub make { bless [], shift }
58 die "STORABLE_freeze" unless Storable::is_storing;
59 return (freeze(\@x), $self);
66 die "STORABLE_thaw #1" unless $obj eq $self;
69 die "STORABLE_thaw #2" unless ref $a eq 'ARRAY';
70 die "STORABLE_thaw #3" unless @$a == 2 && $a->[0] eq 'a' && $a->[1] == 1;
72 die "STORABLE_thaw #4" unless Storable::is_retrieving;
79 sub make { bless {}, shift }
85 return ("", \@x, $self);
90 my ($cloning, $undef, $a, $obj) = @_;
91 die "STORABLE_thaw #1" unless $obj eq $self;
92 die "STORABLE_thaw #2" unless ref $a eq 'ARRAY' || @$a != 2;
98 use Storable qw(dclone);
101 my $self = bless {}, shift;
103 $self->{sync} = OBJ_SYNC->make;
108 sub STORABLE_freeze {
112 my $t = dclone($r->{sync});
113 return ("", [$t, $self->{ext}], $r, $self, $r->{ext});
118 my ($cloning, $undef, $a, $r, $obj, $ext) = @_;
119 die "STORABLE_thaw #1" unless $obj eq $self;
120 die "STORABLE_thaw #2" unless ref $a eq 'ARRAY';
121 die "STORABLE_thaw #3" unless ref $r eq 'HASH';
122 die "STORABLE_thaw #4" unless $a->[1] == $r->{ext};
124 ($self->{sync}, $self->{ext}) = @$a;
129 use Storable qw(freeze thaw);
135 sub make { bless [], shift }
137 sub STORABLE_freeze {
140 return (freeze($self), $self) if ++$recursed < $MAX;
141 return ("no", $self);
148 die "STORABLE_thaw #1" unless $obj eq $self;
149 $self->[0] = thaw($x) if $x ne "no";
155 my $real = OBJ_REAL->make;
156 my $x = freeze $real;
161 ok 3, $y->[0] eq 'a';
164 my $sync = OBJ_SYNC->make;
170 ok 7, $y->{ok} == $y;
173 $sync = OBJ_SYNC2->make($ext);
174 $x = freeze [$sync, $ext];
180 ok 10, $y->{ok} == $y;
181 ok 11, ref $y->{sync} eq 'OBJ_SYNC';
182 ok 12, $y->{ext} == $z->[1];
184 $real = OBJ_REAL2->make;
187 ok 14, $OBJ_REAL2::recursed == $OBJ_REAL2::MAX;
188 ok 15, $OBJ_REAL2::hook_called == $OBJ_REAL2::MAX;
192 ok 17, $OBJ_REAL2::recursed == 0;
196 ok 19, ref $x eq 'OBJ_REAL2';
197 ok 20, $OBJ_REAL2::recursed == 0;
198 ok 21, $OBJ_REAL2::hook_called == 2 * $OBJ_REAL2::MAX;
200 ok 22, !Storable::is_storing;
201 ok 23, !Storable::is_retrieving;
204 # The following was a test-case that Salvador Ortiz Garcia <sog@msg.com.mx>
205 # sent me, along with a proposed fix.
213 return bless {dat => $dat}, $class;
223 Foo->new(2), # Second instance of a Foo
228 sub STORABLE_freeze {
229 my($self,$clonning) = @_;
230 return "$self->{a}", $self->{b};
234 my($self,$clonning,$dummy,$o) = @_;
242 my $bar2 = thaw freeze $bar;
244 ok 24, ref($bar2) eq 'Bar';
245 ok 25, ref($bar->{b}[0]) eq 'Foo';
246 ok 26, ref($bar->{b}[1]) eq 'Foo';
247 ok 27, ref($bar2->{b}[0]) eq 'Foo';
248 ok 28, ref($bar2->{b}[1]) eq 'Foo';
251 # The following attempts to make sure blessed objects are blessed ASAP
258 my $self = bless {}, shift;
265 my $self = bless {}, shift;
267 $self->{c1} = CLASS_1->make();
269 $self->{c3} = bless CLASS_1->make(), "CLASS_3";
274 sub STORABLE_freeze {
275 my($self, $clonning) = @_;
276 return "", $self->{c1}, $self->{c3}, $self->{o};
280 my($self, $clonning, $frozen, $c1, $c3, $o) = @_;
281 main::ok 29, ref $self eq "CLASS_2";
282 main::ok 30, ref $c1 eq "CLASS_1";
283 main::ok 31, ref $c3 eq "CLASS_3";
284 main::ok 32, ref $o eq "CLASS_OTHER";
292 my $self = bless {}, shift;
296 sub set_c2 { $_[0]->{c2} = $_[1] }
300 my $o = CLASS_OTHER->make();
301 my $c2 = CLASS_2->make($o);
302 my $so = thaw freeze $o;