Enhance t/lib/common.pl to provide conditional TODOs using eval'd code.
[p5sagit/p5-mst-13.2.git] / t / lib / common.pl
1 # This code is used by lib/warnings.t and lib/feature.t
2
3 BEGIN {
4     require './test.pl';
5 }
6
7 use Config;
8 use File::Path;
9 use File::Spec::Functions;
10
11 use strict;
12 our $pragma_name;
13
14 $| = 1;
15
16 my $Is_MacOS = $^O eq 'MacOS';
17 my $tmpfile = "tmp0000";
18 1 while -e ++$tmpfile;
19 END { 1 while unlink $tmpfile }
20
21 my @prgs = () ;
22 my @w_files = () ;
23
24 if (@ARGV)
25   { print "ARGV = [@ARGV]\n" ;
26     if ($Is_MacOS) {
27       @w_files = map { s#^#:lib:$pragma_name:#; $_ } @ARGV
28     } else {
29       @w_files = map { s#^#./lib/$pragma_name/#; $_ } @ARGV
30     }
31   }
32 else
33   { @w_files = sort glob(catfile(curdir(), "lib", $pragma_name, "*")) }
34
35 my $files = 0;
36 foreach 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
57 undef $/;
58
59 plan tests => (scalar(@prgs)-$files);
60
61 for (@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\s*(.*)\n//m and $todo_reason = $1;
77     # If the TODO reason starts ? then it's taken as a code snippet to evaluate
78     # This provides the flexibility to have conditional TODOs
79     if ($todo_reason =~ s/^\?//) {
80         my $temp = eval $todo_reason;
81         if ($@) {
82             die "# In TODO code reason:\n# $todo_reason\n$@";
83         }
84         $todo_reason = $temp;
85     }
86     if ( $prog =~ /--FILE--/) {
87         my(@files) = split(/\n--FILE--\s*([^\s\n]*)\s*\n/, $prog) ;
88         shift @files ;
89         die "Internal error: test $_ didn't split into pairs, got " .
90                 scalar(@files) . "[" . join("%%%%", @files) ."]\n"
91             if @files % 2 ;
92         while (@files > 2) {
93             my $filename = shift @files ;
94             my $code = shift @files ;
95             push @temps, $filename ;
96             if ($filename =~ m#(.*)/#) {
97                 mkpath($1);
98                 push(@temp_path, $1);
99             }
100             open F, ">$filename" or die "Cannot open $filename: $!\n" ;
101             print F $code ;
102             close F or die "Cannot close $filename: $!\n";
103         }
104         shift @files ;
105         $prog = shift @files ;
106     }
107
108     # fix up some paths
109     if ($Is_MacOS) {
110         $prog =~ s|require "./abc(d)?";|require ":abc$1";|g;
111         $prog =~ s|"\."|":"|g;
112     }
113
114     open TEST, ">$tmpfile" or die "Cannot open >$tmpfile: $!";
115     print TEST q{
116         BEGIN {
117             open(STDERR, ">&STDOUT")
118               or die "Can't dup STDOUT->STDERR: $!;";
119         }
120     };
121     print TEST "\n#line 1\n";  # So the line numbers don't get messed up.
122     print TEST $prog,"\n";
123     close TEST or die "Cannot close $tmpfile: $!";
124     my $results = runperl( switches => [$switch], stderr => 1, progfile => $tmpfile );
125     my $status = $?;
126     $results =~ s/\n+$//;
127     # allow expected output to be written as if $prog is on STDIN
128     $results =~ s/tmp\d+/-/g;
129     if ($^O eq 'VMS') {
130         # some tests will trigger VMS messages that won't be expected
131         $results =~ s/\n?%[A-Z]+-[SIWEF]-[A-Z]+,.*//;
132
133         # pipes double these sometimes
134         $results =~ s/\n\n/\n/g;
135     }
136 # bison says 'parse error' instead of 'syntax error',
137 # various yaccs may or may not capitalize 'syntax'.
138     $results =~ s/^(syntax|parse) error/syntax error/mig;
139     # allow all tests to run when there are leaks
140     $results =~ s/Scalars leaked: \d+\n//g;
141
142     # fix up some paths
143     if ($Is_MacOS) {
144         $results =~ s|:abc\.pm\b|abc.pm|g;
145         $results =~ s|:abc(d)?\b|./abc$1|g;
146     }
147
148     $expected =~ s/\n+$//;
149     my $prefix = ($results =~ s#^PREFIX(\n|$)##) ;
150     # any special options? (OPTIONS foo bar zap)
151     my $option_regex = 0;
152     my $option_random = 0;
153     if ($expected =~ s/^OPTIONS? (.+)\n//) {
154         foreach my $option (split(' ', $1)) {
155             if ($option eq 'regex') { # allow regular expressions
156                 $option_regex = 1;
157             }
158             elsif ($option eq 'random') { # all lines match, but in any order
159                 $option_random = 1;
160             }
161             else {
162                 die "$0: Unknown OPTION '$option'\n";
163             }
164         }
165     }
166     die "$0: can't have OPTION regex and random\n"
167         if $option_regex + $option_random > 1;
168     my $ok = 0;
169     if ($results =~ s/^SKIPPED\n//) {
170         print "$results\n" ;
171         $ok = 1;
172     }
173     elsif ($option_random) {
174         $ok = randomMatch($results, $expected);
175     }
176     elsif ($option_regex) {
177         $ok = $results =~ /^$expected/;
178     }
179     elsif ($prefix) {
180         $ok = $results =~ /^\Q$expected/;
181     }
182     else {
183         $ok = $results eq $expected;
184     }
185  
186     print_err_line( $switch, $prog, $expected, $results, $todo ) unless $ok;
187
188     our $TODO = $todo ? $todo_reason : 0;
189     ok($ok);
190
191     foreach (@temps)
192         { unlink $_ if $_ }
193     foreach (@temp_path)
194         { rmtree $_ if -d $_ }
195 }
196
197 sub randomMatch
198 {
199     my $got = shift ;
200     my $expected = shift;
201
202     my @got = sort split "\n", $got ;
203     my @expected = sort split "\n", $expected ;
204
205    return "@got" eq "@expected";
206
207 }
208
209 sub print_err_line {
210     my($switch, $prog, $expected, $results, $todo) = @_;
211     my $err_line = "PROG: $switch\n$prog\n" .
212                    "EXPECTED:\n$expected\n" .
213                    "GOT:\n$results\n";
214     if ($todo) {
215         $err_line =~ s/^/# /mg;
216         print $err_line;  # Harness can't filter it out from STDERR.
217     }
218     else {
219         print STDERR $err_line;
220     }
221
222     return 1;
223 }
224
225 1;