This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Make t/op/regexp.t warnings clean.
[perl5.git] / t / op / regexp.t
CommitLineData
378cc40b
LW
1#!./perl
2
ad4f75a6
HM
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
cf93c79d
IZ
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
ad4f75a6 18#
1b1626e4 19# Columns 4 and 5 are used only if column 3 contains C<y> or C<c>.
ad4f75a6
HM
20#
21# Column 4 contains a string, usually C<$&>.
22#
23# Column 5 contains the expected result of double-quote
c277df42
IZ
24# interpolating that string after the match, or start of error message.
25#
ee595aa6
LC
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#
9d116dd7 29# \n in the tests are interpolated, as are variables of the form ${\w+}.
83e898de 30#
b9b4dddf
YO
31# Blanks lines are treated as PASSING tests to keep the line numbers
32# linked to the test number.
33#
8d37f932
DD
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.
b2a156bd
DM
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).
c277df42 39
e4d48cc9
GS
40BEGIN {
41 chdir 't' if -d 't';
20822f61 42 @INC = '../lib';
e4d48cc9 43}
1286eaeb 44use strict;
66fb63c1 45use warnings FATAL=>"all";
1286eaeb
NC
46use vars qw($iters $numtests $bang $ffff $nulnul $OP);
47use vars qw($qr $skip_amp $qr_embed); # set by our callers
e4d48cc9 48
c277df42 49$iters = shift || 1; # Poor man performance suite, 10000 is OK.
ad4f75a6 50
95e8664e 51open(TESTS,'op/re_tests') || open(TESTS,'t/op/re_tests') || open(TESTS,':op:re_tests') ||
8d37f932 52 die "Can't open re_tests";
cfa4f241 53
378cc40b
LW
54while (<TESTS>) { }
55$numtests = $.;
cfa4f241
CS
56seek(TESTS,0,0);
57$. = 0;
378cc40b 58
9d116dd7 59$bang = sprintf "\\%03o", ord "!"; # \41 would not be portable.
b8c5462f
JH
60$ffff = chr(0xff) x 2;
61$nulnul = "\0" x 2;
7fba1cd6 62$OP = $qr ? 'qr' : 'm';
9d116dd7 63
1462b684 64$| = 1;
c277df42 65print "1..$numtests\n# $iters iterations\n";
cfa4f241 66TEST:
378cc40b 67while (<TESTS>) {
b9b4dddf
YO
68 if (!/\S/ || /^\s*#/) {
69 print "ok $. # (Blank line or comment)\n";
70 if (/\S/) { print $_ };
71 next;
72 }
b85d18e9
IZ
73 chomp;
74 s/\\n/\n/g;
1286eaeb 75 my ($pat, $subject, $result, $repl, $expect, $reason) = split(/\t/,$_,6);
66fb63c1 76 $reason = '' unless defined $reason;
1286eaeb 77 my $input = join(':',$pat,$subject,$result,$repl,$expect);
81714fb9 78 $pat = "'$pat'" unless $pat =~ /^[:'\/]/;
9d116dd7 79 $pat =~ s/(\$\{\w+\})/$1/eeg;
b8c5462f 80 $pat =~ s/\\n/\n/g;
b2a156bd
DM
81 $subject = eval qq("$subject");
82 $expect = eval qq("$expect");
c277df42 83 $expect = $repl = '-' if $skip_amp and $input =~ /\$[&\`\']/;
1286eaeb 84 my $skip = ($skip_amp ? ($result =~ s/B//i) : ($result =~ s/B//));
906e884f 85 $reason = 'skipping $&' if $reason eq '' && $skip_amp;
cf93c79d 86 $result =~ s/B//i unless $skip;
1de06328 87
1286eaeb
NC
88 for my $study ('', 'study $subject') {
89 my $c = $iters;
90 my ($code, $match, $got);
1de06328
YO
91 if ($repl eq 'pos') {
92 $code= <<EOFCODE;
93 $study;
94 pos(\$subject)=0;
95 \$match = ( \$subject =~ m${pat}g );
96 \$got = pos(\$subject);
97EOFCODE
98 }
99 elsif ($qr_embed) {
100 $code= <<EOFCODE;
101 my \$RE = qr$pat;
102 $study;
103 \$match = (\$subject =~ /(?:)\$RE(?:)/) while \$c--;
104 \$got = "$repl";
105EOFCODE
106 }
107 else {
108 $code= <<EOFCODE;
109 $study;
1286eaeb 110 \$match = (\$subject =~ $OP$pat) while \$c--;
1de06328
YO
111 \$got = "$repl";
112EOFCODE
113 }
66fb63c1
NC
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 }
1286eaeb 121 chomp( my $err = $@ );
cfa4f241 122 if ($result eq 'c') {
c277df42 123 if ($err !~ m!^\Q$expect!) { print "not ok $. (compile) $input => `$err'\n"; next TEST }
cfa4f241
CS
124 last; # no need to study a syntax error
125 }
cf93c79d 126 elsif ( $skip ) {
ee595aa6
LC
127 print "ok $. # skipped", length($reason) ? " $reason" : '', "\n";
128 next TEST;
cf93c79d 129 }
c277df42 130 elsif ($@) {
1de06328 131 print "not ok $. $input => error `$err'\n$code\n$@\n"; next TEST;
c277df42 132 }
cfa4f241 133 elsif ($result eq 'n') {
c277df42 134 if ($match) { print "not ok $. ($study) $input => false positive\n"; next TEST }
378cc40b
LW
135 }
136 else {
cfa4f241 137 if (!$match || $got ne $expect) {
cde0cee5
YO
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 }
cfa4f241
CS
147 next TEST;
148 }
378cc40b
LW
149 }
150 }
cfa4f241 151 print "ok $.\n";
378cc40b 152}
cfa4f241 153
378cc40b 154close(TESTS);