Re: [PATCH] another Storable test (Re: perl@16005)
[p5sagit/p5-mst-13.2.git] / ext / Storable / t / blessed.t
1 #!./perl
2
3 # $Id: blessed.t,v 1.0 2000/09/01 19:40:41 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: blessed.t,v $
11 # Revision 1.0  2000/09/01 19:40:41  ram
12 # Baseline for first official release.
13 #
14
15 sub BEGIN {
16     if ($ENV{PERL_CORE}){
17         chdir('t') if -d 't';
18         @INC = '.'; 
19         push @INC, '../lib';
20     }
21     require Config; import Config;
22     if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bStorable\b/) {
23         print "1..0 # Skip: Storable was not built\n";
24         exit 0;
25     }
26     require 'lib/st-dump.pl';
27 }
28
29 sub ok;
30
31 use Storable qw(freeze thaw);
32
33 print "1..10\n";
34
35 package SHORT_NAME;
36
37 sub make { bless [], shift }
38
39 package SHORT_NAME_WITH_HOOK;
40
41 sub make { bless [], shift }
42
43 sub STORABLE_freeze {
44         my $self = shift;
45         return ("", $self);
46 }
47
48 sub STORABLE_thaw {
49         my $self = shift;
50         my $cloning = shift;
51         my ($x, $obj) = @_;
52         die "STORABLE_thaw" unless $obj eq $self;
53 }
54
55 package main;
56
57 # Still less than 256 bytes, so long classname logic not fully exercised
58 # Wait until Perl removes the restriction on identifier lengths.
59 my $name = "LONG_NAME_" . 'xxxxxxxxxxxxx::' x 14 . "final";
60
61 eval <<EOC;
62 package $name;
63
64 \@ISA = ("SHORT_NAME");
65 EOC
66 die $@ if $@;
67 ok 1, $@ eq '';
68
69 eval <<EOC;
70 package ${name}_WITH_HOOK;
71
72 \@ISA = ("SHORT_NAME_WITH_HOOK");
73 EOC
74 ok 2, $@ eq '';
75
76 # Construct a pool of objects
77 my @pool;
78
79 for (my $i = 0; $i < 10; $i++) {
80         push(@pool, SHORT_NAME->make);
81         push(@pool, SHORT_NAME_WITH_HOOK->make);
82         push(@pool, $name->make);
83         push(@pool, "${name}_WITH_HOOK"->make);
84 }
85
86 my $x = freeze \@pool;
87 ok 3, 1;
88
89 my $y = thaw $x;
90 ok 4, ref $y eq 'ARRAY';
91 ok 5, @{$y} == @pool;
92
93 ok 6, ref $y->[0] eq 'SHORT_NAME';
94 ok 7, ref $y->[1] eq 'SHORT_NAME_WITH_HOOK';
95 ok 8, ref $y->[2] eq $name;
96 ok 9, ref $y->[3] eq "${name}_WITH_HOOK";
97
98 my $good = 1;
99 for (my $i = 0; $i < 10; $i++) {
100         do { $good = 0; last } unless ref $y->[4*$i]   eq 'SHORT_NAME';
101         do { $good = 0; last } unless ref $y->[4*$i+1] eq 'SHORT_NAME_WITH_HOOK';
102         do { $good = 0; last } unless ref $y->[4*$i+2] eq $name;
103         do { $good = 0; last } unless ref $y->[4*$i+3] eq "${name}_WITH_HOOK";
104 }
105 ok 10, $good;
106