Blame t/attach_singleton.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
# Tests freezing/thawing structures containing Singleton objects,
Packit 14c646
# which should see both structs pointing to the same object.
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 Test::More tests => 16;
Packit 14c646
use Storable ();
Packit 14c646
Packit 14c646
# Get the singleton
Packit 14c646
my $object = My::Singleton->new;
Packit 14c646
isa_ok( $object, 'My::Singleton' );
Packit 14c646
Packit 14c646
# Confirm (for the record) that the class is actually a Singleton
Packit 14c646
my $object2 = My::Singleton->new;
Packit 14c646
isa_ok( $object2, 'My::Singleton' );
Packit 14c646
is( "$object", "$object2", 'Class is a singleton' );
Packit 14c646
Packit 14c646
############
Packit 14c646
# Main Tests
Packit 14c646
Packit 14c646
my $struct = [ 1, $object, 3 ];
Packit 14c646
Packit 14c646
# Freeze the struct
Packit 14c646
my $frozen = Storable::freeze( $struct );
Packit 14c646
ok( (defined($frozen) and ! ref($frozen) and length($frozen)), 'freeze returns a string' );
Packit 14c646
Packit 14c646
# Thaw the struct
Packit 14c646
my $thawed = Storable::thaw( $frozen );
Packit 14c646
Packit 14c646
# Now it should look exactly like the original
Packit 14c646
is_deeply( $struct, $thawed, 'Struct superficially looks like the original' );
Packit 14c646
Packit 14c646
# ... EXCEPT that the Singleton should be the same instance of the object
Packit 14c646
is( "$struct->[1]", "$thawed->[1]", 'Singleton thaws correctly' );
Packit 14c646
Packit 14c646
# We can also test this empirically
Packit 14c646
$struct->[1]->{value} = 'Goodbye cruel world!';
Packit 14c646
is_deeply( $struct, $thawed, 'Empiric testing confirms correct behaviour' );
Packit 14c646
Packit 14c646
$struct = [ $object, $object ];
Packit 14c646
$frozen = Storable::freeze($struct);
Packit 14c646
$thawed = Storable::thaw($frozen);
Packit 14c646
is("$thawed->[0]", "$thawed->[1]", "Multiple Singletons thaw correctly");
Packit 14c646
Packit 14c646
# End Tests
Packit 14c646
###########
Packit 14c646
Packit 14c646
package My::Singleton;
Packit 14c646
Packit 14c646
my $SINGLETON = undef;
Packit 14c646
Packit 14c646
sub new {
Packit 14c646
	$SINGLETON or
Packit 14c646
	$SINGLETON = bless { value => 'Hello World!' }, $_[0];
Packit 14c646
}
Packit 14c646
Packit 14c646
sub STORABLE_freeze {
Packit 14c646
	my $self = shift;
Packit 14c646
Packit 14c646
	# We don't actually need to return anything, but provide a null string
Packit 14c646
	# to avoid the null-list-return behaviour.
Packit 14c646
	return ('foo');
Packit 14c646
}
Packit 14c646
Packit 14c646
sub STORABLE_attach {
Packit 14c646
	my ($class, $clone, $string) = @_;
Packit 14c646
	Test::More::ok( ! ref $class, 'STORABLE_attach passed class, and not an object' );
Packit 14c646
	Test::More::is( $class, 'My::Singleton', 'STORABLE_attach is passed the correct class name' );
Packit 14c646
	Test::More::is( $clone, 0, 'We are not in a dclone' );
Packit 14c646
	Test::More::is( $string, 'foo', 'STORABLE_attach gets the string back' );
Packit 14c646
Packit 14c646
	# Get the Singleton object and return it
Packit 14c646
	return $class->new;
Packit 14c646
}