1N/A#!./perl
1N/A
1N/A# The tests are in a separate file 't/op/re_tests'.
1N/A# Each line in that file is a separate test.
1N/A# There are five columns, separated by tabs.
1N/A#
1N/A# Column 1 contains the pattern, optionally enclosed in C<''>.
1N/A# Modifiers can be put after the closing C<'>.
1N/A#
1N/A# Column 2 contains the string to be matched.
1N/A#
1N/A# Column 3 contains the expected result:
1N/A# y expect a match
1N/A# n expect no match
1N/A# c expect an error
1N/A# B test exposes a known bug in Perl, should be skipped
1N/A# b test exposes a known bug in Perl, should be skipped if noamp
1N/A#
1N/A# Columns 4 and 5 are used only if column 3 contains C<y> or C<c>.
1N/A#
1N/A# Column 4 contains a string, usually C<$&>.
1N/A#
1N/A# Column 5 contains the expected result of double-quote
1N/A# interpolating that string after the match, or start of error message.
1N/A#
1N/A# Column 6, if present, contains a reason why the test is skipped.
1N/A# This is printed with "skipped", for harness to pick up.
1N/A#
1N/A# \n in the tests are interpolated, as are variables of the form ${\w+}.
1N/A#
1N/A# If you want to add a regular expression test that can't be expressed
1N/A# in this format, don't add it here: put it in op/pat.t instead.
1N/A
1N/ABEGIN {
1N/A chdir 't' if -d 't';
1N/A @INC = '../lib';
1N/A}
1N/A
1N/A$iters = shift || 1; # Poor man performance suite, 10000 is OK.
1N/A
1N/Aopen(TESTS,'op/re_tests') || open(TESTS,'t/op/re_tests') || open(TESTS,':op:re_tests') ||
1N/A die "Can't open re_tests";
1N/A
1N/Awhile (<TESTS>) { }
1N/A$numtests = $.;
1N/Aseek(TESTS,0,0);
1N/A$. = 0;
1N/A
1N/A$bang = sprintf "\\%03o", ord "!"; # \41 would not be portable.
1N/A$ffff = chr(0xff) x 2;
1N/A$nulnul = "\0" x 2;
1N/A
1N/A$| = 1;
1N/Aprint "1..$numtests\n# $iters iterations\n";
1N/ATEST:
1N/Awhile (<TESTS>) {
1N/A chomp;
1N/A s/\\n/\n/g;
1N/A ($pat, $subject, $result, $repl, $expect, $reason) = split(/\t/,$_,6);
1N/A $input = join(':',$pat,$subject,$result,$repl,$expect);
1N/A infty_subst(\$pat);
1N/A infty_subst(\$expect);
1N/A $pat = "'$pat'" unless $pat =~ /^[:']/;
1N/A $pat =~ s/(\$\{\w+\})/$1/eeg;
1N/A $pat =~ s/\\n/\n/g;
1N/A $subject =~ s/(\$\{\w+\})/$1/eeg;
1N/A $subject =~ s/\\n/\n/g;
1N/A $expect =~ s/(\$\{\w+\})/$1/eeg;
1N/A $expect =~ s/\\n/\n/g;
1N/A $expect = $repl = '-' if $skip_amp and $input =~ /\$[&\`\']/;
1N/A $skip = ($skip_amp ? ($result =~ s/B//i) : ($result =~ s/B//));
1N/A $reason = 'skipping $&' if $reason eq '' && $skip_amp;
1N/A $result =~ s/B//i unless $skip;
1N/A for $study ('', 'study \$subject') {
1N/A $c = $iters;
1N/A eval "$study; \$match = (\$subject =~ m$pat) while \$c--; \$got = \"$repl\";";
1N/A chomp( $err = $@ );
1N/A if ($result eq 'c') {
1N/A if ($err !~ m!^\Q$expect!) { print "not ok $. (compile) $input => `$err'\n"; next TEST }
1N/A last; # no need to study a syntax error
1N/A }
1N/A elsif ( $skip ) {
1N/A print "ok $. # skipped", length($reason) ? " $reason" : '', "\n";
1N/A next TEST;
1N/A }
1N/A elsif ($@) {
1N/A print "not ok $. $input => error `$err'\n"; next TEST;
1N/A }
1N/A elsif ($result eq 'n') {
1N/A if ($match) { print "not ok $. ($study) $input => false positive\n"; next TEST }
1N/A }
1N/A else {
1N/A if (!$match || $got ne $expect) {
1N/A print "not ok $. ($study) $input => `$got', match=$match\n";
1N/A next TEST;
1N/A }
1N/A }
1N/A }
1N/A print "ok $.\n";
1N/A}
1N/A
1N/Aclose(TESTS);
1N/A
1N/Asub infty_subst # Special-case substitution
1N/A{ # of $reg_infty and friends
1N/A my $tp = shift;
1N/A $$tp =~ s/,\$reg_infty_m}/,$reg_infty_m}/o;
1N/A $$tp =~ s/,\$reg_infty_p}/,$reg_infty_p}/o;
1N/A $$tp =~ s/,\$reg_infty}/,$reg_infty}/o;
1N/A}