Blame t/circular_hook.t

Packit 14c646
#!./perl -w
Packit 14c646
#
Packit 14c646
#  Copyright 2005, Adam Kennedy.
Packit 14c646
#
Packit 14c646
#  You may redistribute only under the same terms as Perl 5, as specified
Packit 14c646
#  in the README file that comes with the distribution.
Packit 14c646
#
Packit 14c646
Packit 14c646
# Man, blessed.t scared the hell out of me. For a second there I thought
Packit 14c646
# I'd lose Test::More...
Packit 14c646
Packit 14c646
# This file tests several known-error cases relating to STORABLE_attach, in
Packit 14c646
# which Storable should (correctly) throw errors.
Packit 14c646
Packit 14c646
sub BEGIN {
Packit 14c646
    unshift @INC, 't';
Packit 14c646
    unshift @INC, 't/compat' if $] < 5.006002;
Packit 14c646
    require Config; import Config;
Packit 14c646
    if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bStorable\b/) {
Packit 14c646
        print "1..0 # Skip: Storable was not built\n";
Packit 14c646
        exit 0;
Packit 14c646
    }
Packit 14c646
}
Packit 14c646
Packit 14c646
use Storable ();
Packit 14c646
use Test::More tests => 9;
Packit 14c646
Packit 14c646
my $ddd = bless { }, 'Foo';
Packit 14c646
my $eee = bless { Bar => $ddd }, 'Bar';
Packit 14c646
$ddd->{Foo} = $eee;
Packit 14c646
Packit 14c646
my $array = [ $ddd ];
Packit 14c646
Packit 14c646
my $string = Storable::freeze( $array );
Packit 14c646
my $thawed = Storable::thaw( $string );
Packit 14c646
Packit 14c646
# is_deeply infinite loops in circulars, so do it manually
Packit 14c646
# is_deeply( $array, $thawed, 'Circular hooked objects work' );
Packit 14c646
is( ref($thawed), 'ARRAY', 'Top level ARRAY' );
Packit 14c646
is( scalar(@$thawed), 1, 'ARRAY contains one element' );
Packit 14c646
isa_ok( $thawed->[0], 'Foo' );
Packit 14c646
is( scalar(keys %{$thawed->[0]}), 1, 'Foo contains one element' );
Packit 14c646
isa_ok( $thawed->[0]->{Foo}, 'Bar' );
Packit 14c646
is( scalar(keys %{$thawed->[0]->{Foo}}), 1, 'Bar contains one element' );
Packit 14c646
isa_ok( $thawed->[0]->{Foo}->{Bar}, 'Foo' );
Packit 14c646
is( $thawed->[0], $thawed->[0]->{Foo}->{Bar}, 'Circular is... well... circular' );
Packit 14c646
Packit 14c646
# Make sure the thawing went the way we expected
Packit 14c646
is_deeply( \@Foo::order, [ 'Bar', 'Foo' ], 'thaw order is correct (depth first)' );
Packit 14c646
Packit 14c646
Packit 14c646
Packit 14c646
Packit 14c646
Packit 14c646
package Foo;
Packit 14c646
Packit 14c646
@order = ();
Packit 14c646
Packit 14c646
sub STORABLE_freeze {
Packit 14c646
	my ($self, $clone) = @_;
Packit 14c646
	my $class = ref $self;
Packit 14c646
	
Packit 14c646
	# print "# Freezing $class\n";
Packit 14c646
Packit 14c646
	return ($class, $self->{$class});
Packit 14c646
}
Packit 14c646
Packit 14c646
sub STORABLE_thaw {
Packit 14c646
	my ($self, $clone, $string, @refs) = @_;
Packit 14c646
	my $class = ref $self;
Packit 14c646
Packit 14c646
	# print "# Thawing $class\n";
Packit 14c646
Packit 14c646
	$self->{$class} = shift @refs;
Packit 14c646
Packit 14c646
	push @order, $class;
Packit 14c646
Packit 14c646
 	return;
Packit 14c646
}
Packit 14c646
Packit 14c646
package Bar;
Packit 14c646
Packit 14c646
BEGIN {
Packit 14c646
@ISA = 'Foo';
Packit 14c646
}
Packit 14c646
Packit 14c646
1;