a4783bac2fa1a46748a57fdc975186c11d093f3e
[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 # Columns 1, 2 and 5 are \n-interpolated.
25 #
26 # The variables $reg_infty, $reg_infty_m and $reg_infty_m in columns 1
27 # and 5 are replaced respectively with the configuration value reg_infty,
28 # reg_infty-1 and reg_infty+1, or if reg_infty is not defined in the
29 # configuration, default values.  No other variables are substituted.
30
31
32 $iters = shift || 1;            # Poor man performance suite, 10000 is OK.
33
34 chdir 't' if -d 't';
35 @INC = "../lib";
36 eval 'use Config';          #  Defaults assumed if this fails
37 $reg_infty = defined $Config{reg_infty} ? $Config{reg_infty} : 32767;
38 $reg_infty_m = $reg_infty - 1; $reg_infty_p = $reg_infty + 1;
39
40 open(TESTS,'op/re_tests') || die "Can't open re_tests";
41
42 while (<TESTS>) { }
43 $numtests = $.;
44 seek(TESTS,0,0);
45 $. = 0;
46
47 $| = 1;
48 print "1..$numtests\n# $iters iterations\n";
49 TEST:
50 while (<TESTS>) {
51     ($pat, $subject, $result, $repl, $expect) = split(/[\t\n]/,$_);
52     $input = join(':',$pat,$subject,$result,$repl,$expect);
53     infty_subst(\$pat);
54     infty_subst(\$expect);
55     $pat = "'$pat'" unless $pat =~ /^[:']/;
56     $pat =~ s/\\n/\n/g;
57     $subject =~ s/\\n/\n/g;
58     $expect =~ s/\\n/\n/g;
59     $expect = $repl = '-' if $skip_amp and $input =~ /\$[&\`\']/;
60     for $study ("", "study \$subject") {
61         $c = $iters;
62         eval "$study; \$match = (\$subject =~ m$pat) while \$c--; \$got = \"$repl\";";
63         chomp( $err = $@ );
64         if ($result eq 'c') {
65             if ($err !~ m!^\Q$expect!) { print "not ok $. (compile) $input => `$err'\n"; next TEST }
66             last;  # no need to study a syntax error
67         }
68         elsif ($@) {
69             print "not ok $. $input => error `$err'\n"; next TEST;
70         }
71         elsif ($result eq 'n') {
72             if ($match) { print "not ok $. ($study) $input => false positive\n"; next TEST }
73         }
74         else {
75             if (!$match || $got ne $expect) {
76                 print "not ok $. ($study) $input => `$got', match=$match\n";
77                 next TEST;
78             }
79         }
80     }
81     print "ok $.\n";
82 }
83
84 close(TESTS);
85
86 sub infty_subst                             # Special-case substitution
87 {                                           #  of $reg_infty and friends
88     my $tp = shift;
89     $$tp =~ s/,\$reg_infty_m}/,$reg_infty_m}/o;
90     $$tp =~ s/,\$reg_infty_p}/,$reg_infty_p}/o;
91     $$tp =~ s/,\$reg_infty}/,$reg_infty}/o;
92 }