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.
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 # 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
18 # t test exposes a bug with threading, TODO if qr_embed_thr
20 # Columns 4 and 5 are used only if column 3 contains C<y> or C<c>.
22 # Column 4 contains a string, usually C<$&>.
24 # Column 5 contains the expected result of double-quote
25 # interpolating that string after the match, or start of error message.
27 # Column 6, if present, contains a reason why the test is skipped.
28 # This is printed with "skipped", for harness to pick up.
30 # \n in the tests are interpolated, as are variables of the form ${\w+}.
32 # Blanks lines are treated as PASSING tests to keep the line numbers
33 # linked to the test number.
35 # If you want to add a regular expression test that can't be expressed
36 # in this format, don't add it here: put it in op/pat.t instead.
38 # Note that columns 2,3 and 5 are all enclosed in double quotes and then
39 # evalled; so something like a\"\x{100}$1 has length 3+length($1).
43 $iters = shift || 1; # Poor man performance suite, 10000 is OK.
45 # Do this open before any chdir
48 open TESTS, $file or die "Can't open $file";
56 if (!$Config::Config{useithreads}) {
57 print "1..0 # Skip: no ithreads\n";
60 if ($ENV{PERL_CORE_MINITEST}) {
61 print "1..0 # Skip: no dynamic loading on miniperl, no threads\n";
69 use warnings FATAL=>"all";
70 use vars qw($iters $numtests $bang $ffff $nulnul $OP);
71 use vars qw($qr $skip_amp $qr_embed $qr_embed_thr); # set by our callers
75 open(TESTS,'op/re_tests') || open(TESTS,'t/op/re_tests')
76 || open(TESTS,':op:re_tests') || die "Can't open re_tests";
83 $bang = sprintf "\\%03o", ord "!"; # \41 would not be portable.
84 $ffff = chr(0xff) x 2;
86 $OP = $qr ? 'qr' : 'm';
89 printf "1..%d\n# $iters iterations\n", scalar @tests;
95 if (!/\S/ || /^\s*#/ || /^__END__$/) {
96 print "ok $test # (Blank line or comment)\n";
97 if (/#/) { print $_ };
102 my ($pat, $subject, $result, $repl, $expect, $reason) = split(/\t/,$_,6);
103 $reason = '' unless defined $reason;
104 my $input = join(':',$pat,$subject,$result,$repl,$expect);
105 $pat = "'$pat'" unless $pat =~ /^[:'\/]/;
106 $pat =~ s/(\$\{\w+\})/$1/eeg;
108 $subject = eval qq("$subject"); die $@ if $@;
109 $expect = eval qq("$expect"); die $@ if $@;
110 $expect = $repl = '-' if $skip_amp and $input =~ /\$[&\`\']/;
111 my $todo = $qr_embed_thr && ($result =~ s/t//);
112 my $skip = ($skip_amp ? ($result =~ s/B//i) : ($result =~ s/B//));
113 $reason = 'skipping $&' if $reason eq '' && $skip_amp;
114 $result =~ s/B//i unless $skip;
116 for my $study ('', 'study $subject', 'utf8::upgrade($subject)',
117 'utf8::upgrade($subject); study $subject') {
118 # Need to make a copy, else the utf8::upgrade of an alreay studied
119 # scalar confuses things.
120 my $subject = $subject;
122 my ($code, $match, $got);
123 if ($repl eq 'pos') {
127 \$match = ( \$subject =~ m${pat}g );
128 \$got = pos(\$subject);
135 \$match = (\$subject =~ /(?:)\$RE(?:)/) while \$c--;
139 elsif ($qr_embed_thr) {
141 # Can't run the match in a subthread, but can do this and
142 # clone the pattern the other way.
143 my \$RE = threads->new(sub {qr$pat})->join();
145 \$match = (\$subject =~ /(?:)\$RE(?:)/) while \$c--;
152 \$match = (\$subject =~ $OP$pat) while \$c--;
156 #$code.=qq[\n\$expect="$expect";\n];
158 #die Dump($code) if $pat=~/\\h/ and $subject=~/\x{A0}/;
160 # Probably we should annotate specific tests with which warnings
161 # categories they're known to trigger, and hence should be
162 # disabled just for that test
163 no warnings qw(uninitialized regexp);
166 chomp( my $err = $@ );
167 if ($result eq 'c') {
168 if ($err !~ m!^\Q$expect!) { print "not ok $test (compile) $input => `$err'\n"; next TEST }
169 last; # no need to study a syntax error
172 print "ok $test # skipped", length($reason) ? " $reason" : '', "\n";
176 print "not ok $test # todo", length($reason) ? " - $reason" : '', "\n";
180 print "not ok $test $input => error `$err'\n$code\n$@\n"; next TEST;
182 elsif ($result =~ /^n/) {
183 if ($match) { print "not ok $test ($study) $input => false positive\n"; next TEST }
186 if (!$match || $got ne $expect) {
187 eval { require Data::Dumper };
189 print "not ok $test ($study) $input => `$got', match=$match\n$code\n";
191 else { # better diagnostics
192 my $s = Data::Dumper->new([$subject],['subject'])->Useqq(1)->Dump;
193 my $g = Data::Dumper->new([$got],['got'])->Useqq(1)->Dump;
194 print "not ok $test ($study) $input => `$got', match=$match\n$s\n$g\n$code\n";