11 use POSIX qw
(assert
);
13 BEGIN { push @INC, '.' if '.' ne '.'; }
22 foreach $test_vector (Test
::test_vector
())
24 my ($test_name, $flags, $in_spec, $expected, $e_ret_code, $rest) =
26 die "$0: wrong number of elements in test $test_name\n"
27 if (!defined $e_ret_code || defined $rest);
28 assert
(!ref $test_name);
30 assert
(!ref $e_ret_code);
32 die "$0: duplicate test name \`$test_name'\n"
33 if (defined $seen{$test_name});
34 $seen{$test_name} = 1;
38 # Given a spec for the input file(s) or expected output file of a single
39 # test, create a file for any string. A file is created for each literal
40 # string -- not for named files. Whether a perl `string' is treated as
41 # a string to be put in a file for a test or the name of an existing file
42 # depends on how many references have to be traversed to get from
43 # the top level variable to the actual string literal.
44 # If $SPEC is a literal Perl string (not a reference), then treat $SPEC
45 # as the contents of a file.
46 # If $SPEC is a hash reference, then there are no inputs.
47 # If $SPEC is an array reference, consider each element of the array.
48 # If the element is a string reference, treat the string as the name of
49 # an existing file. Otherwise, the element must be a string and is treated
50 # just like a scalar $SPEC. When a file is created, its name is derived
51 # from the name TEST_NAME of the corresponding test and the TYPE of file.
52 # E.g., the inputs for test `3a' would be named t3a.in1 and t3a.in2, and
53 # the expected output for test `7c' would be named t7c.exp.
55 # Also, return two lists of file names:
56 # - maintainer-generated files -- names of files created by this function
57 # - files named explicitly in Test.pm
59 sub spec_to_list
($$$)
61 my ($spec, $test_name, $type) = @_;
63 assert
($type eq $In || $type eq $Exp);
69 # If SPEC is a hash reference, return empty lists.
70 if (ref $spec eq 'HASH')
72 assert
($type eq $In);
74 EXPLICIT
=> \
@explicit_file,
75 MAINT_GEN
=> \
@maint_gen_file
81 assert
(ref $spec eq 'ARRAY' || ref $spec eq 'HASH');
83 foreach $file_spec (@
$spec)
85 # A file spec may be a string or a reference.
86 # If it's a string, that string is to be the contents of a
87 # generated (by this script) file with name derived from the
89 # If it's a reference, then it must be the name of an existing
93 my $r = ref $file_spec;
94 die "bad test: $test_name is $r\n"
95 if ref $file_spec ne 'SCALAR';
96 my $existing_file = $$file_spec;
97 # FIXME: make sure $existing_file exists somewhere.
98 push (@explicit_file, $existing_file);
102 push (@content_string, $file_spec);
108 push (@content_string, $spec);
113 foreach $file_contents (@content_string)
115 my $suffix = (@content_string > 1 ?
$i : '');
116 my $maint_gen_file = "$test_name$type$suffix";
117 push (@maint_gen_file, $maint_gen_file);
118 open (F
, ">$srcdir/$maint_gen_file") || die "$0: $maint_gen_file: $!\n";
119 print F
$file_contents;
120 close (F
) || die "$0: $maint_gen_file: $!\n";
125 foreach $i (@explicit_file, @maint_gen_file)
128 if (length ($i) > $max_len)
130 warn "$0: $i: generated test file name would be longer than"
131 . " $max_len characters\n";
138 EXPLICIT
=> \
@explicit_file,
139 MAINT_GEN
=> \
@maint_gen_file
147 my ($preferred_line_len, @tok) = @_;
148 assert
($preferred_line_len > 0);
154 if ($line && length ($line) + 1 + length ($word) > $preferred_line_len)
156 push (@lines, $line);
160 my $sp = ($line ?
' ' : '');
163 push (@lines, $line);
167 # ~~~~~~~ main ~~~~~~~~
171 die "Usage: $0: program-name\n" if @ARGV != 1;
178 # Output three lists of files:
179 # EXPLICIT -- file names specified in Test.pm
180 # MAINT_GEN -- maintainer-generated files
181 # RUN_GEN -- files created when running the tests
186 foreach $test_vector (Test
::test_vector
())
188 my ($test_name, $flags, $in_spec, $exp_spec, $e_ret_code)
191 push (@run, ("$test_name$Out", "$test_name$Err"));
193 my $in = spec_to_list
($in_spec, $test_name, $In);
194 push (@exp, @
{$in->{EXPLICIT
}});
195 push (@maint, @
{$in->{MAINT_GEN
}});
197 my $e = spec_to_list
($exp_spec, $test_name, $Exp);
198 push (@exp, @
{$e->{EXPLICIT
}});
199 push (@maint, @
{$e->{MAINT_GEN
}});
202 # The list of explicitly mentioned files may contain duplicates.
203 # Eliminated any duplicates.
204 my %e = map {$_ => 1} @exp;
208 print join (" \\\n", wrap
($len, 'explicit =', @exp)), "\n";
209 print join (" \\\n", wrap
($len, 'maint_gen =', @maint)), "\n";
210 print join (" \\\n", wrap
($len, 'run_gen =', @run)), "\n";
217 # This script was generated automatically by build-script.
222 test "\$VERBOSE" && echo=echo || echo=:
223 \$echo testing program: \$xx
225 test "\$srcdir" || srcdir=.
226 test "\$VERBOSE" && \$xx --version 2> /dev/null
228 # Make sure we get English translations.
242 foreach $test_vector (Test::test_vector ())
244 my ($test_name, $flags, $in_spec, $exp_spec, $e_ret_code)
247 my $in = spec_to_list ($in_spec, $test_name, $In);
249 my @srcdir_rel_in_file;
251 foreach $f (@{$in->{EXPLICIT}}, @{$in->{MAINT_GEN}})
253 push (@srcdir_rel_in_file, "\$srcdir/$f");
256 my $exp = spec_to_list ($exp_spec, $test_name, $Exp);
257 my @all = (@{$exp->{EXPLICIT}}, @{$exp->{MAINT_GEN}});
259 my $exp_name = "\$srcdir/$all[0]";
260 my $out = "$test_name$Out";
261 my $err_output = "$test_name$Err";
263 my %valid_via = map {$_ => 1} qw (REDIR FILE PIPE);
264 my %via_msg_string = (REDIR => '<', FILE => 'F', PIPE => '|');
266 # Inhibit warnings about `used only once'.
267 die if 0 && $Test::input_via{$test_name} && $Test::input_via_default;
268 die if 0 && $Test::env{$test_name} && $Test::env_default;
270 my $vias = $Test::input_via{$test_name} || $Test::input_via_default
273 my $n_vias = keys %$vias;
275 while (($via, $val) = each %$vias)
278 my $via_msg = ($n_vias == 1 ? '' : $via_msg_string{$via});
279 my $file_args = join (' ', @srcdir_rel_in_file);
283 $cmd = "\$xx $flags $file_args > $out 2> $err_output";
285 elsif ($via eq 'PIPE')
287 $via_msg = "|$val" if $val;
289 $cmd = "$val $file_args | \$xx $flags > $out 2> $err_output";
293 assert (@srcdir_rel_in_file == 1);
294 $cmd = "\$xx $flags < $file_args > $out 2> $err_output";
297 my $env = $Test::env{$test_name} || $Test::env_default || [''];
301 my $sep = ($via_msg && $e ? ':' : '');
302 my $msg = "$e$sep$via_msg";
303 $msg = "($msg)" if $msg;
304 my $t_name = "$test_name$msg";
305 my $e_cmd = ($e ? "$e " : '');
310 if test \
$code != $e_ret_code; then
311 \
$echo "Test $t_name failed: $xx return code \$code differs from expected value $e_ret_code" 1>&2
312 errors
=`expr \$errors + 1`
314 cmp $out $exp_name > /dev/null
2>&1
316 0) if test
"\$VERBOSE"; then \
$echo "passed $t_name"; fi
;;
317 1) \
$echo "Test $t_name failed: files $out and $exp_name differ" 1>&2
318 (diff
-c
$out $exp_name) 2> /dev/null
319 errors
=`expr \$errors + 1`;;
320 2) \
$echo "Test $t_name may have failed." 1>&2
321 \
$echo The command
\"cmp $out $exp_name\" failed
. 1>&2
322 errors
=`expr \$errors + 1`;;
325 test
-s
$err_output || rm
-f
$err_output
331 if test \$errors = 0; then
332 \$echo Passed all $n_tests tests. 1>&2
334 \$echo Failed \$errors tests. 1>&2
336 test \$errors = 0 || errors=1