3 # Copyright 2005, Adam Kennedy.
5 # You may redistribute only under the same terms as Perl 5, as specified
6 # in the README file that comes with the distribution.
9 # Tests freezing/thawing structures containing Singleton objects,
10 # which should see both structs pointing to the same object.
14 unshift @INC, 't/compat' if $] < 5.006002;
15 require Config; import Config;
16 if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bStorable\b/) {
17 print "1..0 # Skip: Storable was not built\n";
22 use Test::More tests => 11;
26 my $object = My::Singleton->new;
27 isa_ok( $object, 'My::Singleton' );
29 # Confirm (for the record) that the class is actually a Singleton
30 my $object2 = My::Singleton->new;
31 isa_ok( $object2, 'My::Singleton' );
32 is( "$object", "$object2", 'Class is a singleton' );
37 my $struct = [ 1, $object, 3 ];
40 my $frozen = Storable::freeze( $struct );
41 ok( (defined($frozen) and ! ref($frozen) and length($frozen)), 'freeze returns a string' );
44 my $thawed = Storable::thaw( $frozen );
46 # Now it should look exactly like the original
47 is_deeply( $struct, $thawed, 'Struct superficially looks like the original' );
49 # ... EXCEPT that the Singleton should be the same instance of the object
50 is( "$struct->[1]", "$thawed->[1]", 'Singleton thaws correctly' );
52 # We can also test this empirically
53 $struct->[1]->{value} = 'Goodbye cruel world!';
54 is_deeply( $struct, $thawed, 'Empiric testing confirms correct behaviour' );
59 package My::Singleton;
61 my $SINGLETON = undef;
65 $SINGLETON = bless { value => 'Hello World!' }, $_[0];
71 # We don't actually need to return anything, but provide a null string
72 # to avoid the null-list-return behaviour.
77 my ($class, $clone, $string) = @_;
78 Test::More::ok( ! ref $class, 'STORABLE_attach passed class, and not an object' );
79 Test::More::is( $class, 'My::Singleton', 'STORABLE_attach is passed the correct class name' );
80 Test::More::is( $clone, 0, 'We are not in a dclone' );
81 Test::More::is( $string, 'foo', 'STORABLE_attach gets the string back' );
83 # Get the Singleton object and return it