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
22 # Columns 4 and 5 are used only if column 3 contains C<y> or C<c>.
24 # Column 4 contains a string, usually C<$&>.
26 # Column 5 contains the expected result of double-quote
27 # interpolating that string after the match, or start of error message.
29 # Column 6, if present, contains a reason why the test is skipped.
30 # This is printed with "skipped", for harness to pick up.
32 # \n in the tests are interpolated, as are variables of the form ${\w+}.
34 # Blanks lines are treated as PASSING tests to keep the line numbers
35 # linked to the test number.
37 # If you want to add a regular expression test that can't be expressed
38 # in this format, don't add it here: put it in re/pat.t instead.
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+...}.
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).
49 $iters = shift || 1; # Poor man performance suite, 10000 is OK.
51 # Do this open before any chdir
54 open TESTS, $file or die "Can't open $file";
63 return map { /^#/ ? "$_\n" : "# $_\n" }
64 map { split /\n/ } @_;
68 use warnings FATAL=>"all";
69 use vars qw($bang $ffff $nulnul); # used by the tests
70 use vars qw($qr $skip_amp $qr_embed $qr_embed_thr); # set by our callers
74 open TESTS, 're/re_tests' or die "Can't open re/re_tests: $!";
81 $bang = sprintf "\\%03o", ord "!"; # \41 would not be portable.
82 $ffff = chr(0xff) x 2;
84 my $OP = $qr ? 'qr' : 'm';
87 printf "1..%d\n# $iters iterations\n", scalar @tests;
93 if (!/\S/ || /^\s*#/ || /^__END__$/) {
94 print "ok $test # (Blank line or comment)\n";
95 if (/#/) { print $_ };
100 my ($pat, $subject, $result, $repl, $expect, $reason) = split(/\t/,$_,6);
101 $reason = '' unless defined $reason;
102 my $input = join(':',$pat,$subject,$result,$repl,$expect);
103 # the double '' below keeps simple syntax highlighters from going crazy
104 $pat = "'$pat'" unless $pat =~ /^[:''\/]/;
105 $pat =~ s/(\$\{\w+\})/$1/eeg;
107 $subject = eval qq("$subject"); die $@ if $@;
108 $expect = eval qq("$expect"); die $@ if $@;
109 $expect = $repl = '-' if $skip_amp and $input =~ /\$[&\`\']/;
110 my $todo_qr = $qr_embed_thr && ($result =~ s/t//);
111 my $skip = ($skip_amp ? ($result =~ s/B//i) : ($result =~ s/B//));
112 ++$skip if $result =~ s/M// && !defined &DynaLoader::boot_DynaLoader;
113 $reason = 'skipping $&' if $reason eq '' && $skip_amp;
114 $result =~ s/B//i unless $skip;
115 my $todo= $result =~ s/T// ? " # TODO" : "";
118 for my $study ('', 'study $subject', 'utf8::upgrade($subject)',
119 'utf8::upgrade($subject); study $subject') {
120 # Need to make a copy, else the utf8::upgrade of an already studied
121 # scalar confuses things.
122 my $subject = $subject;
124 my ($code, $match, $got);
125 if ($repl eq 'pos') {
129 \$match = ( \$subject =~ m${pat}g );
130 \$got = pos(\$subject);
137 \$match = (\$subject =~ /(?:)\$RE(?:)/) while \$c--;
141 elsif ($qr_embed_thr) {
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();
147 \$match = (\$subject =~ /(?:)\$RE(?:)/) while \$c--;
154 \$match = (\$subject =~ $OP$pat) while \$c--;
158 #$code.=qq[\n\$expect="$expect";\n];
160 #die Dump($code) if $pat=~/\\h/ and $subject=~/\x{A0}/;
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);
168 chomp( my $err = $@ );
169 if ($result eq 'c') {
170 if ($err !~ m!^\Q$expect!) { print "not ok $test$todo (compile) $input => '$err'\n"; next TEST }
171 last; # no need to study a syntax error
174 print "ok $test # skipped", length($reason) ? " $reason" : '', "\n";
178 print "not ok $test # TODO", length($reason) ? " - $reason" : '', "\n";
182 print "not ok $test$todo $input => error '$err'\n", _comment("$code\n$@\n"); next TEST;
184 elsif ($result =~ /^n/) {
185 if ($match) { print "not ok $test$todo ($study) $input => false positive\n"; next TEST }
188 if (!$match || $got ne $expect) {
189 eval { require Data::Dumper };
190 no warnings "utf8"; # But handle should be utf8
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.
196 print "not ok $test$todo ($study) $input => '$got', match=$match\n", _comment("$code\n");
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;
201 print "not ok $test$todo ($study) $input => '$got', match=$match\n", _comment("$s\n$g\n$code\n");
207 print "ok $test$todo\n";