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