warnings for perlio + others
[p5sagit/p5-mst-13.2.git] / lib / warnings.t
1 #!./perl
2
3 BEGIN {
4     chdir 't' if -d 't';
5     @INC = '../lib';
6     $ENV{PERL5LIB} = '../lib';
7     require Config; import Config;
8 }
9
10 use File::Path;
11
12 $| = 1;
13
14 my $Is_VMS     = $^O eq 'VMS';
15 my $Is_MSWin32 = $^O eq 'MSWin32';
16 my $Is_NetWare = $^O eq 'NetWare';
17 my $tmpfile = "tmp0000";
18 my $i = 0 ;
19 1 while -f ++$tmpfile;
20 END {  if ($tmpfile) { 1 while unlink $tmpfile} }
21
22 my @prgs = () ;
23 my @w_files = () ;
24
25 if (@ARGV)
26   { print "ARGV = [@ARGV]\n" ; @w_files = map { s#^#./lib/warnings/#; $_ } @ARGV }
27 else
28   { @w_files = sort glob("lib/warnings/*") }
29
30 my $files = 0;
31 foreach my $file (@w_files) {
32
33     next if $file =~ /(~|\.orig|,v)$/;
34
35     open F, "<$file" or die "Cannot open $file: $!\n" ;
36     my $line = 0;
37     while (<F>) {
38         $line++;
39         last if /^__END__/ ;
40     }
41
42     {
43         local $/ = undef;
44         $files++;
45         @prgs = (@prgs, $file, split "\n########\n", <F>) ;
46     }
47     close F ;
48 }
49
50 undef $/;
51
52 print "1..", scalar(@prgs)-$files, "\n";
53
54
55 for (@prgs){
56     unless (/\n/)
57      {
58       print "# From $_\n";
59       next;
60      }
61     my $switch = "";
62     my @temps = () ;
63     my @temp_path = () ;
64     if (s/^\s*-\w+//){
65         $switch = $&;
66         $switch =~ s/(-\S*[A-Z]\S*)/"$1"/ if $Is_VMS; # protect uc switches
67     }
68     my($prog,$expected) = split(/\nEXPECT\n/, $_);
69     if ( $prog =~ /--FILE--/) {
70         my(@files) = split(/\n--FILE--\s*([^\s\n]*)\s*\n/, $prog) ;
71         shift @files ;
72         die "Internal error test $i didn't split into pairs, got " .
73                 scalar(@files) . "[" . join("%%%%", @files) ."]\n"
74             if @files % 2 ;
75         while (@files > 2) {
76             my $filename = shift @files ;
77             my $code = shift @files ;
78             push @temps, $filename ;
79             if ($filename =~ m#(.*)/#) {
80                 mkpath($1);
81                 push(@temp_path, $1);
82             }
83             open F, ">$filename" or die "Cannot open $filename: $!\n" ;
84             print F $code ;
85             close F or die "Cannot close $filename: $!\n";
86         }
87         shift @files ;
88         $prog = shift @files ;
89     }
90     open TEST, ">$tmpfile" or die "Cannot open >$tmpfile: $!";
91     print TEST q{
92         BEGIN {
93             open(STDERR, ">&STDOUT")
94               or die "Can't dup STDOUT->STDERR: $!;";
95         }
96     };
97     print TEST "\n#line 1\n";  # So the line numbers don't get messed up.
98     print TEST $prog,"\n";
99     close TEST or die "Cannot close $tmpfile: $!";
100     my $results = $Is_VMS ?
101                       `./perl "-I../lib" $switch $tmpfile` :
102                   $Is_MSWin32 ?
103                       `.\\perl -I../lib $switch $tmpfile` :
104                   $Is_NetWare ?
105                       `perl -I../lib $switch $tmpfile` :
106                   $Is_MacOS ?
107                       `$^X -I::lib $switch -MMac::err=unix $tmpfile` :
108                   `./perl -I../lib $switch $tmpfile`;
109     my $status = $?;
110     $results =~ s/\n+$//;
111     # allow expected output to be written as if $prog is on STDIN
112     $results =~ s/tmp\d+/-/g;
113     if ($^O eq 'VMS') {
114         # some tests will trigger VMS messages that won't be expected
115         $results =~ s/\n?%[A-Z]+-[SIWEF]-[A-Z]+,.*//;
116
117         # pipes double these sometimes
118         $results =~ s/\n\n/\n/g;
119     }
120 # bison says 'parse error' instead of 'syntax error',
121 # various yaccs may or may not capitalize 'syntax'.
122     $results =~ s/^(syntax|parse) error/syntax error/mig;
123     # allow all tests to run when there are leaks
124     $results =~ s/Scalars leaked: \d+\n//g;
125     $expected =~ s/\n+$//;
126     my $prefix = ($results =~ s#^PREFIX(\n|$)##) ;
127     # any special options? (OPTIONS foo bar zap)
128     my $option_regex = 0;
129     my $option_random = 0;
130     if ($expected =~ s/^OPTIONS? (.+)\n//) {
131         foreach my $option (split(' ', $1)) {
132             if ($option eq 'regex') { # allow regular expressions
133                 $option_regex = 1;
134             } 
135             elsif ($option eq 'random') { # all lines match, but in any order
136                 $option_random = 1;
137             }
138             else {
139                 die "$0: Unknown OPTION '$option'\n";
140             }
141         }
142     }
143     die "$0: can't have OPTION regex and random\n"
144         if $option_regex + option_random > 1;
145     if ( $results =~ s/^SKIPPED\n//) {
146         print "$results\n" ;
147     }
148     elsif ($option_random)
149     {
150         print "not " if !randomMatch($results, $expected);
151     }
152     elsif (($prefix  && (( $option_regex && $results !~ /^$expected/) ||
153                          (!$option_regex && $results !~ /^\Q$expected/))) or
154            (!$prefix && (( $option_regex && $results !~ /^$expected/) ||
155                          (!$option_regex && $results ne $expected)))) {
156         print STDERR "PROG: $switch\n$prog\n";
157         print STDERR "EXPECTED:\n$expected\n";
158         print STDERR "GOT:\n$results\n";
159         print "not ";
160     }
161     print "ok ", ++$i, "\n";
162     foreach (@temps)
163         { unlink $_ if $_ }
164     foreach (@temp_path)
165         { rmtree $_ if -d $_ }
166 }
167
168 sub randomMatch
169 {
170     my $got = shift ;
171     my $expected = shift;
172
173     my @got = sort split "\n", $got ;
174     my @expected = sort split "\n", $expected ;
175
176    return "@got" eq "@expected";
177
178 }