Re: [PATCH] another Storable test (Re: perl@16005)
[p5sagit/p5-mst-13.2.git] / ext / Storable / t / overload.t
1 #!./perl
2
3 # $Id: overload.t,v 1.0.1.1 2001/02/17 12:27:22 ram Exp $
4 #
5 #  Copyright (c) 1995-2000, Raphael Manfredi
6 #  
7 #  You may redistribute only under the same terms as Perl 5, as specified
8 #  in the README file that comes with the distribution.
9 #  
10 # $Log: overload.t,v $
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 #
14 # Revision 1.0  2000/09/01 19:40:42  ram
15 # Baseline for first official release.
16 #
17
18 sub BEGIN {
19     if ($ENV{PERL_CORE}){
20         chdir('t') if -d 't';
21         @INC = '.'; 
22         push @INC, '../lib';
23     }
24     require Config; import Config;
25     if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bStorable\b/) {
26         print "1..0 # Skip: Storable was not built\n";
27         exit 0;
28     }
29     require 'lib/st-dump.pl';
30 }
31
32 sub ok;
33
34 use Storable qw(freeze thaw);
35
36 print "1..12\n";
37
38 package OVERLOADED;
39
40 use overload
41         '""' => sub { $_[0][0] };
42
43 package main;
44
45 $a = bless [77], OVERLOADED;
46
47 $b = thaw freeze $a;
48 ok 1, ref $b eq 'OVERLOADED';
49 ok 2, "$b" eq "77";
50
51 $c = thaw freeze \$a;
52 ok 3, ref $c eq 'REF';
53 ok 4, ref $$c eq 'OVERLOADED';
54 ok 5, "$$c" eq "77";
55
56 $d = thaw freeze [$a, $a];
57 ok 6, "$d->[0]" eq "77";
58 $d->[0][0]++;
59 ok 7, "$d->[1]" eq "78";
60
61 package REF_TO_OVER;
62
63 sub make {
64         my $self = bless {}, shift;
65         my ($over) = @_;
66         $self->{over} = $over;
67         return $self;
68 }
69
70 package OVER;
71
72 use overload
73         '+'             => \&plus,
74         '""'    => sub { ref $_[0] };
75
76 sub plus {
77         return 314;
78 }
79
80 sub make {
81         my $self = bless {}, shift;
82         my $ref = REF_TO_OVER->make($self);
83         $self->{ref} = $ref;
84         return $self;
85 }
86
87 package main;
88
89 $a = OVER->make();
90 $b = thaw freeze $a;
91
92 ok 8, ref $b eq 'OVER';
93 ok 9, $a + $a == 314;
94 ok 10, ref $b->{ref} eq 'REF_TO_OVER';
95 ok 11, "$b->{ref}->{over}" eq "$b";
96 ok 12, $b + $b == 314;
97
98 1;
99