Lots of spring cleaning. (No functional changes.)
[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
11 sub BEGIN {
12     if ($ENV{PERL_CORE}){
13         chdir('t') if -d 't';
14         @INC = ('.', '../lib', '../ext/Storable/t');
15     } else {
16         unshift @INC, 't';
17     }
18     require Config; import Config;
19     if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bStorable\b/) {
20         print "1..0 # Skip: Storable was not built\n";
21         exit 0;
22     }
23     require 'st-dump.pl';
24 }
25
26 sub ok;
27
28 use Storable qw(freeze thaw);
29
30 print "1..12\n";
31
32 package OVERLOADED;
33
34 use overload
35         '""' => sub { $_[0][0] };
36
37 package main;
38
39 $a = bless [77], OVERLOADED;
40
41 $b = thaw freeze $a;
42 ok 1, ref $b eq 'OVERLOADED';
43 ok 2, "$b" eq "77";
44
45 $c = thaw freeze \$a;
46 ok 3, ref $c eq 'REF';
47 ok 4, ref $$c eq 'OVERLOADED';
48 ok 5, "$$c" eq "77";
49
50 $d = thaw freeze [$a, $a];
51 ok 6, "$d->[0]" eq "77";
52 $d->[0][0]++;
53 ok 7, "$d->[1]" eq "78";
54
55 package REF_TO_OVER;
56
57 sub make {
58         my $self = bless {}, shift;
59         my ($over) = @_;
60         $self->{over} = $over;
61         return $self;
62 }
63
64 package OVER;
65
66 use overload
67         '+'             => \&plus,
68         '""'    => sub { ref $_[0] };
69
70 sub plus {
71         return 314;
72 }
73
74 sub make {
75         my $self = bless {}, shift;
76         my $ref = REF_TO_OVER->make($self);
77         $self->{ref} = $ref;
78         return $self;
79 }
80
81 package main;
82
83 $a = OVER->make();
84 $b = thaw freeze $a;
85
86 ok 8, ref $b eq 'OVER';
87 ok 9, $a + $a == 314;
88 ok 10, ref $b->{ref} eq 'REF_TO_OVER';
89 ok 11, "$b->{ref}->{over}" eq "$b";
90 ok 12, $b + $b == 314;
91
92 1;
93