This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
reg_mesg.t: Test messages from (?[ ])
[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
7e1dab6a 47my ($file, $iters);
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
1b7228c9
KW
62sub _comment {
63 return map { /^#/ ? "$_\n" : "# $_\n" }
64 map { split /\n/ } @_;
65}
66
1286eaeb 67use strict;
66fb63c1 68use warnings FATAL=>"all";
7e1dab6a 69use vars qw($bang $ffff $nulnul); # used by the tests
e3faa678 70use vars qw($qr $skip_amp $qr_embed $qr_embed_thr); # set by our callers
e4d48cc9 71
ad4f75a6 72
1a610890 73if (!defined $file) {
7e1dab6a 74 open TESTS, 're/re_tests' or die "Can't open re/re_tests: $!";
1a610890
NC
75}
76
77my @tests = <TESTS>;
cfa4f241 78
1a610890 79close TESTS;
378cc40b 80
9d116dd7 81$bang = sprintf "\\%03o", ord "!"; # \41 would not be portable.
b8c5462f
JH
82$ffff = chr(0xff) x 2;
83$nulnul = "\0" x 2;
7e1dab6a 84my $OP = $qr ? 'qr' : 'm';
9d116dd7 85
1462b684 86$| = 1;
1a610890 87printf "1..%d\n# $iters iterations\n", scalar @tests;
e3faa678 88
1a610890 89my $test;
cfa4f241 90TEST:
1a610890
NC
91foreach (@tests) {
92 $test++;
5a51db05 93 if (!/\S/ || /^\s*#/ || /^__END__$/) {
1a610890 94 print "ok $test # (Blank line or comment)\n";
5a51db05 95 if (/#/) { print $_ };
b9b4dddf
YO
96 next;
97 }
b85d18e9
IZ
98 chomp;
99 s/\\n/\n/g;
1286eaeb 100 my ($pat, $subject, $result, $repl, $expect, $reason) = split(/\t/,$_,6);
66fb63c1 101 $reason = '' unless defined $reason;
1286eaeb 102 my $input = join(':',$pat,$subject,$result,$repl,$expect);
24d786f4
YO
103 # the double '' below keeps simple syntax highlighters from going crazy
104 $pat = "'$pat'" unless $pat =~ /^[:''\/]/;
9d116dd7 105 $pat =~ s/(\$\{\w+\})/$1/eeg;
b8c5462f 106 $pat =~ s/\\n/\n/g;
1a610890
NC
107 $subject = eval qq("$subject"); die $@ if $@;
108 $expect = eval qq("$expect"); die $@ if $@;
c277df42 109 $expect = $repl = '-' if $skip_amp and $input =~ /\$[&\`\']/;
24d786f4 110 my $todo_qr = $qr_embed_thr && ($result =~ s/t//);
1286eaeb 111 my $skip = ($skip_amp ? ($result =~ s/B//i) : ($result =~ s/B//));
cb6fa888 112 ++$skip if $result =~ s/M// && !defined &DynaLoader::boot_DynaLoader;
906e884f 113 $reason = 'skipping $&' if $reason eq '' && $skip_amp;
cf93c79d 114 $result =~ s/B//i unless $skip;
24d786f4
YO
115 my $todo= $result =~ s/T// ? " # TODO" : "";
116
1de06328 117
52e33015
NC
118 for my $study ('', 'study $subject', 'utf8::upgrade($subject)',
119 'utf8::upgrade($subject); study $subject') {
93f09d7b 120 # Need to make a copy, else the utf8::upgrade of an already studied
52e33015
NC
121 # scalar confuses things.
122 my $subject = $subject;
1286eaeb
NC
123 my $c = $iters;
124 my ($code, $match, $got);
1de06328
YO
125 if ($repl eq 'pos') {
126 $code= <<EOFCODE;
127 $study;
128 pos(\$subject)=0;
129 \$match = ( \$subject =~ m${pat}g );
130 \$got = pos(\$subject);
131EOFCODE
132 }
133 elsif ($qr_embed) {
134 $code= <<EOFCODE;
135 my \$RE = qr$pat;
136 $study;
137 \$match = (\$subject =~ /(?:)\$RE(?:)/) while \$c--;
138 \$got = "$repl";
139EOFCODE
140 }
e3faa678
NC
141 elsif ($qr_embed_thr) {
142 $code= <<EOFCODE;
143 # Can't run the match in a subthread, but can do this and
144 # clone the pattern the other way.
145 my \$RE = threads->new(sub {qr$pat})->join();
146 $study;
147 \$match = (\$subject =~ /(?:)\$RE(?:)/) while \$c--;
148 \$got = "$repl";
149EOFCODE
150 }
1de06328
YO
151 else {
152 $code= <<EOFCODE;
153 $study;
1286eaeb 154 \$match = (\$subject =~ $OP$pat) while \$c--;
1de06328
YO
155 \$got = "$repl";
156EOFCODE
157 }
e1d1eefb
YO
158 #$code.=qq[\n\$expect="$expect";\n];
159 #use Devel::Peek;
160 #die Dump($code) if $pat=~/\\h/ and $subject=~/\x{A0}/;
66fb63c1
NC
161 {
162 # Probably we should annotate specific tests with which warnings
163 # categories they're known to trigger, and hence should be
164 # disabled just for that test
165 no warnings qw(uninitialized regexp);
166 eval $code;
167 }
1286eaeb 168 chomp( my $err = $@ );
565b86e2 169 if ( $skip ) {
3c6cc85e 170 print "ok $test # skipped", length($reason) ? ". $reason" : '', "\n";
ee595aa6 171 next TEST;
cf93c79d 172 }
565b86e2
KW
173 elsif ($result eq 'c') {
174 if ($err !~ m!^\Q$expect!) { print "not ok $test$todo (compile) $input => '$err'\n"; next TEST }
175 last; # no need to study a syntax error
176 }
24d786f4 177 elsif ( $todo_qr ) {
e0892690 178 print "not ok $test # TODO", length($reason) ? " - $reason" : '', "\n";
e3faa678
NC
179 next TEST;
180 }
c277df42 181 elsif ($@) {
2fe1f0f5 182 print "not ok $test$todo $input => error '$err'\n", _comment("$code\n$@\n"); next TEST;
c277df42 183 }
e3faa678 184 elsif ($result =~ /^n/) {
24d786f4 185 if ($match) { print "not ok $test$todo ($study) $input => false positive\n"; next TEST }
378cc40b
LW
186 }
187 else {
cfa4f241 188 if (!$match || $got ne $expect) {
cde0cee5 189 eval { require Data::Dumper };
969c44e7 190 no warnings "utf8"; # But handle should be utf8
65016092
NC
191 if ($@ || !defined &DynaLoader::boot_DynaLoader) {
192 # Data::Dumper will load on miniperl, but fail when used in
193 # anger as it tries to load B. I'd prefer to keep the
194 # regular calls below outside of an eval so that real
195 # (unknown) failures get spotted, not ignored.
2fe1f0f5 196 print "not ok $test$todo ($study) $input => '$got', match=$match\n", _comment("$code\n");
cde0cee5
YO
197 }
198 else { # better diagnostics
199 my $s = Data::Dumper->new([$subject],['subject'])->Useqq(1)->Dump;
200 my $g = Data::Dumper->new([$got],['got'])->Useqq(1)->Dump;
2fe1f0f5 201 print "not ok $test$todo ($study) $input => '$got', match=$match\n", _comment("$s\n$g\n$code\n");
cde0cee5 202 }
cfa4f241
CS
203 next TEST;
204 }
378cc40b
LW
205 }
206 }
24d786f4 207 print "ok $test$todo\n";
378cc40b 208}
cfa4f241 209
1a610890 2101;