11 use POSIX qw (assert);
13 (my $ME = $0) =~ s|.*/||;
15 BEGIN { push @INC, '.' if '.' ne '.'; }
27 foreach $test_vector (Test::test_vector ())
29 my ($test_name, $flags, $in_spec, $expected, $e_ret_code, $rest) =
31 die "$0: wrong number of elements in test $test_name\n"
32 if (!defined $e_ret_code || defined $rest);
33 assert (!ref $test_name);
35 assert (!ref $e_ret_code);
37 die "$0: duplicate test name \`$test_name'\n"
38 if (defined $seen{$test_name});
39 $seen{$test_name} = 1;
43 my $t8 = lc substr $test_name, 0, 8;
46 warn "$ME: 8.3 test name conflict: "
47 . "$test_name, $seen_8dot3{$t8}\n";
50 $seen_8dot3{$t8} = $test_name;
58 # Given a spec for the input file(s) or expected output file of a single
59 # test, create a file for any string. A file is created for each literal
60 # string -- not for named files. Whether a perl `string' is treated as
61 # a string to be put in a file for a test or the name of an existing file
62 # depends on how many references have to be traversed to get from
63 # the top level variable to the actual string literal.
64 # If $SPEC is a literal Perl string (not a reference), then treat $SPEC
65 # as the contents of a file.
66 # If $SPEC is a hash reference, then there are no inputs.
67 # If $SPEC is an array reference, consider each element of the array.
68 # If the element is a string reference, treat the string as the name of
69 # an existing file. Otherwise, the element must be a string and is treated
70 # just like a scalar $SPEC. When a file is created, its name is derived
71 # from the name TEST_NAME of the corresponding test and the TYPE of file.
72 # E.g., the inputs for test `3a' would be named t3a.in1 and t3a.in2, and
73 # the expected output for test `7c' would be named t7c.exp.
75 # Also, return two lists of file names:
76 # - maintainer-generated files -- names of files created by this function
77 # - files named explicitly in Test.pm
79 sub spec_to_list ($$$)
81 my ($spec, $test_name, $type) = @_;
83 assert ($type eq $In || $type eq $Exp);
89 # If SPEC is a hash reference, return empty lists.
90 if (ref $spec eq 'HASH')
92 assert ($type eq $In);
94 EXPLICIT => \@explicit_file,
95 MAINT_GEN => \@maint_gen_file
101 assert (ref $spec eq 'ARRAY' || ref $spec eq 'HASH');
103 foreach $file_spec (@$spec)
105 # A file spec may be a string or a reference.
106 # If it's a string, that string is to be the contents of a
107 # generated (by this script) file with name derived from the
109 # If it's a reference, then it must be the name of an existing
113 my $r = ref $file_spec;
114 die "bad test: $test_name is $r\n"
115 if ref $file_spec ne 'SCALAR';
116 my $existing_file = $$file_spec;
117 # FIXME: make sure $existing_file exists somewhere.
118 push (@explicit_file, $existing_file);
122 push (@content_string, $file_spec);
128 push (@content_string, $spec);
133 foreach $file_contents (@content_string)
135 my $suffix = (@content_string > 1 ? $i : '');
136 my $maint_gen_file = "$test_name$type$suffix";
137 push (@maint_gen_file, $maint_gen_file);
138 open (F, ">$srcdir/$maint_gen_file") || die "$0: $maint_gen_file: $!\n";
139 print F $file_contents;
140 close (F) || die "$0: $maint_gen_file: $!\n";
145 foreach $i (@explicit_file, @maint_gen_file)
148 if (length ($i) > $max_len)
150 warn "$0: $i: generated test file name would be longer than"
151 . " $max_len characters\n";
158 EXPLICIT => \@explicit_file,
159 MAINT_GEN => \@maint_gen_file
167 my ($preferred_line_len, @tok) = @_;
168 assert ($preferred_line_len > 0);
174 if ($line && length ($line) + 1 + length ($word) > $preferred_line_len)
176 push (@lines, $line);
180 my $sp = ($line ? ' ' : '');
183 push (@lines, $line);
187 # ~~~~~~~ main ~~~~~~~~
191 die "Usage: $0: srcdir program-name\n" if @ARGV != 1;
198 # Output three lists of files:
199 # EXPLICIT -- file names specified in Test.pm
200 # MAINT_GEN -- maintainer-generated files
201 # RUN_GEN -- files created when running the tests
206 foreach $test_vector (Test::test_vector ())
208 my ($test_name, $flags, $in_spec, $exp_spec, $e_ret_code)
211 push (@run, ("$test_name$Out", "$test_name$Err"));
213 my $in = spec_to_list ($in_spec, $test_name, $In);
214 push (@exp, @{$in->{EXPLICIT}});
215 push (@maint, @{$in->{MAINT_GEN}});
217 my $e = spec_to_list ($exp_spec, $test_name, $Exp);
218 push (@exp, @{$e->{EXPLICIT}});
219 push (@maint, @{$e->{MAINT_GEN}});
222 # The list of explicitly mentioned files may contain duplicates.
223 # Eliminated any duplicates.
224 my %e = map {$_ => 1} @exp;
228 print join (" \\\n", wrap ($len, 'explicit =', @exp)), "\n";
229 print join (" \\\n", wrap ($len, 'maint_gen =', @maint)), "\n";
230 print join (" \\\n", wrap ($len, 'run_gen =', @run)), "\n";
237 # This script was generated automatically by $ME.
242 test "\$VERBOSE" && echo=echo || echo=:
243 \$echo testing program: \$xx
245 test "\$srcdir" || srcdir=.
246 test "\$VERBOSE" && \$xx --version 2> /dev/null
248 # Make sure we get English translations.
262 foreach $test_vector (Test::test_vector ())
264 my ($test_name, $flags, $in_spec, $exp_spec, $e_ret_code)
267 my $in = spec_to_list ($in_spec, $test_name, $In);
269 my @srcdir_rel_in_file;
271 foreach $f (@{$in->{EXPLICIT}}, @{$in->{MAINT_GEN}})
273 push (@srcdir_rel_in_file, "\$srcdir/$f");
276 my $exp = spec_to_list ($exp_spec, $test_name, $Exp);
277 my @all = (@{$exp->{EXPLICIT}}, @{$exp->{MAINT_GEN}});
279 my $exp_name = "\$srcdir/$all[0]";
280 my $out = "$test_name$Out";
281 my $err_output = "$test_name$Err";
283 my %valid_via = map {$_ => 1} qw (REDIR FILE PIPE);
284 my %via_msg_string = (REDIR => '<', FILE => 'F', PIPE => '|');
286 # Inhibit warnings about `used only once'.
287 die if 0 && $Test::input_via{$test_name} && $Test::input_via_default;
288 die if 0 && $Test::env{$test_name} && $Test::env_default;
290 my $vias = $Test::input_via{$test_name} || $Test::input_via_default
293 my $n_vias = keys %$vias;
295 foreach $via (sort keys %$vias)
298 my $val = $vias->{$via};
299 my $via_msg = ($n_vias == 1 ? '' : $via_msg_string{$via});
300 my $file_args = join (' ', @srcdir_rel_in_file);
302 my $env = $Test::env{$test_name} || $Test::env_default || [''];
304 or die "$ME: unexpected environment: @$env\n";
306 my $env_prefix = ($env ? "$env " : '');
310 $cmd = "$env_prefix\$xx $flags $file_args > $out 2> $err_output";
312 elsif ($via eq 'PIPE')
314 $via_msg = "|$val" if $val;
316 $cmd = "$val $file_args | $env_prefix\$xx $flags"
317 . " > $out 2> $err_output";
321 assert (@srcdir_rel_in_file == 1);
322 $cmd = "$env_prefix\$xx $flags"
323 . " < $file_args > $out 2> $err_output";
327 my $sep = ($via_msg && $e ? ':' : '');
328 my $msg = "$e$sep$via_msg";
329 $msg = "($msg)" if $msg;
330 my $t_name = "$test_name$msg";
335 if test \$code != $e_ret_code; then
336 \$echo "Test $t_name failed: \$xx return code \$code differs from expected value $e_ret_code" 1>&2
337 errors=`expr \$errors + 1`
339 cmp $out $exp_name > /dev/null 2>&1
341 0) if test "\$VERBOSE"; then \$echo "passed $t_name"; fi;;
342 1) \$echo "Test $t_name failed: files $out and $exp_name differ" 1>&2
343 (diff -c $out $exp_name) 2> /dev/null
344 errors=`expr \$errors + 1`;;
345 2) \$echo "Test $t_name may have failed." 1>&2
346 \$echo The command \"cmp $out $exp_name\" failed. 1>&2
347 errors=`expr \$errors + 1`;;
350 test -s $err_output || rm -f $err_output
355 if test \$errors = 0; then
356 \$echo Passed all $n_tests tests. 1>&2
358 \$echo Failed \$errors tests. 1>&2
360 test \$errors = 0 || errors=1