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