doc tweaks suggested by Abigail, M.J.T. Guy, and Larry Wall
[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
16#
1b1626e4 17# Columns 4 and 5 are used only if column 3 contains C<y> or C<c>.
ad4f75a6 18#
19# Column 4 contains a string, usually C<$&>.
20#
21# Column 5 contains the expected result of double-quote
c277df42 22# interpolating that string after the match, or start of error message.
23#
b85d18e9 24# \n in the tests are interpolated.
83e898de 25#
8d37f932 26# If you want to add a regular expression test that can't be expressed
27# in this format, don't add it here: put it in op/pat.t instead.
c277df42 28
e4d48cc9 29BEGIN {
30 chdir 't' if -d 't';
31 @INC = '../lib' if -d '../lib';
32}
33
34use re 'eval';
35
c277df42 36$iters = shift || 1; # Poor man performance suite, 10000 is OK.
ad4f75a6 37
8d37f932 38open(TESTS,'op/re_tests') || open(TESTS,'t/op/re_tests') ||
39 die "Can't open re_tests";
cfa4f241 40
378cc40b 41while (<TESTS>) { }
42$numtests = $.;
cfa4f241 43seek(TESTS,0,0);
44$. = 0;
378cc40b 45
1462b684 46$| = 1;
c277df42 47print "1..$numtests\n# $iters iterations\n";
cfa4f241 48TEST:
378cc40b 49while (<TESTS>) {
b85d18e9 50 chomp;
51 s/\\n/\n/g;
52 ($pat, $subject, $result, $repl, $expect) = split(/\t/,$_);
378cc40b 53 $input = join(':',$pat,$subject,$result,$repl,$expect);
83e898de 54 infty_subst(\$pat);
55 infty_subst(\$expect);
1b1626e4 56 $pat = "'$pat'" unless $pat =~ /^[:']/;
c277df42 57 $pat =~ s/\\n/\n/g;
58 $subject =~ s/\\n/\n/g;
59 $expect =~ s/\\n/\n/g;
60 $expect = $repl = '-' if $skip_amp and $input =~ /\$[&\`\']/;
ad4f75a6 61 for $study ("", "study \$subject") {
c277df42 62 $c = $iters;
63 eval "$study; \$match = (\$subject =~ m$pat) while \$c--; \$got = \"$repl\";";
64 chomp( $err = $@ );
cfa4f241 65 if ($result eq 'c') {
c277df42 66 if ($err !~ m!^\Q$expect!) { print "not ok $. (compile) $input => `$err'\n"; next TEST }
cfa4f241 67 last; # no need to study a syntax error
68 }
c277df42 69 elsif ($@) {
70 print "not ok $. $input => error `$err'\n"; next TEST;
71 }
cfa4f241 72 elsif ($result eq 'n') {
c277df42 73 if ($match) { print "not ok $. ($study) $input => false positive\n"; next TEST }
378cc40b 74 }
75 else {
cfa4f241 76 if (!$match || $got ne $expect) {
c277df42 77 print "not ok $. ($study) $input => `$got', match=$match\n";
cfa4f241 78 next TEST;
79 }
378cc40b 80 }
81 }
cfa4f241 82 print "ok $.\n";
378cc40b 83}
cfa4f241 84
378cc40b 85close(TESTS);
83e898de 86
87sub infty_subst # Special-case substitution
88{ # of $reg_infty and friends
89 my $tp = shift;
90 $$tp =~ s/,\$reg_infty_m}/,$reg_infty_m}/o;
91 $$tp =~ s/,\$reg_infty_p}/,$reg_infty_p}/o;
92 $$tp =~ s/,\$reg_infty}/,$reg_infty}/o;
93}