3 # Basic test suite for Tie::RefHash and Tie::RefHash::Nestable.
5 # The testing is in two parts: first, run lots of tests on both a tied
6 # hash and an ordinary un-tied hash, and check they give the same
7 # answer. Then there are tests for those cases where the tied hashes
8 # should behave differently to normal hashes, that is, when using
23 print "1..$numtests\n";
25 my $ref = []; my $ref1 = [];
27 # Test standard hash functionality, by performing the same operations
28 # on a tied hash and on a normal hash, and checking that the results
29 # are the same. This does of course assume that Perl hashes are not
32 my @tests = standard_hash_tests();
34 my @ordinary_results = runtests(\@tests, undef);
35 foreach my $class ('Tie::RefHash', 'Tie::RefHash::Nestable') {
36 my @tied_results = runtests(\@tests, $class);
39 die if @ordinary_results != @tied_results;
40 foreach my $i (0 .. $#ordinary_results) {
41 my ($or, $ow, $oe) = @{$ordinary_results[$i]};
42 my ($tr, $tw, $te) = @{$tied_results[$i]};
46 $ok = 0 if (defined($or) != defined($tr)) or ($or ne $tr);
47 $ok = 0 if (defined($ow) != defined($tw)) or ($ow ne $tw);
48 $ok = 0 if (defined($oe) != defined($te)) or ($oe ne $te);
52 "failed for $class: $tests[$i]\n",
53 "ordinary hash gave:\n",
54 defined $or ? "\tresult: $or\n" : "\tundef result\n",
55 defined $ow ? "\twarning: $ow\n" : "\tno warning\n",
56 defined $oe ? "\texception: $oe\n" : "\tno exception\n",
57 "tied $class hash gave:\n",
58 defined $tr ? "\tresult: $tr\n" : "\tundef result\n",
59 defined $tw ? "\twarning: $tw\n" : "\tno warning\n",
60 defined $te ? "\texception: $te\n" : "\tno exception\n",
68 # Now test Tie::RefHash's special powers
70 $h = eval { tie %h, 'Tie::RefHash' };
73 test(ref($h) eq 'Tie::RefHash');
74 test(defined(tied(%h)) and tied(%h) =~ /^Tie::RefHash/);
76 test($h{$ref} eq 'cholet');
77 test(exists $h{$ref});
79 test(ref((keys %h)[0]) eq 'ARRAY');
80 test((keys %h)[0] eq $ref);
81 test((values %h) == 1);
82 test((values %h)[0] eq 'cholet');
84 while (my ($k, $v) = each %h) {
86 test(ref($k) eq 'ARRAY');
92 test(not defined $h{$ref});
93 test(not exists($h{$ref}));
95 test((values %h) == 0);
99 # And now Tie::RefHash::Nestable's differences from Tie::RefHash.
100 $h = eval { tie %h, 'Tie::RefHash::Nestable' };
103 test(ref($h) eq 'Tie::RefHash::Nestable');
104 test(defined(tied(%h)) and tied(%h) =~ /^Tie::RefHash::Nestable/);
105 $h{$ref}->{$ref1} = 'bungo';
106 test($h{$ref}->{$ref1} eq 'bungo');
108 # Test that the nested hash is also tied (for current implementation)
109 test(defined(tied(%{$h{$ref}}))
110 and tied(%{$h{$ref}}) =~ /^Tie::RefHash::Nestable=/ );
112 test((keys %h) == 1);
113 test((keys %h)[0] eq $ref);
114 test((keys %{$h{$ref}}) == 1);
115 test((keys %{$h{$ref}})[0] eq $ref1);
118 die "expected to run $numtests tests, but ran ", $currtest - 1
119 if $currtest - 1 != $numtests;
128 # Print 'ok X' if true, 'not ok X' if false
129 # Uses global $currtest.
133 print 'not ' if not $t;
134 print 'ok ', $currtest++, "\n";
138 # Wrapper for Data::Dumper to 'dump' a scalar as an EXPR string.
142 $d =~ s/^\$VAR1 =\s*//;
148 # Crudely dump a hash into a canonical string representation (because
149 # hash keys can appear in any order, Data::Dumper may give different
150 # strings for the same hash).
155 foreach (sort keys %$h) {
156 $r = dumped($_) . ' => ' . dumped($h->{$_}) . "\n";
161 # Run the tests and give results.
163 # Parameters: reference to list of tests to run
164 # name of class to use for tied hash, or undef if not tied
166 # Returns: list of [R, W, E] tuples, one for each test.
167 # R is the return value from running the test, W any warnings it gave,
168 # and E any exception raised with 'die'. E and W will be tidied up a
169 # little to remove irrelevant details like line numbers :-)
171 # Will also run a few of its own 'ok N' tests.
174 my ($tests, $class) = @_;
178 if (defined $class) {
179 $h = eval { tie %h, $class };
182 test(ref($h) eq $class);
183 test(defined(tied(%h)) and tied(%h) =~ /^\Q$class\E/);
187 my ($result, $warning, $exception);
188 local $SIG{__WARN__} = sub { $warning .= $_[0] };
189 $result = scalar(eval $_);
192 die "$@:$_" unless defined $class;
196 foreach ($warning, $exception) {
198 s/ at .+ line \d+\.$//mg;
199 s/ at .+ line \d+, at .*//mg;
200 s/ at .+ line \d+, near .*//mg;
203 my (@warnings, %seen);
204 foreach (split /\n/, $warning) {
205 push @warnings, $_ unless $seen{$_}++;
207 $warning = join("\n", @warnings);
209 push @r, [ $result, $warning, $exception ];
216 # Things that should work just the same for an ordinary hash and a
219 # Each test is a code string to be eval'd, it should do something with
220 # %h and give a scalar return value. The global $ref and $ref1 may
223 # One thing we don't test is that the ordering from 'keys', 'values'
224 # and 'each' is the same. You can't reasonably expect that.
226 sub standard_hash_tests {
229 # Library of standard tests on keys, values and each
230 my $STD_TESTS = <<'END'
231 join $;, sort keys %h;
232 join $;, sort values %h;
233 { my ($v, %tmp); $tmp{$v}++ while (defined($v = each %h)); dumph(\%tmp) }
234 { my ($k, $v, %tmp); $tmp{"$k$;$v"}++ while (($k, $v) = each %h); dumph(\%tmp) }
238 # Tests on the existence of the element 'foo'
239 my $FOO_TESTS = <<'END'
246 # Test storing and deleting 'foo'
247 push @r, split /\n/, <<"END"
262 # Test storing and removing under ordinary keys
263 my @things = ('boink', 0, 1, '', undef);
264 foreach my $key (map { dumped($_) } @things) {
265 foreach my $value ((map { dumped($_) } @things), '$ref') {
266 push @r, split /\n/, <<"END"
284 @slicetests = split /\n/, <<'END'
287 @h{'e'} = ('f', 'g');
289 @h{'j', 'k'} = ('l');
290 @h{'m', 'n'} = ('o', 'p');
291 @h{'q', 'r'} = ('s', 't', 'u');
294 my @aaa = @slicetests;
295 foreach (@slicetests) {
297 push @r, split(/\n/, $STD_TESTS);
301 push @r, '%h = ();', split(/\n/, $STD_TESTS);