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.
30 require Config; import Config;
31 if ($Config{'extensions'} !~ /\bStorable\b/) {
32 print "1..0 # Skip: Storable was not built\n";
35 require 'lib/st-dump.pl';
40 use Storable qw(freeze thaw dclone);
46 use Storable qw(freeze thaw);
50 sub make { bless [], shift }
55 die "STORABLE_freeze" unless Storable::is_storing;
56 return (freeze(\@x), $self);
63 die "STORABLE_thaw #1" unless $obj eq $self;
66 die "STORABLE_thaw #2" unless ref $a eq 'ARRAY';
67 die "STORABLE_thaw #3" unless @$a == 2 && $a->[0] eq 'a' && $a->[1] == 1;
69 die "STORABLE_thaw #4" unless Storable::is_retrieving;
76 sub make { bless {}, shift }
82 return ("", \@x, $self);
87 my ($cloning, $undef, $a, $obj) = @_;
88 die "STORABLE_thaw #1" unless $obj eq $self;
89 die "STORABLE_thaw #2" unless ref $a eq 'ARRAY' || @$a != 2;
95 use Storable qw(dclone);
98 my $self = bless {}, shift;
100 $self->{sync} = OBJ_SYNC->make;
105 sub STORABLE_freeze {
109 my $t = dclone($r->{sync});
110 return ("", [$t, $self->{ext}], $r, $self, $r->{ext});
115 my ($cloning, $undef, $a, $r, $obj, $ext) = @_;
116 die "STORABLE_thaw #1" unless $obj eq $self;
117 die "STORABLE_thaw #2" unless ref $a eq 'ARRAY';
118 die "STORABLE_thaw #3" unless ref $r eq 'HASH';
119 die "STORABLE_thaw #4" unless $a->[1] == $r->{ext};
121 ($self->{sync}, $self->{ext}) = @$a;
126 use Storable qw(freeze thaw);
132 sub make { bless [], shift }
134 sub STORABLE_freeze {
137 return (freeze($self), $self) if ++$recursed < $MAX;
138 return ("no", $self);
145 die "STORABLE_thaw #1" unless $obj eq $self;
146 $self->[0] = thaw($x) if $x ne "no";
152 my $real = OBJ_REAL->make;
153 my $x = freeze $real;
158 ok 3, $y->[0] eq 'a';
161 my $sync = OBJ_SYNC->make;
167 ok 7, $y->{ok} == $y;
170 $sync = OBJ_SYNC2->make($ext);
171 $x = freeze [$sync, $ext];
177 ok 10, $y->{ok} == $y;
178 ok 11, ref $y->{sync} eq 'OBJ_SYNC';
179 ok 12, $y->{ext} == $z->[1];
181 $real = OBJ_REAL2->make;
184 ok 14, $OBJ_REAL2::recursed == $OBJ_REAL2::MAX;
185 ok 15, $OBJ_REAL2::hook_called == $OBJ_REAL2::MAX;
189 ok 17, $OBJ_REAL2::recursed == 0;
193 ok 19, ref $x eq 'OBJ_REAL2';
194 ok 20, $OBJ_REAL2::recursed == 0;
195 ok 21, $OBJ_REAL2::hook_called == 2 * $OBJ_REAL2::MAX;
197 ok 22, !Storable::is_storing;
198 ok 23, !Storable::is_retrieving;
201 # The following was a test-case that Salvador Ortiz Garcia <sog@msg.com.mx>
202 # sent me, along with a proposed fix.
210 return bless {dat => $dat}, $class;
220 Foo->new(2), # Second instance of a Foo
225 sub STORABLE_freeze {
226 my($self,$clonning) = @_;
227 return "$self->{a}", $self->{b};
231 my($self,$clonning,$dummy,$o) = @_;
239 my $bar2 = thaw freeze $bar;
241 ok 24, ref($bar2) eq 'Bar';
242 ok 25, ref($bar->{b}[0]) eq 'Foo';
243 ok 26, ref($bar->{b}[1]) eq 'Foo';
244 ok 27, ref($bar2->{b}[0]) eq 'Foo';
245 ok 28, ref($bar2->{b}[1]) eq 'Foo';
248 # The following attempts to make sure blessed objects are blessed ASAP
255 my $self = bless {}, shift;
262 my $self = bless {}, shift;
264 $self->{c1} = CLASS_1->make();
266 $self->{c3} = bless CLASS_1->make(), "CLASS_3";
271 sub STORABLE_freeze {
272 my($self, $clonning) = @_;
273 return "", $self->{c1}, $self->{c3}, $self->{o};
277 my($self, $clonning, $frozen, $c1, $c3, $o) = @_;
278 main::ok 29, ref $self eq "CLASS_2";
279 main::ok 30, ref $c1 eq "CLASS_1";
280 main::ok 31, ref $c3 eq "CLASS_3";
281 main::ok 32, ref $o eq "CLASS_OTHER";
289 my $self = bless {}, shift;
293 sub set_c2 { $_[0]->{c2} = $_[1] }
297 my $o = CLASS_OTHER->make();
298 my $c2 = CLASS_2->make($o);
299 my $so = thaw freeze $o;