3 # The tests are in a separate file 't/re/re_tests'.
4 # Each line in that file is a separate test.
5 # There are five columns, separated by tabs.
7 # Column 1 contains the pattern, optionally enclosed in C<''>.
8 # Modifiers can be put after the closing C<'>.
10 # Column 2 contains the string to be matched.
12 # Column 3 contains the expected result:
16 # T the test is a TODO (can be combined with y/n/c)
17 # M skip test on miniperl (combine with y/n/c/T)
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
20 # t test exposes a bug with threading, TODO if qr_embed_thr
21 # s test should only be run for regex_sets_compat.t
22 # S test should not be run for regex_sets_compat.t
24 # Columns 4 and 5 are used only if column 3 contains C<y> or C<c>.
26 # Column 4 contains a string, usually C<$&>.
28 # Column 5 contains the expected result of double-quote
29 # interpolating that string after the match, or start of error message.
31 # Column 6, if present, contains a reason why the test is skipped.
32 # This is printed with "skipped", for harness to pick up.
34 # Column 7 can be used for comments
36 # \n in the tests are interpolated, as are variables of the form ${\w+}.
38 # Blanks lines are treated as PASSING tests to keep the line numbers
39 # linked to the test number.
41 # If you want to add a regular expression test that can't be expressed
42 # in this format, don't add it here: put it in re/pat.t instead.
44 # Note that the inputs get passed on as "m're'", so the re bypasses the lexer.
45 # This means this file cannot be used for testing anything that the lexer
46 # handles; in 5.12 this means just \N{NAME} and \N{U+...}.
48 # Note that columns 2,3 and 5 are all enclosed in double quotes and then
49 # evalled; so something like a\"\x{100}$1 has length 3+length($1).
53 $iters = shift || 1; # Poor man performance suite, 10000 is OK.
55 # Do this open before any chdir
58 open TESTS, $file or die "Can't open $file";
62 @INC = qw '../lib ../ext/re';
67 return map { /^#/ ? "$_\n" : "# $_\n" }
68 map { split /\n/ } @_;
72 use warnings FATAL=>"all";
73 use vars qw($bang $ffff $nulnul); # used by the tests
74 use vars qw($qr $skip_amp $qr_embed $qr_embed_thr $regex_sets); # set by our callers
79 open TESTS, 're/re_tests' or die "Can't open re/re_tests: $!";
86 $bang = sprintf "\\%03o", ord "!"; # \41 would not be portable.
87 $ffff = chr(0xff) x 2;
89 my $OP = $qr ? 'qr' : 'm';
92 printf "1..%d\n# $iters iterations\n", scalar @tests;
98 if (!/\S/ || /^\s*#/ || /^__END__$/) {
99 print "ok $test # (Blank line or comment)\n";
100 if (/#/) { print $_ };
104 s/\\n/\n/g unless $regex_sets;
105 my ($pat, $subject, $result, $repl, $expect, $reason) = split(/\t/,$_,6);
106 if (!defined $subject) {
107 die "Bad test definition on line $test: $_\n";
109 $reason = '' unless defined $reason;
110 my $input = join(':',$pat,$subject,$result,$repl,$expect);
111 # the double '' below keeps simple syntax highlighters from going crazy
112 $pat = "'$pat'" unless $pat =~ /^[:''\/]/;
113 $pat =~ s/(\$\{\w+\})/$1/eeg;
114 $pat =~ s/\\n/\n/g unless $regex_sets;
115 $subject = eval qq("$subject"); die $@ if $@;
116 $expect = eval qq("$expect"); die $@ if $@;
117 $expect = $repl = '-' if $skip_amp and $input =~ /\$[&\`\']/;
118 my $todo_qr = $qr_embed_thr && ($result =~ s/t//);
119 my $skip = ($skip_amp ? ($result =~ s/B//i) : ($result =~ s/B//));
120 ++$skip if $result =~ s/M// && !defined &DynaLoader::boot_DynaLoader;
121 if ($result =~ s/ ( [Ss] ) //x) {
122 if (($1 eq 'S' && $regex_sets) || ($1 eq 's' && ! $regex_sets)) {
124 $reason = "Test not valid for $0";
127 $reason = 'skipping $&' if $reason eq '' && $skip_amp;
128 $result =~ s/B//i unless $skip;
129 my $todo= $result =~ s/T// ? " # TODO" : "";
130 if (! $skip && $regex_sets) {
132 # If testing regex sets, change the [bracketed] classes into
135 if ($pat !~ / \[ /x) {
138 $reason = "Pattern doesn't contain [brackets]";
140 else { # Use non-regex features of Perl to accomplish this.
144 # Go through the pattern character-by-character. We also add
145 # blanks around each token to test the /x parts of (?[ ])
146 my $pat_len = length($pat);
147 CHAR: for (my $i = 0; $i < $pat_len; $i++) {
148 my $curchar = substr($pat, $i, 1);
149 if ($curchar eq '\\') {
150 $modified .= " " if $in_brackets;
151 $modified .= $curchar;
154 # Get the character the backslash is escaping
155 $curchar = substr($pat, $i, 1);
156 $modified .= $curchar;
158 # If the character following that is a '{}', treat the
159 # entire amount as a single token
160 if ($i < $pat_len -1 && substr($pat, $i+1, 1) eq '{') {
161 my $j = index($pat, '}', $i+2);
163 last unless $in_brackets;
164 if ($result eq 'c') {
166 $reason = "Can't handle compilation errors with unmatched '{'";
169 print "not ok $test # Problem in $0; original = '$pat'; mod = '$modified'\n";
173 $modified .= substr($pat, $i+1, $j - $i);
176 elsif ($curchar eq 'x') {
178 # \x without brackets is supposed to be followed by 2
179 # hex digits. Take up to 2, and then add a blank
180 # after the last one. This avoids getting errors from
181 # (?[ ]) for run-ons, like \xabc
183 for (; $j < $i + 3 && $j < $pat_len; $j++) {
184 my $curord = ord(substr($pat, $j, 1));
185 if (!(($curord >= ord("A") && $curord <= ord("F"))
186 || ($curord >= ord("a") && $curord <= ord("f"))
187 || ($curord >= ord("0") && $curord <= ord("9"))))
194 $modified .= substr($pat, $i + 1, $j - $i) . " ";
197 elsif (ord($curchar) >= ord('0')
198 && (ord($curchar) <= ord('7')))
200 # Similarly, octal constants have up to 3 digits.
202 for (; $j < $i + 3 && $j < $pat_len; $j++) {
203 my $curord = ord(substr($pat, $j, 1));
204 if (! ($curord >= ord("0") && $curord <= ord("7"))) {
210 $modified .= substr($pat, $i + 1, $j - $i);
215 } # End of processing a backslash sequence
217 if (! $in_brackets # Skip (?{ })
220 && substr($pat, $i+1, 1) eq '?'
221 && substr($pat, $i+2, 1) eq '{')
224 $reason = "Pattern contains '(?{'";
229 if ($curchar eq ']' && $in_brackets) {
230 $modified .= " ] ])";
235 # A regular character.
236 if ($curchar ne '[') {
237 if (! $in_brackets) {
238 $modified .= $curchar;
241 $modified .= " $curchar ";
246 # Here is a '['; If not in a bracketed class, treat as the
248 if (! $in_brackets) {
250 $modified .= "(?[ [ ";
252 # An immediately following ']' or '^]' is not the ending
253 # of the class, but is to be treated literally.
254 if ($i < $pat_len - 1
255 && substr($pat, $i+1, 1) eq ']')
260 elsif ($i < $pat_len - 2
261 && substr($pat, $i+1, 1) eq '^'
262 && substr($pat, $i+2, 1) eq ']')
265 $modified .= " ^ ] ";
270 # Here is a plain '[' within [ ]. Could mean wants to
271 # match a '[', or it could be a posix class that has a
272 # corresponding ']'. Absorb either
275 last if $i >= $pat_len - 1;
278 $curchar = substr($pat, $i, 1);
279 if ($curchar =~ /[:=.]/) {
280 for (my $j = $i + 1; $j < $pat_len; $j++) {
281 next unless substr($pat, $j, 1) eq ']';
283 if (substr($pat, $j - 1, 1) eq $curchar) {
284 # Here, is a posix class
285 $modified .= substr($pat, $i, $j - $i + 1) . " ";
292 # Here wasn't a posix class, just process normally
293 $modified .= " $curchar ";
296 if ($in_brackets && ! $skip) {
297 if ($result eq 'c') {
299 $reason = "Can't figure out where to put the (?[ and ]) since is a compilation error";
302 print "not ok $test # Problem in $0; original = '$pat'; mod = '$modified'\n";
307 # Use our modified pattern instead of the original
312 for my $study ('', 'study $subject', 'utf8::upgrade($subject)',
313 'utf8::upgrade($subject); study $subject') {
314 # Need to make a copy, else the utf8::upgrade of an already studied
315 # scalar confuses things.
316 my $subject = $subject;
318 my ($code, $match, $got);
319 if ($repl eq 'pos') {
323 \$match = ( \$subject =~ m${pat}g );
324 \$got = pos(\$subject);
331 \$match = (\$subject =~ /(?:)\$RE(?:)/) while \$c--;
335 elsif ($qr_embed_thr) {
337 # Can't run the match in a subthread, but can do this and
338 # clone the pattern the other way.
339 my \$RE = threads->new(sub {qr$pat})->join();
341 \$match = (\$subject =~ /(?:)\$RE(?:)/) while \$c--;
348 \$match = (\$subject =~ $OP$pat) while \$c--;
352 $code = "no warnings 'experimental::regex_sets';$code" if $regex_sets;
353 #$code.=qq[\n\$expect="$expect";\n];
355 #die Dump($code) if $pat=~/\\h/ and $subject=~/\x{A0}/;
357 # Probably we should annotate specific tests with which warnings
358 # categories they're known to trigger, and hence should be
359 # disabled just for that test
360 no warnings qw(uninitialized regexp);
363 chomp( my $err = $@ );
365 print "ok $test # skipped", length($reason) ? ". $reason" : '', "\n";
368 elsif ($result eq 'c') {
369 if ($err !~ m!^\Q$expect!) { print "not ok $test$todo (compile) $input => '$err'\n"; next TEST }
370 last; # no need to study a syntax error
373 print "not ok $test # TODO", length($reason) ? " - $reason" : '', "\n";
377 print "not ok $test$todo $input => error '$err'\n", _comment("$code\n$@\n"); next TEST;
379 elsif ($result =~ /^n/) {
380 if ($match) { print "not ok $test$todo ($study) $input => false positive\n"; next TEST }
383 if (!$match || $got ne $expect) {
384 eval { require Data::Dumper };
385 no warnings "utf8"; # But handle should be utf8
386 if ($@ || !defined &DynaLoader::boot_DynaLoader) {
387 # Data::Dumper will load on miniperl, but fail when used in
388 # anger as it tries to load B. I'd prefer to keep the
389 # regular calls below outside of an eval so that real
390 # (unknown) failures get spotted, not ignored.
391 print "not ok $test$todo ($study) $input => '$got', match=$match\n", _comment("$code\n");
393 else { # better diagnostics
394 my $s = Data::Dumper->new([$subject],['subject'])->Useqq(1)->Dump;
395 my $g = Data::Dumper->new([$got],['got'])->Useqq(1)->Dump;
396 print "not ok $test$todo ($study) $input => '$got', match=$match\n", _comment("$s\n$g\n$code\n");
402 print "ok $test$todo\n";