+ _fresh_perl($prog, '=~', $expected, $runperl_args, $name);
+}
+
+# Many tests use the same format in __DATA__ or external files to specify a
+# sequence of (fresh) tests to run, extra files they may temporarily need, and
+# what the expected output is. So have excatly one copy of the code to run that
+#
+# Each program is source code to run followed by an "EXPECT" line, followed
+# by the expected output.
+#
+# The code to run may begin with a command line switch such as -w or -0777
+# (alphanumerics only), and may contain (note the '# ' on each):
+# # TODO reason for todo
+# # SKIP reason for skip
+# # SKIP ?code to test if this should be skipped
+# # NAME name of the test (as with ok($ok, $name))
+#
+# The expected output may contain:
+# OPTION list of options
+# OPTIONS list of options
+#
+# The possible options for OPTION may be:
+# regex - the expected output is a regular expression
+# random - all lines match but in any order
+# fatal - the code will fail fatally (croak, die)
+#
+# If the actual output contains a line "SKIPPED" the test will be
+# skipped.
+#
+# If the actual output contains a line "PREFIX", any output starting with that
+# line will be ignored when comparing with the expected output
+#
+# If the global variable $FATAL is true then OPTION fatal is the
+# default.
+
+sub _setup_one_file {
+ my $fh = shift;
+ # Store the filename as a program that started at line 0.
+ # Real files count lines starting at line 1.
+ my @these = (0, shift);
+ my ($lineno, $current);
+ while (<$fh>) {
+ if ($_ eq "########\n") {
+ if (defined $current) {
+ push @these, $lineno, $current;
+ }
+ undef $current;
+ } else {
+ if (!defined $current) {
+ $lineno = $.;
+ }
+ $current .= $_;
+ }
+ }
+ if (defined $current) {
+ push @these, $lineno, $current;
+ }
+ ((scalar @these) / 2 - 1, @these);
+}
+
+sub setup_multiple_progs {
+ my ($tests, @prgs);
+ foreach my $file (@_) {
+ next if $file =~ /(?:~|\.orig|,v)$/;
+ next if $file =~ /perlio$/ && !PerlIO::Layer->find('perlio');
+ next if -d $file;
+
+ open my $fh, '<', $file or die "Cannot open $file: $!\n" ;
+ my $found;
+ while (<$fh>) {
+ if (/^__END__/) {
+ ++$found;
+ last;
+ }
+ }
+ # This is an internal error, and should never happen. All bar one of
+ # the files had an __END__ marker to signal the end of their preamble,
+ # although for some it wasn't technically necessary as they have no
+ # tests. It might be possible to process files without an __END__ by
+ # seeking back to the start and treating the whole file as tests, but
+ # it's simpler and more reliable just to make the rule that all files
+ # must have __END__ in. This should never fail - a file without an
+ # __END__ should not have been checked in, because the regression tests
+ # would not have passed.
+ die "Could not find '__END__' in $file"
+ unless $found;
+
+ my ($t, @p) = _setup_one_file($fh, $file);
+ $tests += $t;
+ push @prgs, @p;
+
+ close $fh
+ or die "Cannot close $file: $!\n";
+ }
+ return ($tests, @prgs);
+}
+
+sub run_multiple_progs {
+ my $up = shift;
+ my @prgs;
+ if ($up) {
+ # The tests in lib run in a temporary subdirectory of t, and always
+ # pass in a list of "programs" to run
+ @prgs = @_;
+ } else {
+ # The tests below t run in t and pass in a file handle. In theory we
+ # can pass (caller)[1] as the second argument to report errors with
+ # the filename of our caller, as the handle is always DATA. However,
+ # line numbers in DATA count from the __END__ token, so will be wrong.
+ # Which is more confusing than not providing line numbers. So, for now,
+ # don't provide line numbers. No obvious clean solution - one hack
+ # would be to seek DATA back to the start and read to the __END__ token,
+ # but that feels almost like we should just open $0 instead.
+
+ # Not going to rely on undef in list assignment.
+ my $dummy;
+ ($dummy, @prgs) = _setup_one_file(shift);
+ }
+
+ my $tmpfile = tempfile();
+
+ my ($file, $line);
+ PROGRAM:
+ while (defined ($line = shift @prgs)) {
+ $_ = shift @prgs;
+ unless ($line) {
+ $file = $_;
+ if (defined $file) {
+ print "# From $file\n";
+ }
+ next;
+ }
+ my $switch = "";
+ my @temps ;
+ my @temp_path;
+ if (s/^(\s*-\w+)//) {
+ $switch = $1;
+ }
+ my ($prog, $expected) = split(/\nEXPECT(?:\n|$)/, $_, 2);
+
+ my %reason;
+ foreach my $what (qw(skip todo)) {
+ $prog =~ s/^#\s*\U$what\E\s*(.*)\n//m and $reason{$what} = $1;
+ # If the SKIP reason starts ? then it's taken as a code snippet to
+ # evaluate. This provides the flexibility to have conditional SKIPs
+ if ($reason{$what} && $reason{$what} =~ s/^\?//) {
+ my $temp = eval $reason{$what};
+ if ($@) {
+ die "# In \U$what\E code reason:\n# $reason{$what}\n$@";
+ }
+ $reason{$what} = $temp;
+ }
+ }
+
+ my $name = '';
+ if ($prog =~ s/^#\s*NAME\s+(.+)\n//m) {
+ $name = $1;
+ }
+
+ if ($reason{skip}) {
+ SKIP:
+ {
+ skip($name ? "$name - $reason{skip}" : $reason{skip}, 1);
+ }
+ next PROGRAM;
+ }
+
+ if ($prog =~ /--FILE--/) {
+ my @files = split(/\n?--FILE--\s*([^\s\n]*)\s*\n/, $prog) ;
+ shift @files ;
+ die "Internal error: test $_ didn't split into pairs, got " .
+ scalar(@files) . "[" . join("%%%%", @files) ."]\n"
+ if @files % 2;
+ while (@files > 2) {
+ my $filename = shift @files;
+ my $code = shift @files;
+ push @temps, $filename;
+ if ($filename =~ m#(.*)/# && $filename !~ m#^\.\./#) {
+ require File::Path;
+ File::Path::mkpath($1);
+ push(@temp_path, $1);
+ }
+ open my $fh, '>', $filename or die "Cannot open $filename: $!\n";
+ print $fh $code;
+ close $fh or die "Cannot close $filename: $!\n";
+ }
+ shift @files;
+ $prog = shift @files;
+ }
+
+ open my $fh, '>', $tmpfile or die "Cannot open >$tmpfile: $!";
+ print $fh q{
+ BEGIN {
+ open STDERR, '>&', STDOUT
+ or die "Can't dup STDOUT->STDERR: $!;";
+ }
+ };
+ print $fh "\n#line 1\n"; # So the line numbers don't get messed up.
+ print $fh $prog,"\n";
+ close $fh or die "Cannot close $tmpfile: $!";
+ my $results = runperl( stderr => 1, progfile => $tmpfile,
+ stdin => '', $up
+ ? (switches => ["-I$up/lib", $switch], nolib => 1)
+ : (switches => [$switch])
+ );
+ my $status = $?;
+ $results =~ s/\n+$//;
+ # allow expected output to be written as if $prog is on STDIN
+ $results =~ s/$::tempfile_regexp/-/g;
+ if ($^O eq 'VMS') {
+ # some tests will trigger VMS messages that won't be expected
+ $results =~ s/\n?%[A-Z]+-[SIWEF]-[A-Z]+,.*//;
+
+ # pipes double these sometimes
+ $results =~ s/\n\n/\n/g;
+ }
+ # bison says 'parse error' instead of 'syntax error',
+ # various yaccs may or may not capitalize 'syntax'.
+ $results =~ s/^(syntax|parse) error/syntax error/mig;
+ # allow all tests to run when there are leaks
+ $results =~ s/Scalars leaked: \d+\n//g;
+
+ $expected =~ s/\n+$//;
+ my $prefix = ($results =~ s#^PREFIX(\n|$)##) ;
+ # any special options? (OPTIONS foo bar zap)
+ my $option_regex = 0;
+ my $option_random = 0;
+ my $fatal = $FATAL;
+ if ($expected =~ s/^OPTIONS? (.+)\n//) {
+ foreach my $option (split(' ', $1)) {
+ if ($option eq 'regex') { # allow regular expressions
+ $option_regex = 1;
+ }
+ elsif ($option eq 'random') { # all lines match, but in any order
+ $option_random = 1;
+ }
+ elsif ($option eq 'fatal') { # perl should fail
+ $fatal = 1;
+ }
+ else {
+ die "$0: Unknown OPTION '$option'\n";
+ }
+ }
+ }
+ die "$0: can't have OPTION regex and random\n"
+ if $option_regex + $option_random > 1;
+ my $ok = 0;
+ if ($results =~ s/^SKIPPED\n//) {
+ print "$results\n" ;
+ $ok = 1;
+ }
+ else {
+ if ($option_random) {
+ my @got = sort split "\n", $results;
+ my @expected = sort split "\n", $expected;
+
+ $ok = "@got" eq "@expected";
+ }
+ elsif ($option_regex) {
+ $ok = $results =~ /^$expected/;
+ }
+ elsif ($prefix) {
+ $ok = $results =~ /^\Q$expected/;
+ }
+ else {
+ $ok = $results eq $expected;
+ }
+
+ if ($ok && $fatal && !($status >> 8)) {
+ $ok = 0;
+ }
+ }
+
+ local $::TODO = $reason{todo};
+
+ unless ($ok) {
+ my $err_line = "PROG: $switch\n$prog\n" .
+ "EXPECTED:\n$expected\n";
+ $err_line .= "EXIT STATUS: != 0\n" if $fatal;
+ $err_line .= "GOT:\n$results\n";
+ $err_line .= "EXIT STATUS: " . ($status >> 8) . "\n" if $fatal;
+ if ($::TODO) {
+ $err_line =~ s/^/# /mg;
+ print $err_line; # Harness can't filter it out from STDERR.
+ }
+ else {
+ print STDERR $err_line;
+ }
+ }
+
+ if (defined $file) {
+ _ok($ok, "at $file line $line", $name);
+ } else {
+ # We don't have file and line number data for the test, so report
+ # errors as coming from our caller.
+ local $Level = $Level + 1;
+ ok($ok, $name);
+ }
+
+ foreach (@temps) {
+ unlink $_ if $_;
+ }
+ foreach (@temp_path) {
+ File::Path::rmtree $_ if -d $_;
+ }
+ }