244ed4ab99460267a0b8ebd03a35125d24de6cbd
[p5sagit/p5-mst-13.2.git] / t / op / regexp.t
1 #!./perl
2
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 #
17 # Columns 4 and 5 are used only if column 3 contains C<y> or C<c>.
18 #
19 # Column 4 contains a string, usually C<$&>.
20 #
21 # Column 5 contains the expected result of double-quote
22 # interpolating that string after the match, or start of error message.
23 #
24 # \n in the tests are interpolated.
25 #
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.
28
29 BEGIN {
30     chdir 't' if -d 't';
31     @INC = '../lib' if -d '../lib';
32 }
33
34 use re 'eval';
35
36 $iters = shift || 1;            # Poor man performance suite, 10000 is OK.
37
38 open(TESTS,'op/re_tests') || open(TESTS,'t/op/re_tests') ||
39         die "Can't open re_tests";
40
41 while (<TESTS>) { }
42 $numtests = $.;
43 seek(TESTS,0,0);
44 $. = 0;
45
46 $| = 1;
47 print "1..$numtests\n# $iters iterations\n";
48 TEST:
49 while (<TESTS>) {
50     chomp;
51     s/\\n/\n/g;
52     ($pat, $subject, $result, $repl, $expect) = split(/\t/,$_);
53     $input = join(':',$pat,$subject,$result,$repl,$expect);
54     infty_subst(\$pat);
55     infty_subst(\$expect);
56     $pat = "'$pat'" unless $pat =~ /^[:']/;
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 =~ /\$[&\`\']/;
61     for $study ("", "study \$subject") {
62         $c = $iters;
63         eval "$study; \$match = (\$subject =~ m$pat) while \$c--; \$got = \"$repl\";";
64         chomp( $err = $@ );
65         if ($result eq 'c') {
66             if ($err !~ m!^\Q$expect!) { print "not ok $. (compile) $input => `$err'\n"; next TEST }
67             last;  # no need to study a syntax error
68         }
69         elsif ($@) {
70             print "not ok $. $input => error `$err'\n"; next TEST;
71         }
72         elsif ($result eq 'n') {
73             if ($match) { print "not ok $. ($study) $input => false positive\n"; next TEST }
74         }
75         else {
76             if (!$match || $got ne $expect) {
77                 print "not ok $. ($study) $input => `$got', match=$match\n";
78                 next TEST;
79             }
80         }
81     }
82     print "ok $.\n";
83 }
84
85 close(TESTS);
86
87 sub 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 }