Add Storable 0.7.2 from Raphael Manfredi,
[p5sagit/p5-mst-13.2.git] / t / lib / st-blessed.t
1 #!./perl
2
3 # $Id: blessed.t,v 0.7 2000/08/03 22:04:44 ram Exp $
4 #
5 #  Copyright (c) 1995-2000, Raphael Manfredi
6 #  
7 #  You may redistribute only under the terms of the Artistic License,
8 #  as specified in the README file that comes with the distribution.
9 #
10 # $Log: blessed.t,v $
11 # Revision 0.7  2000/08/03 22:04:44  ram
12 # Baseline for second beta release.
13 #
14
15 sub BEGIN {
16     chdir('t') if -d 't';
17     unshift @INC, '../lib';
18     require 'lib/st-dump.pl';
19 }
20
21 sub ok;
22
23 use Storable qw(freeze thaw);
24
25 print "1..10\n";
26
27 package SHORT_NAME;
28
29 sub make { bless [], shift }
30
31 package SHORT_NAME_WITH_HOOK;
32
33 sub make { bless [], shift }
34
35 sub STORABLE_freeze {
36         my $self = shift;
37         return ("", $self);
38 }
39
40 sub STORABLE_thaw {
41         my $self = shift;
42         my $cloning = shift;
43         my ($x, $obj) = @_;
44         die "STORABLE_thaw" unless $obj eq $self;
45 }
46
47 package main;
48
49 # Still less than 256 bytes, so long classname logic not fully exercised
50 # Wait until Perl removes the restriction on identifier lengths.
51 my $name = "LONG_NAME_" . 'xxxxxxxxxxxxx::' x 14 . "final";
52
53 eval <<EOC;
54 package $name;
55
56 \@ISA = ("SHORT_NAME");
57 EOC
58 die $@ if $@;
59 ok 1, $@ eq '';
60
61 eval <<EOC;
62 package ${name}_WITH_HOOK;
63
64 \@ISA = ("SHORT_NAME_WITH_HOOK");
65 EOC
66 ok 2, $@ eq '';
67
68 # Construct a pool of objects
69 my @pool;
70
71 for (my $i = 0; $i < 10; $i++) {
72         push(@pool, SHORT_NAME->make);
73         push(@pool, SHORT_NAME_WITH_HOOK->make);
74         push(@pool, $name->make);
75         push(@pool, "${name}_WITH_HOOK"->make);
76 }
77
78 my $x = freeze \@pool;
79 ok 3, 1;
80
81 my $y = thaw $x;
82 ok 4, ref $y eq 'ARRAY';
83 ok 5, @{$y} == @pool;
84
85 ok 6, ref $y->[0] eq 'SHORT_NAME';
86 ok 7, ref $y->[1] eq 'SHORT_NAME_WITH_HOOK';
87 ok 8, ref $y->[2] eq $name;
88 ok 9, ref $y->[3] eq "${name}_WITH_HOOK";
89
90 my $good = 1;
91 for (my $i = 0; $i < 10; $i++) {
92         do { $good = 0; last } unless ref $y->[4*$i]   eq 'SHORT_NAME';
93         do { $good = 0; last } unless ref $y->[4*$i+1] eq 'SHORT_NAME_WITH_HOOK';
94         do { $good = 0; last } unless ref $y->[4*$i+2] eq $name;
95         do { $good = 0; last } unless ref $y->[4*$i+3] eq "${name}_WITH_HOOK";
96 }
97 ok 10, $good;
98