Commit | Line | Data |
7a6a85bf |
1 | #!./perl |
2 | |
b12202d0 |
3 | # $Id: overload.t,v 1.0.1.1 2001/02/17 12:27:22 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: overload.t,v $ |
b12202d0 |
11 | # Revision 1.0.1.1 2001/02/17 12:27:22 ram |
12 | # patch8: added test for structures with indirect ref to overloaded |
13 | # |
9e21b3d0 |
14 | # Revision 1.0 2000/09/01 19:40:42 ram |
15 | # Baseline for first official release. |
7a6a85bf |
16 | # |
17 | |
18 | sub 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 | |
33 | sub ok; |
34 | |
35 | use Storable qw(freeze thaw); |
36 | |
b12202d0 |
37 | print "1..12\n"; |
7a6a85bf |
38 | |
39 | package OVERLOADED; |
40 | |
41 | use overload |
42 | '""' => sub { $_[0][0] }; |
43 | |
44 | package main; |
45 | |
46 | $a = bless [77], OVERLOADED; |
47 | |
48 | $b = thaw freeze $a; |
49 | ok 1, ref $b eq 'OVERLOADED'; |
50 | ok 2, "$b" eq "77"; |
51 | |
52 | $c = thaw freeze \$a; |
53 | ok 3, ref $c eq 'REF'; |
54 | ok 4, ref $$c eq 'OVERLOADED'; |
55 | ok 5, "$$c" eq "77"; |
56 | |
57 | $d = thaw freeze [$a, $a]; |
58 | ok 6, "$d->[0]" eq "77"; |
59 | $d->[0][0]++; |
60 | ok 7, "$d->[1]" eq "78"; |
61 | |
b12202d0 |
62 | package REF_TO_OVER; |
63 | |
64 | sub make { |
65 | my $self = bless {}, shift; |
66 | my ($over) = @_; |
67 | $self->{over} = $over; |
68 | return $self; |
69 | } |
70 | |
71 | package OVER; |
72 | |
73 | use overload |
74 | '+' => \&plus, |
75 | '""' => sub { ref $_[0] }; |
76 | |
77 | sub plus { |
78 | return 314; |
79 | } |
80 | |
81 | sub make { |
82 | my $self = bless {}, shift; |
83 | my $ref = REF_TO_OVER->make($self); |
84 | $self->{ref} = $ref; |
85 | return $self; |
86 | } |
87 | |
88 | package main; |
89 | |
90 | $a = OVER->make(); |
91 | $b = thaw freeze $a; |
92 | |
93 | ok 8, ref $b eq 'OVER'; |
94 | ok 9, $a + $a == 314; |
95 | ok 10, ref $b->{ref} eq 'REF_TO_OVER'; |
96 | ok 11, "$b->{ref}->{over}" eq "$b"; |
97 | ok 12, $b + $b == 314; |
98 | |
99 | 1; |
100 | |