3 # Copyright 2004, Larry Wall.
5 # You may redistribute only under the same terms as Perl 5, as specified
6 # in the README file that comes with the distribution.
10 # This lets us distribute Test::More in t/
12 require Config; import Config;
13 if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bStorable\b/) {
14 print "1..0 # Skip: Storable was not built\n";
17 if ($Config{extensions} !~ /\bList\/Util\b/) {
18 print "1..0 # Skip: List::Util was not built\n";
23 Scalar::Util->import(qw(weaken isweak));
24 if (grep { /weaken/ } @Scalar::Util::EXPORT_FAIL) {
25 print("1..0 # Skip: No support for weaken in Scalar::Util\n");
30 use Test::More 'no_plan';
31 use Storable qw (store retrieve freeze thaw nstore nfreeze);
37 my ($contents, $sub, $testersub, $what) = @_;
38 # Test that if we re-write it, everything still works:
39 my $clone = &$sub ($contents);
40 is ($@, "", "There should be no error extracting for $what");
41 &$testersub ($clone, $what);
47 ok (isweak($s1->[1]), "element 1 is a weak reference");
51 ok (isweak($s0->[0]), "element 0 is a weak reference");
55 ok (isweak($w->[0]), "element 0 is a weak reference");
60 '""' => sub { $_[0][0] };
64 $a = bless [77], 'OVERLOADED';
68 ok (isweak($o->[0]), "element 0 is a weak reference");
73 my ($clone, $what) = @_;
74 isa_ok($clone,'ARRAY');
75 isa_ok($clone->[0],'HASH');
76 isa_ok($clone->[1],'HASH');
77 ok(!isweak $clone->[0], "Element 0 isn't weak");
78 ok(isweak $clone->[1], "Element 1 is weak");
81 # The weak reference needs to hang around long enough for other stuff to
82 # be able to make references to it. So try it second.
85 my ($clone, $what) = @_;
86 isa_ok($clone,'ARRAY');
87 isa_ok($clone->[0],'HASH');
88 isa_ok($clone->[1],'HASH');
89 ok(isweak $clone->[0], "Element 0 is weak");
90 ok(!isweak $clone->[1], "Element 1 isn't weak");
95 my ($clone, $what) = @_;
96 isa_ok($clone,'ARRAY');
97 if ($what eq 'nothing') {
98 # We're the original, so we're still a weakref to a hash
99 isa_ok($clone->[0],'HASH');
100 ok(isweak $clone->[0], "Element 0 is weak");
102 is($clone->[0],undef);
108 my ($clone, $what) = @_;
109 isa_ok($clone,'ARRAY');
110 isa_ok($clone->[0],'OVERLOADED');
111 isa_ok($clone->[1],'OVERLOADED');
112 ok(isweak $clone->[0], "Element 0 is weak");
113 ok(!isweak $clone->[1], "Element 1 isn't weak");
114 is ("$clone->[0]", 77, "Element 0 stringifies to 77");
115 is ("$clone->[1]", 77, "Element 1 stringifies to 77");
121 my ($input, $testsub) = @$_;
123 tester($input, sub {return shift}, $testsub, 'nothing');
125 ok (defined store($input, $file));
127 # Read the contents into memory:
128 my $contents = slurp ($file);
130 tester($contents, \&store_and_retrieve, $testsub, 'file');
132 # And now try almost everything again with a Storable string
133 my $stored = freeze $input;
134 tester($stored, \&freeze_and_thaw, $testsub, 'string');
136 ok (defined nstore($input, $file));
138 tester($contents, \&store_and_retrieve, $testsub, 'network file');
140 $stored = nfreeze $input;
141 tester($stored, \&freeze_and_thaw, $testsub, 'network string');