This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Move the special-case logic for $qr_embed_thr to regexp_qr_embed_thr.t
[perl5.git] / t / re / regexp.t
CommitLineData
378cc40b
LW
1#!./perl
2
ae34ee58 3# The tests are in a separate file 't/re/re_tests'.
ad4f75a6
HM
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
24d786f4 16# T the test is a TODO (can be combined with y/n/c)
cb6fa888 17# M skip test on miniperl (combine with y/n/c/T)
cf93c79d
IZ
18# B test exposes a known bug in Perl, should be skipped
19# b test exposes a known bug in Perl, should be skipped if noamp
e3faa678 20# t test exposes a bug with threading, TODO if qr_embed_thr
ad4f75a6 21#
1b1626e4 22# Columns 4 and 5 are used only if column 3 contains C<y> or C<c>.
ad4f75a6
HM
23#
24# Column 4 contains a string, usually C<$&>.
25#
26# Column 5 contains the expected result of double-quote
c277df42
IZ
27# interpolating that string after the match, or start of error message.
28#
ee595aa6
LC
29# Column 6, if present, contains a reason why the test is skipped.
30# This is printed with "skipped", for harness to pick up.
31#
9d116dd7 32# \n in the tests are interpolated, as are variables of the form ${\w+}.
83e898de 33#
b9b4dddf
YO
34# Blanks lines are treated as PASSING tests to keep the line numbers
35# linked to the test number.
36#
8d37f932 37# If you want to add a regular expression test that can't be expressed
67a2b8c6 38# in this format, don't add it here: put it in re/pat.t instead.
b2a156bd 39#
ff3f963a
KW
40# Note that the inputs get passed on as "m're'", so the re bypasses the lexer.
41# This means this file cannot be used for testing anything that the lexer
42# handles; in 5.12 this means just \N{NAME} and \N{U+...}.
43#
b2a156bd
DM
44# Note that columns 2,3 and 5 are all enclosed in double quotes and then
45# evalled; so something like a\"\x{100}$1 has length 3+length($1).
c277df42 46
1a610890 47my $file;
e4d48cc9 48BEGIN {
1a610890
NC
49 $iters = shift || 1; # Poor man performance suite, 10000 is OK.
50
51 # Do this open before any chdir
52 $file = shift;
53 if (defined $file) {
54 open TESTS, $file or die "Can't open $file";
55 }
56
e4d48cc9 57 chdir 't' if -d 't';
20822f61 58 @INC = '../lib';
e3faa678 59
e4d48cc9 60}
1a610890 61
1286eaeb 62use strict;
66fb63c1 63use warnings FATAL=>"all";
1286eaeb 64use vars qw($iters $numtests $bang $ffff $nulnul $OP);
e3faa678 65use vars qw($qr $skip_amp $qr_embed $qr_embed_thr); # set by our callers
e4d48cc9 66
ad4f75a6 67
1a610890 68if (!defined $file) {
a4499558
YO
69 open(TESTS,'re/re_tests') || open(TESTS,'t/re/re_tests')
70 || open(TESTS,':re:re_tests') || die "Can't open re_tests";
1a610890
NC
71}
72
73my @tests = <TESTS>;
cfa4f241 74
1a610890 75close TESTS;
378cc40b 76
9d116dd7 77$bang = sprintf "\\%03o", ord "!"; # \41 would not be portable.
b8c5462f
JH
78$ffff = chr(0xff) x 2;
79$nulnul = "\0" x 2;
7fba1cd6 80$OP = $qr ? 'qr' : 'm';
9d116dd7 81
1462b684 82$| = 1;
1a610890 83printf "1..%d\n# $iters iterations\n", scalar @tests;
e3faa678 84
1a610890 85my $test;
cfa4f241 86TEST:
1a610890
NC
87foreach (@tests) {
88 $test++;
5a51db05 89 if (!/\S/ || /^\s*#/ || /^__END__$/) {
1a610890 90 print "ok $test # (Blank line or comment)\n";
5a51db05 91 if (/#/) { print $_ };
b9b4dddf
YO
92 next;
93 }
b85d18e9
IZ
94 chomp;
95 s/\\n/\n/g;
1286eaeb 96 my ($pat, $subject, $result, $repl, $expect, $reason) = split(/\t/,$_,6);
66fb63c1 97 $reason = '' unless defined $reason;
1286eaeb 98 my $input = join(':',$pat,$subject,$result,$repl,$expect);
24d786f4
YO
99 # the double '' below keeps simple syntax highlighters from going crazy
100 $pat = "'$pat'" unless $pat =~ /^[:''\/]/;
9d116dd7 101 $pat =~ s/(\$\{\w+\})/$1/eeg;
b8c5462f 102 $pat =~ s/\\n/\n/g;
1a610890
NC
103 $subject = eval qq("$subject"); die $@ if $@;
104 $expect = eval qq("$expect"); die $@ if $@;
c277df42 105 $expect = $repl = '-' if $skip_amp and $input =~ /\$[&\`\']/;
24d786f4 106 my $todo_qr = $qr_embed_thr && ($result =~ s/t//);
1286eaeb 107 my $skip = ($skip_amp ? ($result =~ s/B//i) : ($result =~ s/B//));
cb6fa888 108 ++$skip if $result =~ s/M// && !defined &DynaLoader::boot_DynaLoader;
906e884f 109 $reason = 'skipping $&' if $reason eq '' && $skip_amp;
cf93c79d 110 $result =~ s/B//i unless $skip;
24d786f4
YO
111 my $todo= $result =~ s/T// ? " # TODO" : "";
112
1de06328 113
52e33015
NC
114 for my $study ('', 'study $subject', 'utf8::upgrade($subject)',
115 'utf8::upgrade($subject); study $subject') {
93f09d7b 116 # Need to make a copy, else the utf8::upgrade of an already studied
52e33015
NC
117 # scalar confuses things.
118 my $subject = $subject;
1286eaeb
NC
119 my $c = $iters;
120 my ($code, $match, $got);
1de06328
YO
121 if ($repl eq 'pos') {
122 $code= <<EOFCODE;
123 $study;
124 pos(\$subject)=0;
125 \$match = ( \$subject =~ m${pat}g );
126 \$got = pos(\$subject);
127EOFCODE
128 }
129 elsif ($qr_embed) {
130 $code= <<EOFCODE;
131 my \$RE = qr$pat;
132 $study;
133 \$match = (\$subject =~ /(?:)\$RE(?:)/) while \$c--;
134 \$got = "$repl";
135EOFCODE
136 }
e3faa678
NC
137 elsif ($qr_embed_thr) {
138 $code= <<EOFCODE;
139 # Can't run the match in a subthread, but can do this and
140 # clone the pattern the other way.
141 my \$RE = threads->new(sub {qr$pat})->join();
142 $study;
143 \$match = (\$subject =~ /(?:)\$RE(?:)/) while \$c--;
144 \$got = "$repl";
145EOFCODE
146 }
1de06328
YO
147 else {
148 $code= <<EOFCODE;
149 $study;
1286eaeb 150 \$match = (\$subject =~ $OP$pat) while \$c--;
1de06328
YO
151 \$got = "$repl";
152EOFCODE
153 }
e1d1eefb
YO
154 #$code.=qq[\n\$expect="$expect";\n];
155 #use Devel::Peek;
156 #die Dump($code) if $pat=~/\\h/ and $subject=~/\x{A0}/;
66fb63c1
NC
157 {
158 # Probably we should annotate specific tests with which warnings
159 # categories they're known to trigger, and hence should be
160 # disabled just for that test
161 no warnings qw(uninitialized regexp);
162 eval $code;
163 }
1286eaeb 164 chomp( my $err = $@ );
cfa4f241 165 if ($result eq 'c') {
24d786f4 166 if ($err !~ m!^\Q$expect!) { print "not ok $test$todo (compile) $input => `$err'\n"; next TEST }
cfa4f241
CS
167 last; # no need to study a syntax error
168 }
cf93c79d 169 elsif ( $skip ) {
1a610890 170 print "ok $test # skipped", length($reason) ? " $reason" : '', "\n";
ee595aa6 171 next TEST;
cf93c79d 172 }
24d786f4 173 elsif ( $todo_qr ) {
e0892690 174 print "not ok $test # TODO", length($reason) ? " - $reason" : '', "\n";
e3faa678
NC
175 next TEST;
176 }
c277df42 177 elsif ($@) {
24d786f4 178 print "not ok $test$todo $input => error `$err'\n$code\n$@\n"; next TEST;
c277df42 179 }
e3faa678 180 elsif ($result =~ /^n/) {
24d786f4 181 if ($match) { print "not ok $test$todo ($study) $input => false positive\n"; next TEST }
378cc40b
LW
182 }
183 else {
cfa4f241 184 if (!$match || $got ne $expect) {
cde0cee5
YO
185 eval { require Data::Dumper };
186 if ($@) {
24d786f4 187 print "not ok $test$todo ($study) $input => `$got', match=$match\n$code\n";
cde0cee5
YO
188 }
189 else { # better diagnostics
190 my $s = Data::Dumper->new([$subject],['subject'])->Useqq(1)->Dump;
191 my $g = Data::Dumper->new([$got],['got'])->Useqq(1)->Dump;
24d786f4 192 print "not ok $test$todo ($study) $input => `$got', match=$match\n$s\n$g\n$code\n";
cde0cee5 193 }
cfa4f241
CS
194 next TEST;
195 }
378cc40b
LW
196 }
197 }
24d786f4 198 print "ok $test$todo\n";
378cc40b 199}
cfa4f241 200
1a610890 2011;