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
17 if (($Config::Config{'extensions'} !~ m!\bData/Dumper\b!) ){
18 print "1..0 # Skip -- Perl configured without Data::Dumper module\n";
28 print "1..$numtests\n";
30 my $ref = []; my $ref1 = [];
32 package Boustrophedon; # A class with overloaded "".
33 sub new { my ($c, $s) = @_; bless \$s, $c }
34 use overload '""' => sub { ${$_[0]} . reverse ${$_[0]} };
36 my $ox = Boustrophedon->new("foobar");
38 # Test standard hash functionality, by performing the same operations
39 # on a tied hash and on a normal hash, and checking that the results
40 # are the same. This does of course assume that Perl hashes are not
43 my @tests = standard_hash_tests();
45 my @ordinary_results = runtests(\@tests, undef);
46 foreach my $class ('Tie::RefHash', 'Tie::RefHash::Nestable') {
47 my @tied_results = runtests(\@tests, $class);
50 die if @ordinary_results != @tied_results;
51 foreach my $i (0 .. $#ordinary_results) {
52 my ($or, $ow, $oe) = @{$ordinary_results[$i]};
53 my ($tr, $tw, $te) = @{$tied_results[$i]};
57 $ok = 0 if (defined($or) != defined($tr)) or ($or ne $tr);
58 $ok = 0 if (defined($ow) != defined($tw)) or ($ow ne $tw);
59 $ok = 0 if (defined($oe) != defined($te)) or ($oe ne $te);
63 "failed for $class: $tests[$i]\n",
64 "ordinary hash gave:\n",
65 defined $or ? "\tresult: $or\n" : "\tundef result\n",
66 defined $ow ? "\twarning: $ow\n" : "\tno warning\n",
67 defined $oe ? "\texception: $oe\n" : "\tno exception\n",
68 "tied $class hash gave:\n",
69 defined $tr ? "\tresult: $tr\n" : "\tundef result\n",
70 defined $tw ? "\twarning: $tw\n" : "\tno warning\n",
71 defined $te ? "\texception: $te\n" : "\tno exception\n",
79 # Now test Tie::RefHash's special powers
81 $h = eval { tie %h, 'Tie::RefHash' };
84 test(ref($h) eq 'Tie::RefHash');
85 test(defined(tied(%h)) and tied(%h) =~ /^Tie::RefHash/);
87 test($h{$ref} eq 'cholet');
88 test(exists $h{$ref});
90 test(ref((keys %h)[0]) eq 'ARRAY');
91 test((keys %h)[0] eq $ref);
92 test((values %h) == 1);
93 test((values %h)[0] eq 'cholet');
95 while (my ($k, $v) = each %h) {
97 test(ref($k) eq 'ARRAY');
103 test(not defined $h{$ref});
104 test(not exists($h{$ref}));
105 test((keys %h) == 0);
106 test((values %h) == 0);
107 $h{$ox} = "bellow"; # overloaded ""
108 test(exists $h{$ox});
109 test($h{$ox} eq "bellow");
110 test(not exists $h{"foobarraboof"});
114 # And now Tie::RefHash::Nestable's differences from Tie::RefHash.
115 $h = eval { tie %h, 'Tie::RefHash::Nestable' };
118 test(ref($h) eq 'Tie::RefHash::Nestable');
119 test(defined(tied(%h)) and tied(%h) =~ /^Tie::RefHash::Nestable/);
120 $h{$ref}->{$ref1} = 'bungo';
121 test($h{$ref}->{$ref1} eq 'bungo');
123 # Test that the nested hash is also tied (for current implementation)
124 test(defined(tied(%{$h{$ref}}))
125 and tied(%{$h{$ref}}) =~ /^Tie::RefHash::Nestable=/ );
127 test((keys %h) == 1);
128 test((keys %h)[0] eq $ref);
129 test((keys %{$h{$ref}}) == 1);
130 test((keys %{$h{$ref}})[0] eq $ref1);
133 die "expected to run $numtests tests, but ran ", $currtest - 1
134 if $currtest - 1 != $numtests;
143 # Print 'ok X' if true, 'not ok X' if false
144 # Uses global $currtest.
148 print 'not ' if not $t;
149 print 'ok ', $currtest++, "\n";
153 # Wrapper for Data::Dumper to 'dump' a scalar as an EXPR string.
157 $d =~ s/^\$VAR1 =\s*//;
163 # Crudely dump a hash into a canonical string representation (because
164 # hash keys can appear in any order, Data::Dumper may give different
165 # strings for the same hash).
170 foreach (sort keys %$h) {
171 $r = dumped($_) . ' => ' . dumped($h->{$_}) . "\n";
176 # Run the tests and give results.
178 # Parameters: reference to list of tests to run
179 # name of class to use for tied hash, or undef if not tied
181 # Returns: list of [R, W, E] tuples, one for each test.
182 # R is the return value from running the test, W any warnings it gave,
183 # and E any exception raised with 'die'. E and W will be tidied up a
184 # little to remove irrelevant details like line numbers :-)
186 # Will also run a few of its own 'ok N' tests.
189 my ($tests, $class) = @_;
193 if (defined $class) {
194 $h = eval { tie %h, $class };
197 test(ref($h) eq $class);
198 test(defined(tied(%h)) and tied(%h) =~ /^\Q$class\E/);
202 my ($result, $warning, $exception);
203 local $SIG{__WARN__} = sub { $warning .= $_[0] };
204 $result = scalar(eval $_);
207 die "$@:$_" unless defined $class;
211 foreach ($warning, $exception) {
213 s/ at .+ line \d+\.$//mg;
214 s/ at .+ line \d+, at .*//mg;
215 s/ at .+ line \d+, near .*//mg;
216 s/(uninitialized value)( within)? [\$@%].*? in /$1 in /g;
219 my (@warnings, %seen);
220 foreach (split /\n/, $warning) {
221 push @warnings, $_ unless $seen{$_}++;
223 $warning = join("\n", @warnings);
225 push @r, [ $result, $warning, $exception ];
232 # Things that should work just the same for an ordinary hash and a
235 # Each test is a code string to be eval'd, it should do something with
236 # %h and give a scalar return value. The global $ref and $ref1 may
239 # One thing we don't test is that the ordering from 'keys', 'values'
240 # and 'each' is the same. You can't reasonably expect that.
242 sub standard_hash_tests {
245 # Library of standard tests on keys, values and each
246 my $STD_TESTS = <<'END'
247 join $;, sort keys %h;
248 join $;, sort values %h;
249 { my ($v, %tmp); $tmp{$v}++ while (defined($v = each %h)); dumph(\%tmp) }
250 { my ($k, $v, %tmp); $tmp{"$k$;$v"}++ while (($k, $v) = each %h); dumph(\%tmp) }
254 # Tests on the existence of the element 'foo'
255 my $FOO_TESTS = <<'END'
262 # Test storing and deleting 'foo'
263 push @r, split /\n/, <<"END"
278 # Test storing and removing under ordinary keys
279 my @things = ('boink', 0, 1, '', undef);
280 foreach my $key (map { dumped($_) } @things) {
281 foreach my $value ((map { dumped($_) } @things), '$ref') {
282 push @r, split /\n/, <<"END"
300 @slicetests = split /\n/, <<'END'
303 @h{'e'} = ('f', 'g');
305 @h{'j', 'k'} = ('l');
306 @h{'m', 'n'} = ('o', 'p');
307 @h{'q', 'r'} = ('s', 't', 'u');
310 my @aaa = @slicetests;
311 foreach (@slicetests) {
313 push @r, split(/\n/, $STD_TESTS);
317 push @r, '%h = ();', split(/\n/, $STD_TESTS);