xref: /openbsd-src/gnu/usr.bin/perl/dist/Storable/t/overload.t (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1#!./perl
2#
3#  Copyright (c) 1995-2000, Raphael Manfredi
4#
5#  You may redistribute only under the same terms as Perl 5, as specified
6#  in the README file that comes with the distribution.
7#
8
9sub BEGIN {
10    unshift @INC, 't';
11    unshift @INC, 't/compat' if $] < 5.006002;
12    require Config; import Config;
13    if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bStorable\b/) {
14        print "1..0 # Skip: Storable was not built\n";
15        exit 0;
16    }
17}
18
19use Storable qw(freeze thaw);
20
21use Test::More tests => 19;
22
23package OVERLOADED;
24
25use overload
26	'""' => sub { $_[0][0] };
27
28package main;
29
30$a = bless [77], OVERLOADED;
31
32$b = thaw freeze $a;
33is(ref $b, 'OVERLOADED');
34is("$b", "77");
35
36$c = thaw freeze \$a;
37is(ref $c, 'REF');
38is(ref $$c, 'OVERLOADED');
39is("$$c", "77");
40
41$d = thaw freeze [$a, $a];
42is("$d->[0]", "77");
43$d->[0][0]++;
44is("$d->[1]", "78");
45
46package REF_TO_OVER;
47
48sub make {
49	my $self = bless {}, shift;
50	my ($over) = @_;
51	$self->{over} = $over;
52	return $self;
53}
54
55package OVER;
56
57use overload
58	'+'		=> \&plus,
59	'""'	=> sub { ref $_[0] };
60
61sub plus {
62	return 314;
63}
64
65sub make {
66	my $self = bless {}, shift;
67	my $ref = REF_TO_OVER->make($self);
68	$self->{ref} = $ref;
69	return $self;
70}
71
72package main;
73
74$a = OVER->make();
75$b = thaw freeze $a;
76
77is(ref $b, 'OVER');
78is($a + $a, 314);
79is(ref $b->{ref}, 'REF_TO_OVER');
80is("$b->{ref}->{over}", "$b");
81is($b + $b, 314);
82
83# nfreeze data generated by make_overload.pl
84my $f = '';
85if (ord ('A') == 193) { # EBCDIC.
86    $f = unpack 'u', q{7!084$0S(P>)MUN7%V=/6P<0*!**5EJ8`};
87}else {
88    $f = unpack 'u', q{7!084$0Q(05-?3U9%4DQ/040*!'-N;W<`};
89}
90
91# see note at the end of do_retrieve in Storable.xs about why this test has to
92# use a reference to an overloaded reference, rather than just a reference.
93my $t = eval {thaw $f};
94print "# $@" if $@;
95is($@, "");
96is(ref ($t), 'REF');
97is(ref ($$t), 'HAS_OVERLOAD');
98is($$$t, 'snow');
99
100
101#---
102# blessed reference to overloaded object.
103{
104  my $a = bless [88], 'OVERLOADED';
105  my $c = thaw freeze bless \$a, 'main';
106  is(ref $c, 'main');
107  is(ref $$c, 'OVERLOADED');
108  is("$$c", "88");
109
110}
111
1121;
113