This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Test case for C<undef %File::Glob::>
[perl5.git] / t / op / regexp.t
CommitLineData
378cc40b
LW
1#!./perl
2
ad4f75a6
HM
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
IZ
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
HM
20#
21# Column 4 contains a string, usually C<$&>.
22#
23# Column 5 contains the expected result of double-quote
c277df42
IZ
24# interpolating that string after the match, or start of error message.
25#
ee595aa6
LC
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
DD
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
GS
34BEGIN {
35 chdir 't' if -d 't';
20822f61 36 @INC = '../lib';
e4d48cc9
GS
37}
38
ffbc6a93
JH
39use re 'asciirange'; # ranges are computed in ASCII
40
c277df42 41$iters = shift || 1; # Poor man performance suite, 10000 is OK.
ad4f75a6 42
8d37f932
DD
43open(TESTS,'op/re_tests') || open(TESTS,'t/op/re_tests') ||
44 die "Can't open re_tests";
cfa4f241 45
378cc40b
LW
46while (<TESTS>) { }
47$numtests = $.;
cfa4f241
CS
48seek(TESTS,0,0);
49$. = 0;
378cc40b 50
9d116dd7 51$bang = sprintf "\\%03o", ord "!"; # \41 would not be portable.
b8c5462f
JH
52$ffff = chr(0xff) x 2;
53$nulnul = "\0" x 2;
9d116dd7 54
1462b684 55$| = 1;
c277df42 56print "1..$numtests\n# $iters iterations\n";
cfa4f241 57TEST:
378cc40b 58while (<TESTS>) {
b85d18e9
IZ
59 chomp;
60 s/\\n/\n/g;
ee595aa6 61 ($pat, $subject, $result, $repl, $expect, $reason) = split(/\t/,$_,6);
378cc40b 62 $input = join(':',$pat,$subject,$result,$repl,$expect);
83e898de
DD
63 infty_subst(\$pat);
64 infty_subst(\$expect);
1b1626e4 65 $pat = "'$pat'" unless $pat =~ /^[:']/;
9d116dd7 66 $pat =~ s/(\$\{\w+\})/$1/eeg;
b8c5462f
JH
67 $pat =~ s/\\n/\n/g;
68 $subject =~ s/(\$\{\w+\})/$1/eeg;
c277df42 69 $subject =~ s/\\n/\n/g;
b8c5462f 70 $expect =~ s/(\$\{\w+\})/$1/eeg;
c277df42
IZ
71 $expect =~ s/\\n/\n/g;
72 $expect = $repl = '-' if $skip_amp and $input =~ /\$[&\`\']/;
cf93c79d 73 $skip = ($skip_amp ? ($result =~ s/B//i) : ($result =~ s/B//));
b8c5462f 74 # Certain tests don't work with utf8 (the re_test should be in UTF8)
ee595aa6 75 $skip = 1, $reason = 'utf8'
78109b9e 76 if ($^H &= ~0x00000008) && $pat =~ /\[:\^(alnum|print|word|ascii|xdigit):\]/;
cf93c79d
IZ
77 $result =~ s/B//i unless $skip;
78 for $study ('', 'study \$subject') {
c277df42
IZ
79 $c = $iters;
80 eval "$study; \$match = (\$subject =~ m$pat) while \$c--; \$got = \"$repl\";";
81 chomp( $err = $@ );
cfa4f241 82 if ($result eq 'c') {
c277df42 83 if ($err !~ m!^\Q$expect!) { print "not ok $. (compile) $input => `$err'\n"; next TEST }
cfa4f241
CS
84 last; # no need to study a syntax error
85 }
cf93c79d 86 elsif ( $skip ) {
ee595aa6
LC
87 print "ok $. # skipped", length($reason) ? " $reason" : '', "\n";
88 next TEST;
cf93c79d 89 }
c277df42
IZ
90 elsif ($@) {
91 print "not ok $. $input => error `$err'\n"; next TEST;
92 }
cfa4f241 93 elsif ($result eq 'n') {
c277df42 94 if ($match) { print "not ok $. ($study) $input => false positive\n"; next TEST }
378cc40b
LW
95 }
96 else {
cfa4f241 97 if (!$match || $got ne $expect) {
c277df42 98 print "not ok $. ($study) $input => `$got', match=$match\n";
cfa4f241
CS
99 next TEST;
100 }
378cc40b
LW
101 }
102 }
cfa4f241 103 print "ok $.\n";
378cc40b 104}
cfa4f241 105
378cc40b 106close(TESTS);
83e898de
DD
107
108sub infty_subst # Special-case substitution
109{ # of $reg_infty and friends
110 my $tp = shift;
111 $$tp =~ s/,\$reg_infty_m}/,$reg_infty_m}/o;
112 $$tp =~ s/,\$reg_infty_p}/,$reg_infty_p}/o;
113 $$tp =~ s/,\$reg_infty}/,$reg_infty}/o;
114}