Commit | Line | Data |
378cc40b |
1 | #!./perl |
2 | |
eec2d3df |
3 | $ENV{PERL_DESTRUCT_LEVEL} = 0; # XXX known to leaks scalars |
4 | |
ad4f75a6 |
5 | # The tests are in a separate file 't/op/re_tests'. |
6 | # Each line in that file is a separate test. |
7 | # There are five columns, separated by tabs. |
8 | # |
9 | # Column 1 contains the pattern, optionally enclosed in C<''>. |
10 | # Modifiers can be put after the closing C<'>. |
11 | # |
12 | # Column 2 contains the string to be matched. |
13 | # |
14 | # Column 3 contains the expected result: |
15 | # y expect a match |
16 | # n expect no match |
17 | # c expect an error |
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 | # |
b85d18e9 |
26 | # \n in the tests are interpolated. |
83e898de |
27 | # |
8d37f932 |
28 | # If you want to add a regular expression test that can't be expressed |
29 | # in this format, don't add it here: put it in op/pat.t instead. |
c277df42 |
30 | |
e4d48cc9 |
31 | BEGIN { |
32 | chdir 't' if -d 't'; |
33 | @INC = '../lib' if -d '../lib'; |
34 | } |
35 | |
36 | use re 'eval'; |
37 | |
c277df42 |
38 | $iters = shift || 1; # Poor man performance suite, 10000 is OK. |
ad4f75a6 |
39 | |
8d37f932 |
40 | open(TESTS,'op/re_tests') || open(TESTS,'t/op/re_tests') || |
41 | die "Can't open re_tests"; |
cfa4f241 |
42 | |
378cc40b |
43 | while (<TESTS>) { } |
44 | $numtests = $.; |
cfa4f241 |
45 | seek(TESTS,0,0); |
46 | $. = 0; |
378cc40b |
47 | |
1462b684 |
48 | $| = 1; |
c277df42 |
49 | print "1..$numtests\n# $iters iterations\n"; |
cfa4f241 |
50 | TEST: |
378cc40b |
51 | while (<TESTS>) { |
b85d18e9 |
52 | chomp; |
53 | s/\\n/\n/g; |
54 | ($pat, $subject, $result, $repl, $expect) = split(/\t/,$_); |
378cc40b |
55 | $input = join(':',$pat,$subject,$result,$repl,$expect); |
83e898de |
56 | infty_subst(\$pat); |
57 | infty_subst(\$expect); |
1b1626e4 |
58 | $pat = "'$pat'" unless $pat =~ /^[:']/; |
c277df42 |
59 | $pat =~ s/\\n/\n/g; |
60 | $subject =~ s/\\n/\n/g; |
61 | $expect =~ s/\\n/\n/g; |
62 | $expect = $repl = '-' if $skip_amp and $input =~ /\$[&\`\']/; |
ad4f75a6 |
63 | for $study ("", "study \$subject") { |
c277df42 |
64 | $c = $iters; |
65 | eval "$study; \$match = (\$subject =~ m$pat) while \$c--; \$got = \"$repl\";"; |
66 | chomp( $err = $@ ); |
cfa4f241 |
67 | if ($result eq 'c') { |
c277df42 |
68 | if ($err !~ m!^\Q$expect!) { print "not ok $. (compile) $input => `$err'\n"; next TEST } |
cfa4f241 |
69 | last; # no need to study a syntax error |
70 | } |
c277df42 |
71 | elsif ($@) { |
72 | print "not ok $. $input => error `$err'\n"; next TEST; |
73 | } |
cfa4f241 |
74 | elsif ($result eq 'n') { |
c277df42 |
75 | if ($match) { print "not ok $. ($study) $input => false positive\n"; next TEST } |
378cc40b |
76 | } |
77 | else { |
cfa4f241 |
78 | if (!$match || $got ne $expect) { |
c277df42 |
79 | print "not ok $. ($study) $input => `$got', match=$match\n"; |
cfa4f241 |
80 | next TEST; |
81 | } |
378cc40b |
82 | } |
83 | } |
cfa4f241 |
84 | print "ok $.\n"; |
378cc40b |
85 | } |
cfa4f241 |
86 | |
378cc40b |
87 | close(TESTS); |
83e898de |
88 | |
89 | sub infty_subst # Special-case substitution |
90 | { # of $reg_infty and friends |
91 | my $tp = shift; |
92 | $$tp =~ s/,\$reg_infty_m}/,$reg_infty_m}/o; |
93 | $$tp =~ s/,\$reg_infty_p}/,$reg_infty_p}/o; |
94 | $$tp =~ s/,\$reg_infty}/,$reg_infty}/o; |
95 | } |