Wrap the macro arguments for ck_proto in ().
[p5sagit/p5-mst-13.2.git] / t / lib / common.pl
CommitLineData
25ae1130 1# This code is used by lib/warnings.t and lib/feature.t
2
3BEGIN {
25ae1130 4 require './test.pl';
5}
6
afb1190b 7use Config;
25ae1130 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 17my $tmpfile = "tmp0000";
181 while -e ++$tmpfile;
afb1190b 19END { 1 while unlink $tmpfile }
25ae1130 20
21my @prgs = () ;
22my @w_files = () ;
23
24if (@ARGV)
25 { print "ARGV = [@ARGV]\n" ;
f4ae0982 26 if ($Is_MacOS) {
25ae1130 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 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 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
f4ae0982 100 if ($Is_MacOS) {
25ae1130 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
f4ae0982 134 if ($Is_MacOS) {
25ae1130 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;
7702f076 159 my $ok = 0;
160 if ($results =~ s/^SKIPPED\n//) {
25ae1130 161 print "$results\n" ;
7702f076 162 $ok = 1;
25ae1130 163 }
afb1190b 164 elsif ($option_random) {
25ae1130 165 $ok = randomMatch($results, $expected);
166 }
7702f076 167 elsif ($option_regex) {
168 $ok = $results =~ /^$expected/;
169 }
170 elsif ($prefix) {
171 $ok = $results =~ /^\Q$expected/;
172 }
173 else {
174 $ok = $results eq $expected;
25ae1130 175 }
7702f076 176
177 print_err_line( $switch, $prog, $expected, $results, $todo ) unless $ok;
25ae1130 178
179 our $TODO = $todo ? $todo_reason : 0;
180 ok($ok);
181
182 foreach (@temps)
183 { unlink $_ if $_ }
184 foreach (@temp_path)
185 { rmtree $_ if -d $_ }
186}
187
188sub randomMatch
189{
190 my $got = shift ;
191 my $expected = shift;
192
193 my @got = sort split "\n", $got ;
194 my @expected = sort split "\n", $expected ;
195
196 return "@got" eq "@expected";
197
198}
199
afb1190b 200sub print_err_line {
201 my($switch, $prog, $expected, $results, $todo) = @_;
202 my $err_line = "PROG: $switch\n$prog\n" .
203 "EXPECTED:\n$expected\n" .
204 "GOT:\n$results\n";
205 if ($todo) {
206 $err_line =~ s/^/# /mg;
207 print $err_line; # Harness can't filter it out from STDERR.
208 }
209 else {
210 print STDERR $err_line;
211 }
212
213 return 1;
214}
215
25ae1130 2161;