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
19 # Columns 4 and 5 are used only if column 3 contains C<y> or C<c>.
21 # Column 4 contains a string, usually C<$&>.
23 # Column 5 contains the expected result of double-quote
24 # interpolating that string after the match, or start of error message.
26 # Column 6, if present, contains a reason why the test is skipped.
27 # This is printed with "skipped", for harness to pick up.
29 # \n in the tests are interpolated, as are variables of the form ${\w+}.
31 # Blanks lines are treated as PASSING tests to keep the line numbers
32 # linked to the test number.
34 # If you want to add a regular expression test that can't be expressed
35 # in this format, don't add it here: put it in op/pat.t instead.
37 # Note that columns 2,3 and 5 are all enclosed in double quotes and then
38 # evalled; so something like a\"\x{100}$1 has length 3+length($1).
45 use vars qw($iters $numtests $bang $ffff $nulnul $OP);
46 use vars qw($qr $skip_amp $qr_embed); # set by our callers
48 $iters = shift || 1; # Poor man performance suite, 10000 is OK.
50 open(TESTS,'op/re_tests') || open(TESTS,'t/op/re_tests') || open(TESTS,':op:re_tests') ||
51 die "Can't open re_tests";
58 $bang = sprintf "\\%03o", ord "!"; # \41 would not be portable.
59 $ffff = chr(0xff) x 2;
61 $OP = $qr ? 'qr' : 'm';
64 print "1..$numtests\n# $iters iterations\n";
67 if (!/\S/ || /^\s*#/) {
68 print "ok $. # (Blank line or comment)\n";
69 if (/\S/) { print $_ };
74 my ($pat, $subject, $result, $repl, $expect, $reason) = split(/\t/,$_,6);
75 my $input = join(':',$pat,$subject,$result,$repl,$expect);
76 $pat = "'$pat'" unless $pat =~ /^[:'\/]/;
77 $pat =~ s/(\$\{\w+\})/$1/eeg;
79 $subject = eval qq("$subject");
80 $expect = eval qq("$expect");
81 $expect = $repl = '-' if $skip_amp and $input =~ /\$[&\`\']/;
82 my $skip = ($skip_amp ? ($result =~ s/B//i) : ($result =~ s/B//));
83 $reason = 'skipping $&' if $reason eq '' && $skip_amp;
84 $result =~ s/B//i unless $skip;
86 for my $study ('', 'study $subject') {
88 my ($code, $match, $got);
93 \$match = ( \$subject =~ m${pat}g );
94 \$got = pos(\$subject);
101 \$match = (\$subject =~ /(?:)\$RE(?:)/) while \$c--;
108 \$match = (\$subject =~ $OP$pat) while \$c--;
113 chomp( my $err = $@ );
114 if ($result eq 'c') {
115 if ($err !~ m!^\Q$expect!) { print "not ok $. (compile) $input => `$err'\n"; next TEST }
116 last; # no need to study a syntax error
119 print "ok $. # skipped", length($reason) ? " $reason" : '', "\n";
123 print "not ok $. $input => error `$err'\n$code\n$@\n"; next TEST;
125 elsif ($result eq 'n') {
126 if ($match) { print "not ok $. ($study) $input => false positive\n"; next TEST }
129 if (!$match || $got ne $expect) {
130 eval { require Data::Dumper };
132 print "not ok $. ($study) $input => `$got', match=$match\n$code\n";
134 else { # better diagnostics
135 my $s = Data::Dumper->new([$subject],['subject'])->Useqq(1)->Dump;
136 my $g = Data::Dumper->new([$got],['got'])->Useqq(1)->Dump;
137 print "not ok $. ($study) $input => `$got', match=$match\n$s\n$g\n$code\n";