3 # Copyright (c) 1995-2000, Raphael Manfredi
5 # You may redistribute only under the same terms as Perl 5, as specified
6 # in the README file that comes with the distribution.
12 @INC = ('.', '../lib', '../ext/Storable/t');
16 require Config; import Config;
17 if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bStorable\b/) {
18 print "1..0 # Skip: Storable was not built\n";
26 use Storable qw(freeze thaw);
35 my $tests = $test + 6 + 2 * 6 * keys %::immortals;
40 sub make { bless [], shift }
42 package SHORT_NAME_WITH_HOOK;
44 sub make { bless [], shift }
55 die "STORABLE_thaw" unless $obj eq $self;
60 # Still less than 256 bytes, so long classname logic not fully exercised
61 # Wait until Perl removes the restriction on identifier lengths.
62 my $name = "LONG_NAME_" . 'xxxxxxxxxxxxx::' x 14 . "final";
67 \@ISA = ("SHORT_NAME");
73 package ${name}_WITH_HOOK;
75 \@ISA = ("SHORT_NAME_WITH_HOOK");
79 # Construct a pool of objects
82 for (my $i = 0; $i < 10; $i++) {
83 push(@pool, SHORT_NAME->make);
84 push(@pool, SHORT_NAME_WITH_HOOK->make);
85 push(@pool, $name->make);
86 push(@pool, "${name}_WITH_HOOK"->make);
89 my $x = freeze \@pool;
93 ok 4, ref $y eq 'ARRAY';
96 ok 6, ref $y->[0] eq 'SHORT_NAME';
97 ok 7, ref $y->[1] eq 'SHORT_NAME_WITH_HOOK';
98 ok 8, ref $y->[2] eq $name;
99 ok 9, ref $y->[3] eq "${name}_WITH_HOOK";
102 for (my $i = 0; $i < 10; $i++) {
103 do { $good = 0; last } unless ref $y->[4*$i] eq 'SHORT_NAME';
104 do { $good = 0; last } unless ref $y->[4*$i+1] eq 'SHORT_NAME_WITH_HOOK';
105 do { $good = 0; last } unless ref $y->[4*$i+2] eq $name;
106 do { $good = 0; last } unless ref $y->[4*$i+3] eq "${name}_WITH_HOOK";
111 my $blessed_ref = bless \\[1,2,3], 'Foobar';
112 my $x = freeze $blessed_ref;
114 ok 11, ref $y eq 'Foobar';
115 ok 12, $$$y->[0] == 1;
118 package RETURNS_IMMORTALS;
120 sub make { my $self = shift; bless [@_], $self }
122 sub STORABLE_freeze {
123 # Some reference some number of times.
125 my ($what, $times) = @$self;
126 return ("$what$times", ($::immortals{$what}) x $times);
133 my ($what, $times) = $x =~ /(.)(\d+)/;
134 die "'$x' didn't match" unless defined $times;
135 main::ok ++$test, @refs == $times;
136 my $expect = $::immortals{$what};
137 die "'$x' did not give a reference" unless ref $expect;
140 $fail++ if $_ != $expect;
142 main::ok ++$test, !$fail;
147 # $Storable::DEBUGME = 1;
149 foreach $count (1..3) {
151 foreach $immortal (keys %::immortals) {
152 print "# $immortal x $count\n";
153 my $i = RETURNS_IMMORTALS->make ($immortal, $count);
162 # Test automatic require of packages to find thaw hook.
173 sub STORABLE_freeze {
180 my $f = freeze (HAS_HOOK->make);
182 ok ++$test, $HAS_HOOK::loaded_count == 0;
183 ok ++$test, $HAS_HOOK::thawed_count == 0;
186 ok ++$test, $HAS_HOOK::loaded_count == 1;
187 ok ++$test, $HAS_HOOK::thawed_count == 1;
189 ok ++$test, ref $t eq 'HAS_HOOK';
191 # Can't do this because the method is still cached by UNIVERSAL::can
192 # delete $INC{"HAS_HOOK.pm"};
193 # undef &HAS_HOOK::STORABLE_thaw;
195 # warn HAS_HOOK->can('STORABLE_thaw');
197 # ok ++$test, $HAS_HOOK::loaded_count == 2;
198 # ok ++$test, $HAS_HOOK::thawed_count == 2;
200 # ok ++$test, ref $t eq 'HAS_HOOK';