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