Upgrade to CPAN-1.8801.
[p5sagit/p5-mst-13.2.git] / t / op / regexp.t
CommitLineData
378cc40b 1#!./perl
2
ad4f75a6 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.
6#
7# Column 1 contains the pattern, optionally enclosed in C<''>.
8# Modifiers can be put after the closing C<'>.
9#
10# Column 2 contains the string to be matched.
11#
12# Column 3 contains the expected result:
13# y expect a match
14# n expect no match
15# c expect an error
cf93c79d 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
ad4f75a6 18#
1b1626e4 19# Columns 4 and 5 are used only if column 3 contains C<y> or C<c>.
ad4f75a6 20#
21# Column 4 contains a string, usually C<$&>.
22#
23# Column 5 contains the expected result of double-quote
c277df42 24# interpolating that string after the match, or start of error message.
25#
ee595aa6 26# Column 6, if present, contains a reason why the test is skipped.
27# This is printed with "skipped", for harness to pick up.
28#
9d116dd7 29# \n in the tests are interpolated, as are variables of the form ${\w+}.
83e898de 30#
8d37f932 31# If you want to add a regular expression test that can't be expressed
32# in this format, don't add it here: put it in op/pat.t instead.
c277df42 33
e4d48cc9 34BEGIN {
35 chdir 't' if -d 't';
20822f61 36 @INC = '../lib';
e4d48cc9 37}
38
c277df42 39$iters = shift || 1; # Poor man performance suite, 10000 is OK.
ad4f75a6 40
95e8664e 41open(TESTS,'op/re_tests') || open(TESTS,'t/op/re_tests') || open(TESTS,':op:re_tests') ||
8d37f932 42 die "Can't open re_tests";
cfa4f241 43
378cc40b 44while (<TESTS>) { }
45$numtests = $.;
cfa4f241 46seek(TESTS,0,0);
47$. = 0;
378cc40b 48
9d116dd7 49$bang = sprintf "\\%03o", ord "!"; # \41 would not be portable.
b8c5462f 50$ffff = chr(0xff) x 2;
51$nulnul = "\0" x 2;
7fba1cd6 52$OP = $qr ? 'qr' : 'm';
9d116dd7 53
1462b684 54$| = 1;
c277df42 55print "1..$numtests\n# $iters iterations\n";
cfa4f241 56TEST:
378cc40b 57while (<TESTS>) {
b85d18e9 58 chomp;
59 s/\\n/\n/g;
ee595aa6 60 ($pat, $subject, $result, $repl, $expect, $reason) = split(/\t/,$_,6);
378cc40b 61 $input = join(':',$pat,$subject,$result,$repl,$expect);
83e898de 62 infty_subst(\$pat);
63 infty_subst(\$expect);
1b1626e4 64 $pat = "'$pat'" unless $pat =~ /^[:']/;
9d116dd7 65 $pat =~ s/(\$\{\w+\})/$1/eeg;
b8c5462f 66 $pat =~ s/\\n/\n/g;
67 $subject =~ s/(\$\{\w+\})/$1/eeg;
c277df42 68 $subject =~ s/\\n/\n/g;
b8c5462f 69 $expect =~ s/(\$\{\w+\})/$1/eeg;
c277df42 70 $expect =~ s/\\n/\n/g;
71 $expect = $repl = '-' if $skip_amp and $input =~ /\$[&\`\']/;
cf93c79d 72 $skip = ($skip_amp ? ($result =~ s/B//i) : ($result =~ s/B//));
906e884f 73 $reason = 'skipping $&' if $reason eq '' && $skip_amp;
cf93c79d 74 $result =~ s/B//i unless $skip;
1de06328 75
76 for $study ('', 'study $subject') {
c277df42 77 $c = $iters;
1de06328 78 if ($repl eq 'pos') {
79 $code= <<EOFCODE;
80 $study;
81 pos(\$subject)=0;
82 \$match = ( \$subject =~ m${pat}g );
83 \$got = pos(\$subject);
84EOFCODE
85 }
86 elsif ($qr_embed) {
87 $code= <<EOFCODE;
88 my \$RE = qr$pat;
89 $study;
90 \$match = (\$subject =~ /(?:)\$RE(?:)/) while \$c--;
91 \$got = "$repl";
92EOFCODE
93 }
94 else {
95 $code= <<EOFCODE;
96 $study;
97 \$match = (\$subject =~ $OP$pat$addg) while \$c--;
98 \$got = "$repl";
99EOFCODE
100 }
101 eval $code;
c277df42 102 chomp( $err = $@ );
cfa4f241 103 if ($result eq 'c') {
c277df42 104 if ($err !~ m!^\Q$expect!) { print "not ok $. (compile) $input => `$err'\n"; next TEST }
cfa4f241 105 last; # no need to study a syntax error
106 }
cf93c79d 107 elsif ( $skip ) {
ee595aa6 108 print "ok $. # skipped", length($reason) ? " $reason" : '', "\n";
109 next TEST;
cf93c79d 110 }
c277df42 111 elsif ($@) {
1de06328 112 print "not ok $. $input => error `$err'\n$code\n$@\n"; next TEST;
c277df42 113 }
cfa4f241 114 elsif ($result eq 'n') {
c277df42 115 if ($match) { print "not ok $. ($study) $input => false positive\n"; next TEST }
378cc40b 116 }
117 else {
cfa4f241 118 if (!$match || $got ne $expect) {
1de06328 119 print "not ok $. ($study) $input => `$got', match=$match\n$code\n";
cfa4f241 120 next TEST;
121 }
378cc40b 122 }
123 }
cfa4f241 124 print "ok $.\n";
378cc40b 125}
cfa4f241 126
378cc40b 127close(TESTS);
83e898de 128
129sub infty_subst # Special-case substitution
130{ # of $reg_infty and friends
131 my $tp = shift;
132 $$tp =~ s/,\$reg_infty_m}/,$reg_infty_m}/o;
133 $$tp =~ s/,\$reg_infty_p}/,$reg_infty_p}/o;
134 $$tp =~ s/,\$reg_infty}/,$reg_infty}/o;
135}