This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Convert subs.t to use t/lib/common.pl
[perl5.git] / t / lib / common.pl
CommitLineData
6fc551a0
NC
1# This code is used by lib/charnames.t, lib/feature.t, lib/subs.t,
2# lib/strict.t and lib/warnings.t
25ae1130
RGS
3
4BEGIN {
25ae1130
RGS
5 require './test.pl';
6}
7
afb1190b 8use Config;
25ae1130
RGS
9use File::Path;
10use File::Spec::Functions;
11
12use strict;
742dc32d 13use warnings;
25ae1130
RGS
14our $pragma_name;
15
16$| = 1;
17
f9cd5d9f 18my $tmpfile = tempfile();
25ae1130
RGS
19
20my @prgs = () ;
21my @w_files = () ;
22
23if (@ARGV)
24 { print "ARGV = [@ARGV]\n" ;
25ae1130 25 @w_files = map { s#^#./lib/$pragma_name/#; $_ } @ARGV
25ae1130
RGS
26 }
27else
28 { @w_files = sort glob(catfile(curdir(), "lib", $pragma_name, "*")) }
29
30my $files = 0;
31foreach my $file (@w_files) {
32
33 next if $file =~ /(~|\.orig|,v)$/;
34 next if $file =~ /perlio$/ && !(find PerlIO::Layer 'perlio');
35 next if -d $file;
36
37 open F, "<$file" or die "Cannot open $file: $!\n" ;
38 my $line = 0;
39 while (<F>) {
40 $line++;
41 last if /^__END__/ ;
42 }
43
44 {
45 local $/ = undef;
46 $files++;
47 @prgs = (@prgs, $file, split "\n########\n", <F>) ;
48 }
49 close F ;
50}
51
52undef $/;
53
619d9e9c 54plan tests => (scalar(@prgs)-$files + ($::local_tests || 0));
25ae1130 55
25ae1130
RGS
56for (@prgs){
57 unless (/\n/)
58 {
59 print "# From $_\n";
60 next;
61 }
62 my $switch = "";
63 my @temps = () ;
64 my @temp_path = () ;
65 if (s/^\s*-\w+//){
66 $switch = $&;
67 }
68 my($prog,$expected) = split(/\nEXPECT(?:\n|$)/, $_, 2);
a983b08d 69
6ee4ed10
NC
70 my %reason;
71 foreach my $what (qw(skip todo)) {
72 $prog =~ s/^#\s*\U$what\E\s*(.*)\n//m and $reason{$what} = $1;
73 # If the SKIP reason starts ? then it's taken as a code snippet to
74 # evaluate. This provides the flexibility to have conditional SKIPs
75 if ($reason{$what} && $reason{$what} =~ s/^\?//) {
76 my $temp = eval $reason{$what};
77 if ($@) {
78 die "# In \U$what\E code reason:\n# $reason{$what}\n$@";
79 }
80 $reason{$what} = $temp;
8e5f6690 81 }
8e5f6690 82 }
6ee4ed10 83
25ae1130
RGS
84 if ( $prog =~ /--FILE--/) {
85 my(@files) = split(/\n--FILE--\s*([^\s\n]*)\s*\n/, $prog) ;
86 shift @files ;
87 die "Internal error: test $_ didn't split into pairs, got " .
88 scalar(@files) . "[" . join("%%%%", @files) ."]\n"
89 if @files % 2 ;
90 while (@files > 2) {
91 my $filename = shift @files ;
92 my $code = shift @files ;
93 push @temps, $filename ;
ea065648 94 if ($filename =~ m#(.*)/# && $filename !~ m#^\.\./#) {
25ae1130
RGS
95 mkpath($1);
96 push(@temp_path, $1);
ea065648 97 }
25ae1130
RGS
98 open F, ">$filename" or die "Cannot open $filename: $!\n" ;
99 print F $code ;
100 close F or die "Cannot close $filename: $!\n";
101 }
102 shift @files ;
103 $prog = shift @files ;
104 }
105
25ae1130
RGS
106 open TEST, ">$tmpfile" or die "Cannot open >$tmpfile: $!";
107 print TEST q{
108 BEGIN {
109 open(STDERR, ">&STDOUT")
110 or die "Can't dup STDOUT->STDERR: $!;";
111 }
112 };
113 print TEST "\n#line 1\n"; # So the line numbers don't get messed up.
114 print TEST $prog,"\n";
115 close TEST or die "Cannot close $tmpfile: $!";
116 my $results = runperl( switches => [$switch], stderr => 1, progfile => $tmpfile );
117 my $status = $?;
118 $results =~ s/\n+$//;
119 # allow expected output to be written as if $prog is on STDIN
f9cd5d9f 120 $results =~ s/$::tempfile_regexp/-/g;
25ae1130
RGS
121 if ($^O eq 'VMS') {
122 # some tests will trigger VMS messages that won't be expected
123 $results =~ s/\n?%[A-Z]+-[SIWEF]-[A-Z]+,.*//;
124
125 # pipes double these sometimes
126 $results =~ s/\n\n/\n/g;
127 }
128# bison says 'parse error' instead of 'syntax error',
129# various yaccs may or may not capitalize 'syntax'.
130 $results =~ s/^(syntax|parse) error/syntax error/mig;
131 # allow all tests to run when there are leaks
132 $results =~ s/Scalars leaked: \d+\n//g;
133
25ae1130
RGS
134 $expected =~ s/\n+$//;
135 my $prefix = ($results =~ s#^PREFIX(\n|$)##) ;
136 # any special options? (OPTIONS foo bar zap)
137 my $option_regex = 0;
138 my $option_random = 0;
139 if ($expected =~ s/^OPTIONS? (.+)\n//) {
140 foreach my $option (split(' ', $1)) {
141 if ($option eq 'regex') { # allow regular expressions
142 $option_regex = 1;
143 }
144 elsif ($option eq 'random') { # all lines match, but in any order
145 $option_random = 1;
146 }
147 else {
148 die "$0: Unknown OPTION '$option'\n";
149 }
150 }
151 }
152 die "$0: can't have OPTION regex and random\n"
153 if $option_regex + $option_random > 1;
7702f076
RGS
154 my $ok = 0;
155 if ($results =~ s/^SKIPPED\n//) {
25ae1130 156 print "$results\n" ;
7702f076 157 $ok = 1;
25ae1130 158 }
afb1190b 159 elsif ($option_random) {
25ae1130
RGS
160 $ok = randomMatch($results, $expected);
161 }
7702f076
RGS
162 elsif ($option_regex) {
163 $ok = $results =~ /^$expected/;
164 }
165 elsif ($prefix) {
166 $ok = $results =~ /^\Q$expected/;
167 }
168 else {
169 $ok = $results eq $expected;
25ae1130 170 }
7702f076 171
4a4b8592
NC
172 local $::TODO = $reason{todo};
173 print_err_line( $switch, $prog, $expected, $results, $::TODO ) unless $ok;
25ae1130 174
25ae1130
RGS
175 ok($ok);
176
177 foreach (@temps)
178 { unlink $_ if $_ }
179 foreach (@temp_path)
180 { rmtree $_ if -d $_ }
181}
182
183sub randomMatch
184{
185 my $got = shift ;
186 my $expected = shift;
187
188 my @got = sort split "\n", $got ;
189 my @expected = sort split "\n", $expected ;
190
191 return "@got" eq "@expected";
192
193}
194
afb1190b
RGS
195sub print_err_line {
196 my($switch, $prog, $expected, $results, $todo) = @_;
197 my $err_line = "PROG: $switch\n$prog\n" .
198 "EXPECTED:\n$expected\n" .
199 "GOT:\n$results\n";
200 if ($todo) {
201 $err_line =~ s/^/# /mg;
202 print $err_line; # Harness can't filter it out from STDERR.
203 }
204 else {
205 print STDERR $err_line;
206 }
207
208 return 1;
209}
210
25ae1130 2111;