Make t/op/regexp.t warnings clean.
[p5sagit/p5-mst-13.2.git] / t / op / regexp.t
1 #!./perl
2
3 # The tests are in a separate file 't/op/re_tests'.
4 # Each line in that file is a separate test.
5 # There are five columns, separated by tabs.
6 #
7 # Column 1 contains the pattern, optionally enclosed in C<''>.
8 # Modifiers can be put after the closing C<'>.
9 #
10 # Column 2 contains the string to be matched.
11 #
12 # Column 3 contains the expected result:
13 #       y       expect a match
14 #       n       expect no match
15 #       c       expect an error
16 #       B       test exposes a known bug in Perl, should be skipped
17 #       b       test exposes a known bug in Perl, should be skipped if noamp
18 #
19 # Columns 4 and 5 are used only if column 3 contains C<y> or C<c>.
20 #
21 # Column 4 contains a string, usually C<$&>.
22 #
23 # Column 5 contains the expected result of double-quote
24 # interpolating that string after the match, or start of error message.
25 #
26 # Column 6, if present, contains a reason why the test is skipped.
27 # This is printed with "skipped", for harness to pick up.
28 #
29 # \n in the tests are interpolated, as are variables of the form ${\w+}.
30 #
31 # Blanks lines are treated as PASSING tests to keep the line numbers
32 # linked to the test number.
33 #
34 # If you want to add a regular expression test that can't be expressed
35 # in this format, don't add it here: put it in op/pat.t instead.
36 #
37 # Note that columns 2,3 and 5 are all enclosed in double quotes and then
38 # evalled; so something like a\"\x{100}$1 has length 3+length($1).
39
40 BEGIN {
41     chdir 't' if -d 't';
42     @INC = '../lib';
43 }
44 use strict;
45 use warnings FATAL=>"all";
46 use vars qw($iters $numtests $bang $ffff $nulnul $OP);
47 use vars qw($qr $skip_amp $qr_embed); # set by our callers
48
49 $iters = shift || 1;            # Poor man performance suite, 10000 is OK.
50
51 open(TESTS,'op/re_tests') || open(TESTS,'t/op/re_tests') || open(TESTS,':op:re_tests') ||
52         die "Can't open re_tests";
53
54 while (<TESTS>) { }
55 $numtests = $.;
56 seek(TESTS,0,0);
57 $. = 0;
58
59 $bang = sprintf "\\%03o", ord "!"; # \41 would not be portable.
60 $ffff  = chr(0xff) x 2;
61 $nulnul = "\0" x 2;
62 $OP = $qr ? 'qr' : 'm';
63
64 $| = 1;
65 print "1..$numtests\n# $iters iterations\n";
66 TEST:
67 while (<TESTS>) {
68     if (!/\S/ || /^\s*#/) {
69         print "ok $. # (Blank line or comment)\n";
70         if (/\S/) { print $_ };
71         next;
72     }
73     chomp;
74     s/\\n/\n/g;
75     my ($pat, $subject, $result, $repl, $expect, $reason) = split(/\t/,$_,6);
76     $reason = '' unless defined $reason;
77     my $input = join(':',$pat,$subject,$result,$repl,$expect);
78     $pat = "'$pat'" unless $pat =~ /^[:'\/]/;
79     $pat =~ s/(\$\{\w+\})/$1/eeg;
80     $pat =~ s/\\n/\n/g;
81     $subject = eval qq("$subject");
82     $expect  = eval qq("$expect");
83     $expect = $repl = '-' if $skip_amp and $input =~ /\$[&\`\']/;
84     my $skip = ($skip_amp ? ($result =~ s/B//i) : ($result =~ s/B//));
85     $reason = 'skipping $&' if $reason eq  '' && $skip_amp;
86     $result =~ s/B//i unless $skip;
87
88     for my $study ('', 'study $subject') {
89         my $c = $iters;
90         my ($code, $match, $got);
91         if ($repl eq 'pos') {
92             $code= <<EOFCODE;
93                 $study;
94                 pos(\$subject)=0;
95                 \$match = ( \$subject =~ m${pat}g );
96                 \$got = pos(\$subject);
97 EOFCODE
98         }
99         elsif ($qr_embed) {
100             $code= <<EOFCODE;
101                 my \$RE = qr$pat;
102                 $study;
103                 \$match = (\$subject =~ /(?:)\$RE(?:)/) while \$c--;
104                 \$got = "$repl";
105 EOFCODE
106         }
107         else {
108             $code= <<EOFCODE;
109                 $study;
110                 \$match = (\$subject =~ $OP$pat) while \$c--;
111                 \$got = "$repl";
112 EOFCODE
113         }
114         {
115             # Probably we should annotate specific tests with which warnings
116             # categories they're known to trigger, and hence should be
117             # disabled just for that test
118             no warnings qw(uninitialized regexp);
119             eval $code;
120         }
121         chomp( my $err = $@ );
122         if ($result eq 'c') {
123             if ($err !~ m!^\Q$expect!) { print "not ok $. (compile) $input => `$err'\n"; next TEST }
124             last;  # no need to study a syntax error
125         }
126         elsif ( $skip ) {
127             print "ok $. # skipped", length($reason) ? " $reason" : '', "\n";
128             next TEST;
129         }
130         elsif ($@) {
131             print "not ok $. $input => error `$err'\n$code\n$@\n"; next TEST;
132         }
133         elsif ($result eq 'n') {
134             if ($match) { print "not ok $. ($study) $input => false positive\n"; next TEST }
135         }
136         else {
137             if (!$match || $got ne $expect) {
138                 eval { require Data::Dumper };
139                 if ($@) {
140                     print "not ok $. ($study) $input => `$got', match=$match\n$code\n";
141                 }
142                 else { # better diagnostics
143                     my $s = Data::Dumper->new([$subject],['subject'])->Useqq(1)->Dump;
144                     my $g = Data::Dumper->new([$got],['got'])->Useqq(1)->Dump;
145                     print "not ok $. ($study) $input => `$got', match=$match\n$s\n$g\n$code\n";
146                 }
147                 next TEST;
148             }
149         }
150     }
151     print "ok $.\n";
152 }
153
154 close(TESTS);