[PATCH] Re: Storable 2.0.0 fails on vendor perl on Mac OS X 10.1
[p5sagit/p5-mst-13.2.git] / ext / Storable / t / tied_hook.t
CommitLineData
7a6a85bf 1#!./perl
2
b12202d0 3# $Id: tied_hook.t,v 1.0.1.1 2001/02/17 12:29:01 ram Exp $
7a6a85bf 4#
5# Copyright (c) 1995-2000, Raphael Manfredi
6#
9e21b3d0 7# You may redistribute only under the same terms as Perl 5, as specified
8# in the README file that comes with the distribution.
7a6a85bf 9#
10# $Log: tied_hook.t,v $
b12202d0 11# Revision 1.0.1.1 2001/02/17 12:29:01 ram
12# patch8: added test for blessed ref to tied hash
13#
9e21b3d0 14# Revision 1.0 2000/09/01 19:40:42 ram
15# Baseline for first official release.
7a6a85bf 16#
17
18sub BEGIN {
0c384302 19 if ($ENV{PERL_CORE}){
20 chdir('t') if -d 't';
7dadce44 21 @INC = ('.', '../lib', '../ext/Storable/t');
372cb964 22 } else {
23 unshift @INC, 't';
0c384302 24 }
9f233367 25 require Config; import Config;
0c384302 26 if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bStorable\b/) {
9f233367 27 print "1..0 # Skip: Storable was not built\n";
28 exit 0;
29 }
372cb964 30 require 'st-dump.pl';
7a6a85bf 31}
32
33sub ok;
34
35use Storable qw(freeze thaw);
36
b12202d0 37print "1..25\n";
7a6a85bf 38
39($scalar_fetch, $array_fetch, $hash_fetch) = (0, 0, 0);
40
41package TIED_HASH;
42
43sub TIEHASH {
44 my $self = bless {}, shift;
45 return $self;
46}
47
48sub FETCH {
49 my $self = shift;
50 my ($key) = @_;
51 $main::hash_fetch++;
52 return $self->{$key};
53}
54
55sub STORE {
56 my $self = shift;
57 my ($key, $value) = @_;
58 $self->{$key} = $value;
59}
60
61sub FIRSTKEY {
62 my $self = shift;
63 scalar keys %{$self};
64 return each %{$self};
65}
66
67sub NEXTKEY {
68 my $self = shift;
69 return each %{$self};
70}
71
72sub STORABLE_freeze {
73 my $self = shift;
74 $main::hash_hook1++;
75 return join(":", keys %$self) . ";" . join(":", values %$self);
76}
77
78sub STORABLE_thaw {
79 my ($self, $cloning, $frozen) = @_;
80 my ($keys, $values) = split(/;/, $frozen);
81 my @keys = split(/:/, $keys);
82 my @values = split(/:/, $values);
83 for (my $i = 0; $i < @keys; $i++) {
84 $self->{$keys[$i]} = $values[$i];
85 }
86 $main::hash_hook2++;
87}
88
89package TIED_ARRAY;
90
91sub TIEARRAY {
92 my $self = bless [], shift;
93 return $self;
94}
95
96sub FETCH {
97 my $self = shift;
98 my ($idx) = @_;
99 $main::array_fetch++;
100 return $self->[$idx];
101}
102
103sub STORE {
104 my $self = shift;
105 my ($idx, $value) = @_;
106 $self->[$idx] = $value;
107}
108
109sub FETCHSIZE {
110 my $self = shift;
111 return @{$self};
112}
113
114sub STORABLE_freeze {
115 my $self = shift;
116 $main::array_hook1++;
117 return join(":", @$self);
118}
119
120sub STORABLE_thaw {
121 my ($self, $cloning, $frozen) = @_;
122 @$self = split(/:/, $frozen);
123 $main::array_hook2++;
124}
125
126package TIED_SCALAR;
127
128sub TIESCALAR {
129 my $scalar;
130 my $self = bless \$scalar, shift;
131 return $self;
132}
133
134sub FETCH {
135 my $self = shift;
136 $main::scalar_fetch++;
137 return $$self;
138}
139
140sub STORE {
141 my $self = shift;
142 my ($value) = @_;
143 $$self = $value;
144}
145
146sub STORABLE_freeze {
147 my $self = shift;
148 $main::scalar_hook1++;
149 return $$self;
150}
151
152sub STORABLE_thaw {
153 my ($self, $cloning, $frozen) = @_;
154 $$self = $frozen;
155 $main::scalar_hook2++;
156}
157
158package main;
159
160$a = 'toto';
161$b = \$a;
162
163$c = tie %hash, TIED_HASH;
164$d = tie @array, TIED_ARRAY;
165tie $scalar, TIED_SCALAR;
166
167$scalar = 'foo';
168$hash{'attribute'} = 'plain value';
169$array[0] = \$scalar;
170$array[1] = $c;
171$array[2] = \@array;
172$array[3] = "plaine scalaire";
173
174@tied = (\$scalar, \@array, \%hash);
175%a = ('key', 'value', 1, 0, $a, $b, 'cvar', \$a, 'scalarref', \$scalar);
176@a = ('first', 3, -4, -3.14159, 456, 4.5, $d, \$d,
177 $b, \$a, $a, $c, \$c, \%a, \@array, \%hash, \@tied);
178
179ok 1, defined($f = freeze(\@a));
180
181$dumped = &dump(\@a);
182ok 2, 1;
183
184$root = thaw($f);
185ok 3, defined $root;
186
187$got = &dump($root);
188ok 4, 1;
189
190ok 5, $got ne $dumped; # our hooks did not handle refs in array
191
192$g = freeze($root);
193ok 6, length($f) == length($g);
194
195# Ensure the tied items in the retrieved image work
196@old = ($scalar_fetch, $array_fetch, $hash_fetch);
197@tied = ($tscalar, $tarray, $thash) = @{$root->[$#{$root}]};
198@type = qw(SCALAR ARRAY HASH);
199
200ok 7, tied $$tscalar;
201ok 8, tied @{$tarray};
202ok 9, tied %{$thash};
203
204@new = ($$tscalar, $tarray->[0], $thash->{'attribute'});
205@new = ($scalar_fetch, $array_fetch, $hash_fetch);
206
207# Tests 10..15
208for ($i = 0; $i < @new; $i++) {
209 ok 10 + 2*$i, $new[$i] == $old[$i] + 1; # Tests 10,12,14
210 ok 11 + 2*$i, ref $tied[$i] eq $type[$i]; # Tests 11,13,15
211}
212
213ok 16, $$tscalar eq 'foo';
214ok 17, $tarray->[3] eq 'plaine scalaire';
215ok 18, $thash->{'attribute'} eq 'plain value';
216
217# Ensure hooks were called
218ok 19, ($scalar_hook1 && $scalar_hook2);
219ok 20, ($array_hook1 && $array_hook2);
220ok 21, ($hash_hook1 && $hash_hook2);
221
b12202d0 222#
223# And now for the "blessed ref to tied hash" with "store hook" test...
224#
225
226my $bc = bless \%hash, 'FOO'; # FOO does not exist -> no hook
227my $bx = thaw freeze $bc;
228
229ok 22, ref $bx eq 'FOO';
230my $old_hash_fetch = $hash_fetch;
231my $v = $bx->{attribute};
232ok 23, $hash_fetch == $old_hash_fetch + 1; # Still tied
233
234package TIED_HASH_REF;
235
236
237sub STORABLE_freeze {
238 my ($self, $cloning) = @_;
239 return if $cloning;
240 return('ref lost');
241}
242
243sub STORABLE_thaw {
244 my ($self, $cloning, $data) = @_;
245 return if $cloning;
246}
247
248package main;
249
250$bc = bless \%hash, 'TIED_HASH_REF';
251$bx = thaw freeze $bc;
252
253ok 24, ref $bx eq 'TIED_HASH_REF';
254$old_hash_fetch = $hash_fetch;
255$v = $bx->{attribute};
256ok 25, $hash_fetch == $old_hash_fetch + 1; # Still tied
257