This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Re: [PATCH] Abstract "utility" information from installman
[perl5.git] / t / op / regmesg.t
CommitLineData
b45f050a
JF
1#!./perl -w
2
3BEGIN {
4 chdir 't' if -d 't';
20822f61 5 @INC = '../lib';
b45f050a
JF
6}
7
8my $debug = 1;
9
10##
11## If the markers used are changed (search for "MARKER1" in regcomp.c),
12## update only these two variables, and leave the {#} in the @death/@warning
13## arrays below. The {#} is a meta-marker -- it marks where the marker should
14## go.
15
79eeca27
JH
16my $marker1 = "HERE";
17my $marker2 = " << HERE ";
b45f050a
JF
18
19##
20## Key-value pairs of code/error of code that should have fatal errors.
21##
69f2e79d
MG
22
23eval 'use Config'; # assume defaults if fail
24our %Config;
25my $inf_m1 = ($Config{reg_infty} || 32767) - 1;
26my $inf_p1 = $inf_m1 + 2;
b45f050a
JF
27my @death =
28(
79eeca27 29 '/[[=foo=]]/' => 'POSIX syntax [= =] is reserved for future extensions before {#} mark in regex m/[[=foo=]{#}]/',
b45f050a 30
79eeca27 31 '/(?<= .*)/' => 'Variable length lookbehind not implemented before {#} mark in regex m/(?<= .*){#}/',
b45f050a 32
79eeca27 33 '/(?<= x{1000})/' => 'Lookbehind longer than 255 not implemented before {#} mark in regex m/(?<= x{1000}){#}/',
b45f050a 34
79eeca27 35 '/(?@)/' => 'Sequence (?@...) not implemented before {#} mark in regex m/(?@{#})/',
b45f050a 36
79eeca27 37 '/(?{ 1/' => 'Sequence (?{...}) not terminated or not {}-balanced before {#} mark in regex m/(?{{#} 1/',
b45f050a 38
79eeca27 39 '/(?(1x))/' => 'Switch condition not recognized before {#} mark in regex m/(?(1x{#}))/',
b45f050a 40
79eeca27 41 '/(?(1)x|y|z)/' => 'Switch (?(condition)... contains too many branches before {#} mark in regex m/(?(1)x|y|{#}z)/',
b45f050a 42
79eeca27 43 '/(?(x)y|x)/' => 'Unknown switch condition (?(x) before {#} mark in regex m/(?({#}x)y|x)/',
b45f050a 44
79eeca27 45 '/(?/' => 'Sequence (? incomplete before {#} mark in regex m/(?{#}/',
b45f050a 46
79eeca27
JH
47 '/(?;x/' => 'Sequence (?;...) not recognized before {#} mark in regex m/(?;{#}x/',
48 '/(?<;x/' => 'Sequence (?<;...) not recognized before {#} mark in regex m/(?<;{#}x/',
b45f050a 49
79eeca27 50 '/((x)/' => 'Unmatched ( before {#} mark in regex m/({#}(x)/',
b45f050a 51
79eeca27 52 "/x{$inf_p1}/" => "Quantifier in {,} bigger than $inf_m1 before {#} mark in regex m/x{{#}$inf_p1}/",
b45f050a 53
79eeca27 54 '/x{3,1}/' => 'Can\'t do {n,m} with n > m before {#} mark in regex m/x{3,1}{#}/',
b45f050a 55
79eeca27 56 '/x**/' => 'Nested quantifiers before {#} mark in regex m/x**{#}/',
b45f050a 57
79eeca27 58 '/x[/' => 'Unmatched [ before {#} mark in regex m/x[{#}/',
b45f050a 59
79eeca27 60 '/*/', => 'Quantifier follows nothing before {#} mark in regex m/*{#}/',
b45f050a 61
79eeca27 62 '/\p{x/' => 'Missing right brace on \p{} before {#} mark in regex m/\p{{#}x/',
b45f050a 63
79eeca27 64 'use utf8; /[\p{x]/' => 'Missing right brace on \p{} before {#} mark in regex m/[\p{{#}x]/',
b45f050a 65
79eeca27 66 '/(x)\2/' => 'Reference to nonexistent group before {#} mark in regex m/(x)\2{#}/',
b45f050a 67
40809656 68 'my $m = "\\\"; $m =~ $m', => 'Trailing \ in regex m/\/',
b45f050a 69
79eeca27 70 '/\x{1/' => 'Missing right brace on \x{} before {#} mark in regex m/\x{{#}1/',
b45f050a 71
79eeca27 72 'use utf8; /[\x{X]/' => 'Missing right brace on \x{} before {#} mark in regex m/[\x{{#}X]/',
b45f050a 73
79eeca27 74 '/[[:barf:]]/' => 'POSIX class [:barf:] unknown before {#} mark in regex m/[[:barf:]{#}]/',
b45f050a 75
79eeca27 76 '/[[=barf=]]/' => 'POSIX syntax [= =] is reserved for future extensions before {#} mark in regex m/[[=barf=]{#}]/',
b45f050a 77
79eeca27 78 '/[[.barf.]]/' => 'POSIX syntax [. .] is reserved for future extensions before {#} mark in regex m/[[.barf.]{#}]/',
b45f050a 79
79eeca27 80 '/[z-a]/' => 'Invalid [] range "z-a" before {#} mark in regex m/[z-a{#}]/',
b45f050a
JF
81);
82
83##
84## Key-value pairs of code/error of code that should have non-fatal warnings.
85##
86@warning = (
79eeca27 87 "m/(?p{ 'a' })/" => "(?p{}) is deprecated - use (??{}) before {#} mark in regex m/(?p{#}{ 'a' })/",
b45f050a 88
79eeca27 89 'm/\b*/' => '\b* matches null string many times before {#} mark in regex m/\b*{#}/',
b45f050a 90
79eeca27 91 'm/[:blank:]/' => 'POSIX syntax [: :] belongs inside character classes before {#} mark in regex m/[:blank:]{#}/',
b45f050a 92
79eeca27 93 "m'[\\y]'" => 'Unrecognized escape \y in character class passed through before {#} mark in regex m/[\y{#}]/',
b45f050a 94
79eeca27
JH
95 'm/[a-\d]/' => 'False [] range "a-\d" before {#} mark in regex m/[a-\d{#}]/',
96 'm/[\w-x]/' => 'False [] range "\w-" before {#} mark in regex m/[\w-{#}x]/',
97 "m'\\y'" => 'Unrecognized escape \y passed through before {#} mark in regex m/\y{#}/',
b45f050a
JF
98);
99
100my $total = (@death + @warning)/2;
101
40809656
PP
102# utf8 is a noop on EBCDIC platforms, it is not fatal
103my $Is_EBCDIC = (ord('A') == 193);
104if ($Is_EBCDIC) {
105 my @utf8_death = grep(/utf8/, @death);
7387eed8 106 $total = $total - @utf8_death;
40809656
PP
107}
108
b45f050a
JF
109print "1..$total\n";
110
111my $count = 0;
112
113while (@death)
114{
b45f050a
JF
115 my $regex = shift @death;
116 my $result = shift @death;
40809656
PP
117 # skip the utf8 test on EBCDIC since they do not die
118 next if ($Is_EBCDIC && $regex =~ /utf8/);
119 $count++;
b45f050a 120
b45f050a
JF
121 $_ = "x";
122 eval $regex;
123 if (not $@) {
69f2e79d 124 print "# oops, $regex didn't die\nnot ok $count\n";
b45f050a
JF
125 next;
126 }
127 chomp $@;
b45f050a
JF
128 $result =~ s/{\#}/$marker1/;
129 $result =~ s/{\#}/$marker2/;
69f2e79d
MG
130 if ($@ !~ /^\Q$result/) {
131 print "# For $regex, expected:\n# $result\n# Got:\n# $@\n#\nnot ";
b45f050a
JF
132 }
133 print "ok $count\n";
134}
135
136
137our $warning;
138$SIG{__WARN__} = sub { $warning = shift };
139
140while (@warning)
141{
142 $count++;
143 my $regex = shift @warning;
144 my $result = shift @warning;
145
146 undef $warning;
147 $_ = "x";
148 eval $regex;
149
150 if ($@)
151 {
69f2e79d 152 print "# oops, $regex died with:\n#\t$@#\nnot ok $count\n";
b45f050a
JF
153 next;
154 }
155
156 if (not $warning)
157 {
69f2e79d 158 print "# oops, $regex didn't generate a warning\nnot ok $count\n";
b45f050a
JF
159 next;
160 }
b45f050a
JF
161 $result =~ s/{\#}/$marker1/;
162 $result =~ s/{\#}/$marker2/;
69f2e79d 163 if ($warning !~ /^\Q$result/)
b45f050a 164 {
69f2e79d
MG
165 print <<"EOM";
166# For $regex, expected:
167# $result
168# Got:
169# $warning
170#
171not ok $count
172EOM
b45f050a
JF
173 next;
174 }
175 print "ok $count\n";
176}
177
178
179