11 use POSIX qw
(assert
);
13 (my $program_name = $0) =~ s
|.*/||;
15 BEGIN { push @INC, '.' if '.' ne '.'; }
24 foreach $test_vector (Test
::test_vector
())
26 my ($test_name, $flags, $in_spec, $expected, $e_ret_code, $rest) =
28 die "$0: wrong number of elements in test $test_name\n"
29 if (!defined $e_ret_code || defined $rest);
30 assert
(!ref $test_name);
32 assert
(!ref $e_ret_code);
34 die "$0: duplicate test name \`$test_name'\n"
35 if (defined $seen{$test_name});
36 $seen{$test_name} = 1;
40 # Given a spec for the input file(s) or expected output file of a single
41 # test, create a file for any string. A file is created for each literal
42 # string -- not for named files. Whether a perl `string' is treated as
43 # a string to be put in a file for a test or the name of an existing file
44 # depends on how many references have to be traversed to get from
45 # the top level variable to the actual string literal.
46 # If $SPEC is a literal Perl string (not a reference), then treat $SPEC
47 # as the contents of a file.
48 # If $SPEC is a hash reference, then there are no inputs.
49 # If $SPEC is an array reference, consider each element of the array.
50 # If the element is a string reference, treat the string as the name of
51 # an existing file. Otherwise, the element must be a string and is treated
52 # just like a scalar $SPEC. When a file is created, its name is derived
53 # from the name TEST_NAME of the corresponding test and the TYPE of file.
54 # E.g., the inputs for test `3a' would be named t3a.in1 and t3a.in2, and
55 # the expected output for test `7c' would be named t7c.exp.
57 # Also, return two lists of file names:
58 # - maintainer-generated files -- names of files created by this function
59 # - files named explicitly in Test.pm
61 sub spec_to_list
($$$)
63 my ($spec, $test_name, $type) = @_;
65 assert
($type eq $In || $type eq $Exp);
71 # If SPEC is a hash reference, return empty lists.
72 if (ref $spec eq 'HASH')
74 assert
($type eq $In);
76 EXPLICIT
=> \
@explicit_file,
77 MAINT_GEN
=> \
@maint_gen_file
83 assert
(ref $spec eq 'ARRAY' || ref $spec eq 'HASH');
85 foreach $file_spec (@
$spec)
87 # A file spec may be a string or a reference.
88 # If it's a string, that string is to be the contents of a
89 # generated (by this script) file with name derived from the
91 # If it's a reference, then it must be the name of an existing
95 my $r = ref $file_spec;
96 die "bad test: $test_name is $r\n"
97 if ref $file_spec ne 'SCALAR';
98 my $existing_file = $$file_spec;
99 # FIXME: make sure $existing_file exists somewhere.
100 push (@explicit_file, $existing_file);
104 push (@content_string, $file_spec);
110 push (@content_string, $spec);
115 foreach $file_contents (@content_string)
117 my $suffix = (@content_string > 1 ?
$i : '');
118 my $maint_gen_file = "$test_name$type$suffix";
119 push (@maint_gen_file, $maint_gen_file);
120 open (F
, ">$srcdir/$maint_gen_file") || die "$0: $maint_gen_file: $!\n";
121 print F
$file_contents;
122 close (F
) || die "$0: $maint_gen_file: $!\n";
127 foreach $i (@explicit_file, @maint_gen_file)
130 if (length ($i) > $max_len)
132 warn "$0: $i: generated test file name would be longer than"
133 . " $max_len characters\n";
140 EXPLICIT
=> \
@explicit_file,
141 MAINT_GEN
=> \
@maint_gen_file
149 my ($preferred_line_len, @tok) = @_;
150 assert
($preferred_line_len > 0);
156 if ($line && length ($line) + 1 + length ($word) > $preferred_line_len)
158 push (@lines, $line);
162 my $sp = ($line ?
' ' : '');
165 push (@lines, $line);
169 # ~~~~~~~ main ~~~~~~~~
173 die "Usage: $0: program-name\n" if @ARGV != 1;
180 # Output three lists of files:
181 # EXPLICIT -- file names specified in Test.pm
182 # MAINT_GEN -- maintainer-generated files
183 # RUN_GEN -- files created when running the tests
188 foreach $test_vector (Test
::test_vector
())
190 my ($test_name, $flags, $in_spec, $exp_spec, $e_ret_code)
193 push (@run, ("$test_name$Out", "$test_name$Err"));
195 my $in = spec_to_list
($in_spec, $test_name, $In);
196 push (@exp, @
{$in->{EXPLICIT
}});
197 push (@maint, @
{$in->{MAINT_GEN
}});
199 my $e = spec_to_list
($exp_spec, $test_name, $Exp);
200 push (@exp, @
{$e->{EXPLICIT
}});
201 push (@maint, @
{$e->{MAINT_GEN
}});
204 # The list of explicitly mentioned files may contain duplicates.
205 # Eliminated any duplicates.
206 my %e = map {$_ => 1} @exp;
210 print join (" \\\n", wrap
($len, 'explicit =', @exp)), "\n";
211 print join (" \\\n", wrap
($len, 'maint_gen =', @maint)), "\n";
212 print join (" \\\n", wrap
($len, 'run_gen =', @run)), "\n";
219 # This script was generated automatically by $program_name.
224 test "\$VERBOSE" && echo=echo || echo=:
225 \$echo testing program: \$xx
227 test "\$srcdir" || srcdir=.
228 test "\$VERBOSE" && \$xx --version 2> /dev/null
230 # Make sure we get English translations.
244 foreach $test_vector (Test::test_vector ())
246 my ($test_name, $flags, $in_spec, $exp_spec, $e_ret_code)
249 my $in = spec_to_list ($in_spec, $test_name, $In);
251 my @srcdir_rel_in_file;
253 foreach $f (@{$in->{EXPLICIT}}, @{$in->{MAINT_GEN}})
255 push (@srcdir_rel_in_file, "\$srcdir/$f");
258 my $exp = spec_to_list ($exp_spec, $test_name, $Exp);
259 my @all = (@{$exp->{EXPLICIT}}, @{$exp->{MAINT_GEN}});
261 my $exp_name = "\$srcdir/$all[0]";
262 my $out = "$test_name$Out";
263 my $err_output = "$test_name$Err";
265 my %valid_via = map {$_ => 1} qw (REDIR FILE PIPE);
266 my %via_msg_string = (REDIR => '<', FILE => 'F', PIPE => '|');
268 # Inhibit warnings about `used only once'.
269 die if 0 && $Test::input_via{$test_name} && $Test::input_via_default;
270 die if 0 && $Test::env{$test_name} && $Test::env_default;
272 my $vias = $Test::input_via{$test_name} || $Test::input_via_default
275 my $n_vias = keys %$vias;
277 foreach $via (sort keys %$vias)
280 my $val = $vias->{$via};
281 my $via_msg = ($n_vias == 1 ? '' : $via_msg_string{$via});
282 my $file_args = join (' ', @srcdir_rel_in_file);
284 my $env = $Test::env{$test_name} || $Test::env_default || [''];
286 or die "$program_name: unexpected environment: @$env\n";
288 my $env_prefix = ($env ? "$env " : '');
292 $cmd = "$env_prefix\$xx $flags $file_args > $out 2> $err_output";
294 elsif ($via eq 'PIPE')
296 $via_msg = "|$val" if $val;
298 $cmd = "$val $file_args | $env_prefix\$xx $flags"
299 . " > $out 2> $err_output";
303 assert (@srcdir_rel_in_file == 1);
304 $cmd = "$env_prefix\$xx $flags"
305 . " < $file_args > $out 2> $err_output";
309 my $sep = ($via_msg && $e ? ':' : '');
310 my $msg = "$e$sep$via_msg";
311 $msg = "($msg)" if $msg;
312 my $t_name = "$test_name$msg";
317 if test \
$code != $e_ret_code; then
318 \
$echo "Test $t_name failed: $xx return code \$code differs from expected value $e_ret_code" 1>&2
319 errors
=`expr \$errors + 1`
321 cmp $out $exp_name > /dev/null
2>&1
323 0) if test
"\$VERBOSE"; then \
$echo "passed $t_name"; fi
;;
324 1) \
$echo "Test $t_name failed: files $out and $exp_name differ" 1>&2
325 (diff
-c
$out $exp_name) 2> /dev/null
326 errors
=`expr \$errors + 1`;;
327 2) \
$echo "Test $t_name may have failed." 1>&2
328 \
$echo The command
\"cmp $out $exp_name\" failed
. 1>&2
329 errors
=`expr \$errors + 1`;;
332 test
-s
$err_output || rm
-f
$err_output
337 if test \$errors = 0; then
338 \$echo Passed all $n_tests tests. 1>&2
340 \$echo Failed \$errors tests. 1>&2
342 test \$errors = 0 || errors=1