1 # This code is used by lib/charnames.t, lib/feature.t, lib/subs.t,
2 # lib/strict.t and lib/warnings.t
4 # On input, $::local_tests is the number of tests in the caller; or
5 # 'no_plan' if unknown, in which case it is the caller's responsibility
6 # to call cur_test() to find out how many this executed
14 use File::Spec::Functions qw(catfile curdir rel2abs);
18 my (undef, $file) = caller;
19 my ($pragma_name) = $file =~ /([A-Za-z_0-9]+)\.t$/
20 or die "Can't identify pragama to test from file name '$file'";
24 my $tmpfile = tempfile();
30 { print "ARGV = [@ARGV]\n" ;
31 @w_files = map { s#^#./lib/$pragma_name/#; $_ } @ARGV
34 { @w_files = sort glob(catfile(curdir(), "lib", $pragma_name, "*")) }
37 foreach my $file (@w_files) {
39 next if $file =~ /(~|\.orig|,v)$/;
40 next if $file =~ /perlio$/ && !(find PerlIO::Layer 'perlio');
43 open F, "<$file" or die "Cannot open $file: $!\n" ;
53 @prgs = (@prgs, $file, split "\n########\n", <F>) ;
59 my $tempdir = tempfile;
61 mkdir $tempdir, 0700 or die "Can't mkdir '$tempdir': $!";
62 chdir $tempdir or die die "Can't chdir '$tempdir': $!";
63 unshift @INC, '../../lib';
69 chdir '..' or die "Couldn't chdir .. for cleanup: $!";
76 my $tests = $::local_tests || 0;
77 $tests = scalar(@prgs)-$files + $tests if $tests !~ /\D/;
78 plan $tests; # If input is 'no_plan', pass it on unchanged
92 my($prog,$expected) = split(/\nEXPECT(?:\n|$)/, $_, 2);
95 foreach my $what (qw(skip todo)) {
96 $prog =~ s/^#\s*\U$what\E\s*(.*)\n//m and $reason{$what} = $1;
97 # If the SKIP reason starts ? then it's taken as a code snippet to
98 # evaluate. This provides the flexibility to have conditional SKIPs
99 if ($reason{$what} && $reason{$what} =~ s/^\?//) {
100 my $temp = eval $reason{$what};
102 die "# In \U$what\E code reason:\n# $reason{$what}\n$@";
104 $reason{$what} = $temp;
108 if ( $prog =~ /--FILE--/) {
109 my(@files) = split(/\n--FILE--\s*([^\s\n]*)\s*\n/, $prog) ;
111 die "Internal error: test $_ didn't split into pairs, got " .
112 scalar(@files) . "[" . join("%%%%", @files) ."]\n"
115 my $filename = shift @files ;
116 my $code = shift @files ;
117 push @temps, $filename ;
118 if ($filename =~ m#(.*)/# && $filename !~ m#^\.\./#) {
120 push(@temp_path, $1);
122 open F, ">$filename" or die "Cannot open $filename: $!\n" ;
124 close F or die "Cannot close $filename: $!\n";
127 $prog = shift @files ;
130 open TEST, ">$tmpfile" or die "Cannot open >$tmpfile: $!";
133 open(STDERR, ">&STDOUT")
134 or die "Can't dup STDOUT->STDERR: $!;";
137 print TEST "\n#line 1\n"; # So the line numbers don't get messed up.
138 print TEST $prog,"\n";
139 close TEST or die "Cannot close $tmpfile: $!";
140 my $results = runperl( switches => ["-I../../lib", $switch], nolib => 1,
141 stderr => 1, progfile => $tmpfile );
143 $results =~ s/\n+$//;
144 # allow expected output to be written as if $prog is on STDIN
145 $results =~ s/$::tempfile_regexp/-/g;
147 # some tests will trigger VMS messages that won't be expected
148 $results =~ s/\n?%[A-Z]+-[SIWEF]-[A-Z]+,.*//;
150 # pipes double these sometimes
151 $results =~ s/\n\n/\n/g;
153 # bison says 'parse error' instead of 'syntax error',
154 # various yaccs may or may not capitalize 'syntax'.
155 $results =~ s/^(syntax|parse) error/syntax error/mig;
156 # allow all tests to run when there are leaks
157 $results =~ s/Scalars leaked: \d+\n//g;
159 $expected =~ s/\n+$//;
160 my $prefix = ($results =~ s#^PREFIX(\n|$)##) ;
161 # any special options? (OPTIONS foo bar zap)
162 my $option_regex = 0;
163 my $option_random = 0;
164 if ($expected =~ s/^OPTIONS? (.+)\n//) {
165 foreach my $option (split(' ', $1)) {
166 if ($option eq 'regex') { # allow regular expressions
169 elsif ($option eq 'random') { # all lines match, but in any order
173 die "$0: Unknown OPTION '$option'\n";
177 die "$0: can't have OPTION regex and random\n"
178 if $option_regex + $option_random > 1;
180 if ($results =~ s/^SKIPPED\n//) {
184 elsif ($option_random) {
185 $ok = randomMatch($results, $expected);
187 elsif ($option_regex) {
188 $ok = $results =~ /^$expected/;
191 $ok = $results =~ /^\Q$expected/;
194 $ok = $results eq $expected;
197 local $::TODO = $reason{todo};
198 print_err_line( $switch, $prog, $expected, $results, $::TODO ) unless $ok;
205 { rmtree $_ if -d $_ }
211 my $expected = shift;
213 my @got = sort split "\n", $got ;
214 my @expected = sort split "\n", $expected ;
216 return "@got" eq "@expected";
221 my($switch, $prog, $expected, $results, $todo) = @_;
222 my $err_line = "PROG: $switch\n$prog\n" .
223 "EXPECTED:\n$expected\n" .
226 $err_line =~ s/^/# /mg;
227 print $err_line; # Harness can't filter it out from STDERR.
230 print STDERR $err_line;