This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Introduce the :5.10.0 feature bundle, and make :5.10 an
[perl5.git] / t / lib / common.pl
... / ...
CommitLineData
1# This code is used by lib/warnings.t and lib/feature.t
2
3BEGIN {
4 require Config; import Config;
5 require './test.pl';
6}
7
8use File::Path;
9use File::Spec::Functions;
10
11use strict;
12our $pragma_name;
13
14$| = 1;
15
16my $Is_MacOS = $^O eq 'MacOS';
17my $tmpfile = "tmp0000";
181 while -e ++$tmpfile;
19END { if ($tmpfile) { 1 while unlink $tmpfile} }
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
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);
74
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 }
163 elsif ($option_random)
164 {
165 $ok = randomMatch($results, $expected);
166 }
167 elsif (($prefix && (( $option_regex && $results !~ /^$expected/) ||
168 (!$option_regex && $results !~ /^\Q$expected/))) or
169 (!$prefix && (( $option_regex && $results !~ /^$expected/) ||
170 (!$option_regex && $results ne $expected)))) {
171 my $err_line = "PROG: $switch\n$prog\n" .
172 "EXPECTED:\n$expected\n" .
173 "GOT:\n$results\n";
174 if ($todo) {
175 $err_line =~ s/^/# /mg;
176 print $err_line; # Harness can't filter it out from STDERR.
177 }
178 else {
179 print STDERR $err_line;
180 }
181 $ok = 0;
182 }
183
184 our $TODO = $todo ? $todo_reason : 0;
185 ok($ok);
186
187 foreach (@temps)
188 { unlink $_ if $_ }
189 foreach (@temp_path)
190 { rmtree $_ if -d $_ }
191}
192
193sub randomMatch
194{
195 my $got = shift ;
196 my $expected = shift;
197
198 my @got = sort split "\n", $got ;
199 my @expected = sort split "\n", $expected ;
200
201 return "@got" eq "@expected";
202
203}
204
2051;