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.
32 require Config; import Config;
33 if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bStorable\b/) {
34 print "1..0 # Skip: Storable was not built\n";
37 require 'lib/st-dump.pl';
42 use Storable qw(freeze thaw dclone);
48 use Storable qw(freeze thaw);
52 sub make { bless [], shift }
57 die "STORABLE_freeze" unless Storable::is_storing;
58 return (freeze(\@x), $self);
65 die "STORABLE_thaw #1" unless $obj eq $self;
68 die "STORABLE_thaw #2" unless ref $a eq 'ARRAY';
69 die "STORABLE_thaw #3" unless @$a == 2 && $a->[0] eq 'a' && $a->[1] == 1;
71 die "STORABLE_thaw #4" unless Storable::is_retrieving;
78 sub make { bless {}, shift }
84 return ("", \@x, $self);
89 my ($cloning, $undef, $a, $obj) = @_;
90 die "STORABLE_thaw #1" unless $obj eq $self;
91 die "STORABLE_thaw #2" unless ref $a eq 'ARRAY' || @$a != 2;
97 use Storable qw(dclone);
100 my $self = bless {}, shift;
102 $self->{sync} = OBJ_SYNC->make;
107 sub STORABLE_freeze {
111 my $t = dclone($r->{sync});
112 return ("", [$t, $self->{ext}], $r, $self, $r->{ext});
117 my ($cloning, $undef, $a, $r, $obj, $ext) = @_;
118 die "STORABLE_thaw #1" unless $obj eq $self;
119 die "STORABLE_thaw #2" unless ref $a eq 'ARRAY';
120 die "STORABLE_thaw #3" unless ref $r eq 'HASH';
121 die "STORABLE_thaw #4" unless $a->[1] == $r->{ext};
123 ($self->{sync}, $self->{ext}) = @$a;
128 use Storable qw(freeze thaw);
134 sub make { bless [], shift }
136 sub STORABLE_freeze {
139 return (freeze($self), $self) if ++$recursed < $MAX;
140 return ("no", $self);
147 die "STORABLE_thaw #1" unless $obj eq $self;
148 $self->[0] = thaw($x) if $x ne "no";
154 my $real = OBJ_REAL->make;
155 my $x = freeze $real;
160 ok 3, $y->[0] eq 'a';
163 my $sync = OBJ_SYNC->make;
169 ok 7, $y->{ok} == $y;
172 $sync = OBJ_SYNC2->make($ext);
173 $x = freeze [$sync, $ext];
179 ok 10, $y->{ok} == $y;
180 ok 11, ref $y->{sync} eq 'OBJ_SYNC';
181 ok 12, $y->{ext} == $z->[1];
183 $real = OBJ_REAL2->make;
186 ok 14, $OBJ_REAL2::recursed == $OBJ_REAL2::MAX;
187 ok 15, $OBJ_REAL2::hook_called == $OBJ_REAL2::MAX;
191 ok 17, $OBJ_REAL2::recursed == 0;
195 ok 19, ref $x eq 'OBJ_REAL2';
196 ok 20, $OBJ_REAL2::recursed == 0;
197 ok 21, $OBJ_REAL2::hook_called == 2 * $OBJ_REAL2::MAX;
199 ok 22, !Storable::is_storing;
200 ok 23, !Storable::is_retrieving;
203 # The following was a test-case that Salvador Ortiz Garcia <sog@msg.com.mx>
204 # sent me, along with a proposed fix.
212 return bless {dat => $dat}, $class;
222 Foo->new(2), # Second instance of a Foo
227 sub STORABLE_freeze {
228 my($self,$clonning) = @_;
229 return "$self->{a}", $self->{b};
233 my($self,$clonning,$dummy,$o) = @_;
241 my $bar2 = thaw freeze $bar;
243 ok 24, ref($bar2) eq 'Bar';
244 ok 25, ref($bar->{b}[0]) eq 'Foo';
245 ok 26, ref($bar->{b}[1]) eq 'Foo';
246 ok 27, ref($bar2->{b}[0]) eq 'Foo';
247 ok 28, ref($bar2->{b}[1]) eq 'Foo';
250 # The following attempts to make sure blessed objects are blessed ASAP
257 my $self = bless {}, shift;
264 my $self = bless {}, shift;
266 $self->{c1} = CLASS_1->make();
268 $self->{c3} = bless CLASS_1->make(), "CLASS_3";
273 sub STORABLE_freeze {
274 my($self, $clonning) = @_;
275 return "", $self->{c1}, $self->{c3}, $self->{o};
279 my($self, $clonning, $frozen, $c1, $c3, $o) = @_;
280 main::ok 29, ref $self eq "CLASS_2";
281 main::ok 30, ref $c1 eq "CLASS_1";
282 main::ok 31, ref $c3 eq "CLASS_3";
283 main::ok 32, ref $o eq "CLASS_OTHER";
291 my $self = bless {}, shift;
295 sub set_c2 { $_[0]->{c2} = $_[1] }
299 my $o = CLASS_OTHER->make();
300 my $c2 = CLASS_2->make($o);
301 my $so = thaw freeze $o;