summaryrefslogtreecommitdiff
path: root/dist/Storable/t/weak.t
blob: 0a06b0dcf62f70abc25fd12c3cc0cb7dcf30829b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!./perl -w
#
#  Copyright 2004, Larry Wall.
#
#  You may redistribute only under the same terms as Perl 5, as specified
#  in the README file that comes with the distribution.
#

sub BEGIN {
  # This lets us distribute Test::More in t/
  unshift @INC, 't';
  unshift @INC, 't/compat' if $] < 5.006002;
  require Config; import Config;
  if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bStorable\b/) {
    print "1..0 # Skip: Storable was not built\n";
    exit 0;
  }
  if ($Config{extensions} !~ /\bList\/Util\b/) {
    print "1..0 # Skip: List::Util was not built\n";
    exit 0;
  }

  require Scalar::Util;
  Scalar::Util->import(qw(weaken isweak));
  if (grep { /weaken/ } @Scalar::Util::EXPORT_FAIL) {
    print("1..0 # Skip: No support for weaken in Scalar::Util\n");
    exit 0;
  }
}

use Test::More 'no_plan';
use Storable qw (store retrieve freeze thaw nstore nfreeze);
require 'testlib.pl';
use vars '$file';
use strict;

sub tester {
  my ($contents, $sub, $testersub, $what) = @_;
  # Test that if we re-write it, everything still works:
  my $clone = &$sub ($contents);
  is ($@, "", "There should be no error extracting for $what");
  &$testersub ($clone, $what);
}

my $r = {};
my $s1 = [$r, $r];
weaken $s1->[1];
ok (isweak($s1->[1]), "element 1 is a weak reference");

my $s0 = [$r, $r];
weaken $s0->[0];
ok (isweak($s0->[0]), "element 0 is a weak reference");

my $w = [$r];
weaken $w->[0];
ok (isweak($w->[0]), "element 0 is a weak reference");

package OVERLOADED;

use overload
	'""' => sub { $_[0][0] };

package main;

$a = bless [77], 'OVERLOADED';

my $o = [$a, $a];
weaken $o->[0];
ok (isweak($o->[0]), "element 0 is a weak reference");

my @tests = (
[$s1,
 sub  {
  my ($clone, $what) = @_;
  isa_ok($clone,'ARRAY');
  isa_ok($clone->[0],'HASH');
  isa_ok($clone->[1],'HASH');
  ok(!isweak $clone->[0], "Element 0 isn't weak");
  ok(isweak $clone->[1], "Element 1 is weak");
}
],
# The weak reference needs to hang around long enough for other stuff to
# be able to make references to it. So try it second.
[$s0,
 sub  {
  my ($clone, $what) = @_;
  isa_ok($clone,'ARRAY');
  isa_ok($clone->[0],'HASH');
  isa_ok($clone->[1],'HASH');
  ok(isweak $clone->[0], "Element 0 is weak");
  ok(!isweak $clone->[1], "Element 1 isn't weak");
}
],
[$w,
 sub  {
  my ($clone, $what) = @_;
  isa_ok($clone,'ARRAY');
  if ($what eq 'nothing') {
    # We're the original, so we're still a weakref to a hash
    isa_ok($clone->[0],'HASH');
    ok(isweak $clone->[0], "Element 0 is weak");
  } else {
    is($clone->[0],undef);
  }
}
],
[$o,
sub {
  my ($clone, $what) = @_;
  isa_ok($clone,'ARRAY');
  isa_ok($clone->[0],'OVERLOADED');
  isa_ok($clone->[1],'OVERLOADED');
  ok(isweak $clone->[0], "Element 0 is weak");
  ok(!isweak $clone->[1], "Element 1 isn't weak");
  is ("$clone->[0]", 77, "Element 0 stringifies to 77");
  is ("$clone->[1]", 77, "Element 1 stringifies to 77");
}
],
);

foreach (@tests) {
  my ($input, $testsub) = @$_;

  tester($input, sub {return shift}, $testsub, 'nothing');

  ok (defined store($input, $file));

  # Read the contents into memory:
  my $contents = slurp ($file);

  tester($contents, \&store_and_retrieve, $testsub, 'file');

  # And now try almost everything again with a Storable string
  my $stored = freeze $input;
  tester($stored, \&freeze_and_thaw, $testsub, 'string');

  ok (defined nstore($input, $file));

  tester($contents, \&store_and_retrieve, $testsub, 'network file');

  $stored = nfreeze $input;
  tester($stored, \&freeze_and_thaw, $testsub, 'network string');
}