This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
reg_mesg.t: Turn off regexp warns along with deprecated
[perl5.git] / t / re / reg_mesg.t
CommitLineData
b45f050a
JF
1#!./perl -w
2
3BEGIN {
4 chdir 't' if -d 't';
20822f61 5 @INC = '../lib';
cb79f740
NC
6 require './test.pl';
7 eval 'require Config'; # assume defaults if this fails
b45f050a
JF
8}
9
cb79f740 10use strict;
b45f050a
JF
11
12##
13## If the markers used are changed (search for "MARKER1" in regcomp.c),
cb79f740 14## update only these two regexs, and leave the {#} in the @death/@warning
b45f050a
JF
15## arrays below. The {#} is a meta-marker -- it marks where the marker should
16## go.
cb79f740 17##
af01601c
KW
18## Returns empty string if that is what is expected. Otherwise, handles
19## either a scalar, turning it into a single element array; or a ref to an
20## array, adjusting each element. If called in array context, returns an
21## array, otherwise the join of all elements
22
cb79f740 23sub fixup_expect {
af01601c
KW
24 my $expect_ref = shift;
25 return if $expect_ref eq "";
26
27 my @expect;
28 if (ref $expect_ref) {
29 @expect = @$expect_ref;
30 }
31 else {
32 @expect = $expect_ref;
33 }
34
35 foreach my $element (@expect) {
6d24e9d4 36 $element =~ s/{\#}/in regex; marked by <-- HERE in/;
af01601c
KW
37 $element =~ s/{\#}/ <-- HERE /;
38 $element .= " at ";
39 }
40 return wantarray ? @expect : join "", @expect;
cb79f740 41}
b45f050a 42
cb79f740
NC
43my $inf_m1 = ($Config::Config{reg_infty} || 32767) - 1;
44my $inf_p1 = $inf_m1 + 2;
b45f050a
JF
45
46##
47## Key-value pairs of code/error of code that should have fatal errors.
48##
49my @death =
50(
6d24e9d4 51 '/[[=foo=]]/' => 'POSIX syntax [= =] is reserved for future extensions {#} m/[[=foo=]{#}]/',
b45f050a 52
58e23c8d 53 '/(?<= .*)/' => 'Variable length lookbehind not implemented in regex m/(?<= .*)/',
b45f050a 54
58e23c8d 55 '/(?<= x{1000})/' => 'Lookbehind longer than 255 not implemented in regex m/(?<= x{1000})/',
b45f050a 56
6d24e9d4 57 '/(?@)/' => 'Sequence (?@...) not implemented {#} m/(?@{#})/',
b45f050a 58
9da1dd8f 59 '/(?{ 1/' => 'Missing right curly or square bracket',
b45f050a 60
6d24e9d4 61 '/(?(1x))/' => 'Switch condition not recognized {#} m/(?(1x{#}))/',
b45f050a 62
6d24e9d4 63 '/(?(1)x|y|z)/' => 'Switch (?(condition)... contains too many branches {#} m/(?(1)x|y|{#}z)/',
b45f050a 64
6d24e9d4 65 '/(?(x)y|x)/' => 'Unknown switch condition (?(x) {#} m/(?({#}x)y|x)/',
b45f050a 66
6d24e9d4 67 '/(?/' => 'Sequence (? incomplete {#} m/(?{#}/',
b45f050a 68
6d24e9d4
KW
69 '/(?;x/' => 'Sequence (?;...) not recognized {#} m/(?;{#}x/',
70 '/(?<;x/' => 'Group name must start with a non-digit word character {#} m/(?<;{#}x/',
71 '/(?\ix/' => 'Sequence (?\...) not recognized {#} m/(?\{#}ix/',
72 '/(?\mx/' => 'Sequence (?\...) not recognized {#} m/(?\{#}mx/',
73 '/(?\:x/' => 'Sequence (?\...) not recognized {#} m/(?\{#}:x/',
74 '/(?\=x/' => 'Sequence (?\...) not recognized {#} m/(?\{#}=x/',
75 '/(?\!x/' => 'Sequence (?\...) not recognized {#} m/(?\{#}!x/',
76 '/(?\<=x/' => 'Sequence (?\...) not recognized {#} m/(?\{#}<=x/',
77 '/(?\<!x/' => 'Sequence (?\...) not recognized {#} m/(?\{#}<!x/',
78 '/(?\>x/' => 'Sequence (?\...) not recognized {#} m/(?\{#}>x/',
79 '/(?^-i:foo)/' => 'Sequence (?^-...) not recognized {#} m/(?^-{#}i:foo)/',
80 '/(?^-i)foo/' => 'Sequence (?^-...) not recognized {#} m/(?^-{#}i)foo/',
81 '/(?^d:foo)/' => 'Sequence (?^d...) not recognized {#} m/(?^d{#}:foo)/',
82 '/(?^d)foo/' => 'Sequence (?^d...) not recognized {#} m/(?^d{#})foo/',
83 '/(?^lu:foo)/' => 'Regexp modifiers "l" and "u" are mutually exclusive {#} m/(?^lu{#}:foo)/',
84 '/(?^lu)foo/' => 'Regexp modifiers "l" and "u" are mutually exclusive {#} m/(?^lu{#})foo/',
85'/(?da:foo)/' => 'Regexp modifiers "d" and "a" are mutually exclusive {#} m/(?da{#}:foo)/',
86'/(?lil:foo)/' => 'Regexp modifier "l" may not appear twice {#} m/(?lil{#}:foo)/',
87'/(?aaia:foo)/' => 'Regexp modifier "a" may appear a maximum of twice {#} m/(?aaia{#}:foo)/',
88'/(?i-l:foo)/' => 'Regexp modifier "l" may not appear after the "-" {#} m/(?i-l{#}:foo)/',
cc74c5bd 89
6d24e9d4 90 '/((x)/' => 'Unmatched ( {#} m/({#}(x)/',
b45f050a 91
6d24e9d4 92 "/x{$inf_p1}/" => "Quantifier in {,} bigger than $inf_m1 {#} m/x{{#}$inf_p1}/",
b45f050a 93
b45f050a 94
6d24e9d4 95 '/x**/' => 'Nested quantifiers {#} m/x**{#}/',
b45f050a 96
6d24e9d4 97 '/x[/' => 'Unmatched [ {#} m/x[{#}/',
b45f050a 98
6d24e9d4 99 '/*/', => 'Quantifier follows nothing {#} m/*{#}/',
b45f050a 100
6d24e9d4 101 '/\p{x/' => 'Missing right brace on \p{} {#} m/\p{{#}x/',
b45f050a 102
6d24e9d4 103 '/[\p{x]/' => 'Missing right brace on \p{} {#} m/[\p{{#}x]/',
b45f050a 104
6d24e9d4 105 '/(x)\2/' => 'Reference to nonexistent group {#} m/(x)\2{#}/',
b45f050a 106
40809656 107 'my $m = "\\\"; $m =~ $m', => 'Trailing \ in regex m/\/',
b45f050a 108
6d24e9d4
KW
109 '/\x{1/' => 'Missing right brace on \x{} {#} m/\x{1{#}/',
110 '/\x{X/' => 'Missing right brace on \x{} {#} m/\x{{#}X/',
111
112 '/[\x{X]/' => 'Missing right brace on \x{} {#} m/[\x{{#}X]/',
113 '/[\x{A]/' => 'Missing right brace on \x{} {#} m/[\x{A{#}]/',
114
115 '/\o{1/' => 'Missing right brace on \o{ {#} m/\o{1{#}/',
116 '/\o{X/' => 'Missing right brace on \o{ {#} m/\o{{#}X/',
117
118 '/[\o{X]/' => 'Missing right brace on \o{ {#} m/[\o{{#}X]/',
119 '/[\o{7]/' => 'Missing right brace on \o{ {#} m/[\o{7{#}]/',
120
121 '/[[:barf:]]/' => 'POSIX class [:barf:] unknown {#} m/[[:barf:]{#}]/',
122
123 '/[[=barf=]]/' => 'POSIX syntax [= =] is reserved for future extensions {#} m/[[=barf=]{#}]/',
124
125 '/[[.barf.]]/' => 'POSIX syntax [. .] is reserved for future extensions {#} m/[[.barf.]{#}]/',
126
127 '/[z-a]/' => 'Invalid [] range "z-a" {#} m/[z-a{#}]/',
128
129 '/\p/' => 'Empty \p{} {#} m/\p{#}/',
130
131 '/\P{}/' => 'Empty \P{} {#} m/\P{{#}}/',
132 '/(?[[[:word]]])/' => "Unmatched ':' in POSIX class {#} m/(?[[[:word{#}]]])/",
133 '/(?[[:word]])/' => "Unmatched ':' in POSIX class {#} m/(?[[:word{#}]])/",
134 '/(?[[[:digit: ])/' => "Unmatched '[' in POSIX class {#} m/(?[[[:digit:{#} ])/",
135 '/(?[[:digit: ])/' => "Unmatched '[' in POSIX class {#} m/(?[[:digit:{#} ])/",
136 '/(?[[[::]]])/' => "POSIX class [::] unknown {#} m/(?[[[::]{#}]])/",
137 '/(?[[[:w:]]])/' => "POSIX class [:w:] unknown {#} m/(?[[[:w:]{#}]])/",
138 '/(?[[:w:]])/' => "POSIX class [:w:] unknown {#} m/(?[[:w:]{#}])/",
139 '/(?[a])/' => 'Unexpected character {#} m/(?[a{#}])/',
140 '/(?[\t])/l' => '(?[...]) not valid in locale {#} m/(?[{#}\t])/',
141 '/(?[ + \t ])/' => 'Unexpected binary operator \'+\' with no preceding operand {#} m/(?[ +{#} \t ])/',
142 '/(?[ \cK - ( + \t ) ])/' => 'Unexpected binary operator \'+\' with no preceding operand {#} m/(?[ \cK - ( +{#} \t ) ])/',
143 '/(?[ \cK ( \t ) ])/' => 'Unexpected \'(\' with no preceding operator {#} m/(?[ \cK ({#} \t ) ])/',
144 '/(?[ \cK \t ])/' => 'Operand with no preceding operator {#} m/(?[ \cK \t{#} ])/',
145 '/(?[ \0004 ])/' => 'Need exactly 3 octal digits {#} m/(?[ \0004 {#}])/',
146 '/(?[ \05 ])/' => 'Need exactly 3 octal digits {#} m/(?[ \05 {#}])/',
147 '/(?[ \o{1038} ])/' => 'Non-octal character {#} m/(?[ \o{1038{#}} ])/',
148 '/(?[ \o{} ])/' => 'Number with no digits {#} m/(?[ \o{}{#} ])/',
149 '/(?[ \x{defg} ])/' => 'Non-hex character {#} m/(?[ \x{defg{#}} ])/',
150 '/(?[ \xabcdef ])/' => 'Use \\x{...} for more than two hex characters {#} m/(?[ \xabc{#}def ])/',
151 '/(?[ \x{} ])/' => 'Number with no digits {#} m/(?[ \x{}{#} ])/',
152 '/(?[ \cK + ) ])/' => 'Unexpected \')\' {#} m/(?[ \cK + ){#} ])/',
153 '/(?[ \cK + ])/' => 'Incomplete expression within \'(?[ ])\' {#} m/(?[ \cK + {#}])/',
154 '/(?[ \p{foo} ])/' => 'Property \'foo\' is unknown {#} m/(?[ \p{foo}{#} ])/',
155 '/(?[ \p{ foo = bar } ])/' => 'Property \'foo = bar\' is unknown {#} m/(?[ \p{ foo = bar }{#} ])/',
156 '/(?[ \8 ])/' => 'Unrecognized escape \8 in character class {#} m/(?[ \8{#} ])/',
3f4fde43
KW
157 '/(?[ \t ]/' => 'Syntax error in (?[...]) in regex m/(?[ \t ]/',
158 '/(?[ [ \t ]/' => 'Syntax error in (?[...]) in regex m/(?[ [ \t ]/',
159 '/(?[ \t ] ]/' => 'Syntax error in (?[...]) in regex m/(?[ \t ] ]/',
160 '/(?[ [ ] ]/' => 'Syntax error in (?[...]) in regex m/(?[ [ ] ]/',
161 '/(?[ \t + \e # This was supposed to be a comment ])/' => 'Syntax error in (?[...]) in regex m/(?[ \t + \e # This was supposed to be a comment ])/',
6d24e9d4
KW
162 '/(?[ ])/' => 'Incomplete expression within \'(?[ ])\' {#} m/(?[ {#}])/',
163 'm/(?[[a-\d]])/' => 'False [] range "a-\d" {#} m/(?[[a-\d{#}]])/',
164 'm/(?[[\w-x]])/' => 'False [] range "\w-" {#} m/(?[[\w-{#}x]])/',
165 'm/(?[[a-\pM]])/' => 'False [] range "a-\pM" {#} m/(?[[a-\pM{#}]])/',
166 'm/(?[[\pM-x]])/' => 'False [] range "\pM-" {#} m/(?[[\pM-{#}x]])/',
167 'm/(?[[\N{LATIN CAPITAL LETTER A WITH MACRON AND GRAVE}]])/' => '\N{} in character class restricted to one character {#} m/(?[[\N{U+100.300{#}}]])/',
b5864679
KW
168 'm/(?[ \p{Digit} & (?(?[ \p{Thai} | \p{Lao} ]))])/' => 'Sequence (?(...) not recognized {#} m/(?[ \p{Digit} & (?({#}?[ \p{Thai} | \p{Lao} ]))])/',
169 'm/(?[ \p{Digit} & (?:(?[ \p{Thai} | \p{Lao} ]))])/' => 'Expecting \'(?flags:(?[...\' {#} m/(?[ \p{Digit} & (?{#}:(?[ \p{Thai} | \p{Lao} ]))])/',
6d24e9d4
KW
170 'm/\o{/' => 'Missing right brace on \o{ {#} m/\o{{#}/',
171 'm/\o/' => 'Missing braces on \o{} {#} m/\o{#}/',
172 'm/\o{}/' => 'Number with no digits {#} m/\o{}{#}/',
173 'm/[\o{]/' => 'Missing right brace on \o{ {#} m/[\o{{#}]/',
174 'm/[\o]/' => 'Missing braces on \o{} {#} m/[\o{#}]/',
175 'm/[\o{}]/' => 'Number with no digits {#} m/[\o{}{#}]/',
176 'm/(?^-i:foo)/' => 'Sequence (?^-...) not recognized {#} m/(?^-{#}i:foo)/',
b45f050a 177);
902994e4 178# Tests involving a user-defined charnames translator are in pat_advanced.t
b45f050a 179
af01601c
KW
180# In the following arrays of warnings, the value can be an array of things to
181# expect. If the empty string, it means no warning should be raised.
182
b45f050a 183##
6fe2934b 184## Key-value pairs of code/error of code that should have non-fatal regexp warnings.
b45f050a 185##
cb79f740 186my @warning = (
6d24e9d4
KW
187 'm/\b*/' => '\b* matches null string many times {#} m/\b*{#}/',
188
189 'm/[:blank:]/' => 'POSIX syntax [: :] belongs inside character classes {#} m/[:blank:]{#}/',
190
191 "m'[\\y]'" => 'Unrecognized escape \y in character class passed through {#} m/[\y{#}]/',
192
193 'm/[a-\d]/' => 'False [] range "a-\d" {#} m/[a-\d{#}]/',
194 'm/[\w-x]/' => 'False [] range "\w-" {#} m/[\w-{#}x]/',
195 'm/[a-\pM]/' => 'False [] range "a-\pM" {#} m/[a-\pM{#}]/',
196 'm/[\pM-x]/' => 'False [] range "\pM-" {#} m/[\pM-{#}x]/',
197 "m'\\y'" => 'Unrecognized escape \y passed through {#} m/\y{#}/',
198 '/x{3,1}/' => 'Quantifier {n,m} with n > m can\'t match {#} m/x{3,1}{#}/',
199 '/\08/' => '\'\08\' resolved to \'\o{0}8\' {#} m/\08{#}/',
200 '/\018/' => '\'\018\' resolved to \'\o{1}8\' {#} m/\018{#}/',
201 '/[\08]/' => '\'\08\' resolved to \'\o{0}8\' {#} m/[\08{#}]/',
202 '/[\018]/' => '\'\018\' resolved to \'\o{1}8\' {#} m/[\018{#}]/',
203 '/\87/' => 'Unrecognized escape \8 passed through {#} m/\8{#}7/',
204 '/a\87/' => 'Unrecognized escape \8 passed through {#} m/a\8{#}7/',
205 '/a\97/' => 'Unrecognized escape \9 passed through {#} m/a\9{#}7/',
206 '/(?=a)*/' => '(?=a)* matches null string many times {#} m/(?=a)*{#}/',
207 'my $x = \'\m\'; qr/a$x/' => 'Unrecognized escape \m passed through {#} m/a\m{#}/',
208 '/\q/' => 'Unrecognized escape \q passed through {#} m/\q{#}/',
209 '/\q{/' => 'Unrecognized escape \q{ passed through {#} m/\q{{#}/',
210 '/(?=a){1,3}/' => 'Quantifier unexpected on zero-length expression {#} m/(?=a){1,3}{#}/',
63fbd1cb
KW
211 '/\_/' => "",
212 '/[\_\0]/' => "",
213 '/[\07]/' => "",
214 '/[\006]/' => "",
215 '/[\0005]/' => "",
6d24e9d4
KW
216 '/[\8\9]/' => ['Unrecognized escape \8 in character class passed through {#} m/[\8{#}\9]/',
217 'Unrecognized escape \9 in character class passed through {#} m/[\8\9{#}]/',
63fbd1cb 218 ],
6d24e9d4
KW
219 '/[:alpha:]/' => 'POSIX syntax [: :] belongs inside character classes {#} m/[:alpha:]{#}/',
220 '/[:zog:]/' => 'POSIX syntax [: :] belongs inside character classes {#} m/[:zog:]{#}/',
221 '/[.zog.]/' => 'POSIX syntax [. .] belongs inside character classes {#} m/[.zog.]{#}/',
63fbd1cb 222 '/[a-b]/' => "",
6d24e9d4
KW
223 '/[a-\d]/' => 'False [] range "a-\d" {#} m/[a-\d{#}]/',
224 '/[\d-b]/' => 'False [] range "\d-" {#} m/[\d-{#}b]/',
225 '/[\s-\d]/' => 'False [] range "\s-" {#} m/[\s-{#}\d]/',
226 '/[\d-\s]/' => 'False [] range "\d-" {#} m/[\d-{#}\s]/',
227 '/[a-[:digit:]]/' => 'False [] range "a-[:digit:]" {#} m/[a-[:digit:]{#}]/',
228 '/[[:digit:]-b]/' => 'False [] range "[:digit:]-" {#} m/[[:digit:]-{#}b]/',
229 '/[[:alpha:]-[:digit:]]/' => 'False [] range "[:alpha:]-" {#} m/[[:alpha:]-{#}[:digit:]]/',
230 '/[[:digit:]-[:alpha:]]/' => 'False [] range "[:digit:]-" {#} m/[[:digit:]-{#}[:alpha:]]/',
231 '/[a\zb]/' => 'Unrecognized escape \z in character class passed through {#} m/[a\z{#}b]/',
232 '/(?c)/' => 'Useless (?c) - use /gc modifier {#} m/(?c{#})/',
233 '/(?-c)/' => 'Useless (?-c) - don\'t use /gc modifier {#} m/(?-c{#})/',
234 '/(?g)/' => 'Useless (?g) - use /g modifier {#} m/(?g{#})/',
235 '/(?-g)/' => 'Useless (?-g) - don\'t use /g modifier {#} m/(?-g{#})/',
236 '/(?o)/' => 'Useless (?o) - use /o modifier {#} m/(?o{#})/',
237 '/(?-o)/' => 'Useless (?-o) - don\'t use /o modifier {#} m/(?-o{#})/',
238 '/(?g-o)/' => [ 'Useless (?g) - use /g modifier {#} m/(?g{#}-o)/',
239 'Useless (?-o) - don\'t use /o modifier {#} m/(?g-o{#})/',
63fbd1cb 240 ],
6d24e9d4
KW
241 '/(?g-c)/' => [ 'Useless (?g) - use /g modifier {#} m/(?g{#}-c)/',
242 'Useless (?-c) - don\'t use /gc modifier {#} m/(?g-c{#})/',
63fbd1cb
KW
243 ],
244 # (?c) means (?g) error won't be thrown
6d24e9d4
KW
245 '/(?o-cg)/' => [ 'Useless (?o) - use /o modifier {#} m/(?o{#}-cg)/',
246 'Useless (?-c) - don\'t use /gc modifier {#} m/(?o-c{#}g)/',
63fbd1cb 247 ],
6d24e9d4
KW
248 '/(?ogc)/' => [ 'Useless (?o) - use /o modifier {#} m/(?o{#}gc)/',
249 'Useless (?g) - use /g modifier {#} m/(?og{#}c)/',
250 'Useless (?c) - use /gc modifier {#} m/(?ogc{#})/',
63fbd1cb 251 ],
6fe2934b
KW
252);
253
254my @experimental_regex_sets = (
6d24e9d4 255 '/(?[ \t ])/' => 'The regex_sets feature is experimental {#} m/(?[{#} \t ])/',
b45f050a
JF
256);
257
63fbd1cb 258my @deprecated = (
d7bb0749
KW
259 '/a\b{cde/' => '"\b{" is deprecated; use "\b\{" or "\b[{]" instead {#} m/a\{#}b{cde/',
260 '/a\B{cde/' => '"\B{" is deprecated; use "\B\{" or "\B[{]" instead {#} m/a\{#}B{cde/',
ff56e4f1 261 'use utf8; /(?x)\\85\85\\85/' => 'Escape literal pattern white space under /x {#} m/(?x)\\85\85{#}\\85/',
63fbd1cb
KW
262);
263
cb79f740
NC
264while (my ($regex, $expect) = splice @death, 0, 2) {
265 my $expect = fixup_expect($expect);
3f4fde43 266 no warnings 'experimental::regex_sets';
40809656 267 # skip the utf8 test on EBCDIC since they do not die
cb79f740 268 next if $::IS_EBCDIC && $regex =~ /utf8/;
b45f050a 269
cb79f740
NC
270 warning_is(sub {
271 $_ = "x";
272 eval $regex;
40e98def
KW
273 like($@, qr/\Q$expect/, $regex);
274 }, undef, "... and died without any other warnings");
b45f050a
JF
275}
276
63fbd1cb 277foreach my $ref (\@warning, \@experimental_regex_sets, \@deprecated) {
6fe2934b
KW
278 my $warning_type = ($ref == \@warning)
279 ? 'regexp'
63fbd1cb 280 : ($ref == \@deprecated)
c2a42ab2 281 ? 'regexp, deprecated'
63fbd1cb 282 : 'experimental::regex_sets';
b42857f3 283 while (my ($regex, $expect) = splice @$ref, 0, 2) {
af01601c
KW
284 my @expect = fixup_expect($expect);
285 {
286 $_ = "x";
287 no warnings;
288 eval $regex;
289 }
290 if (is($@, "", "$regex did not die")) {
291 my @got = capture_warnings(sub {
292 $_ = "x";
293 eval $regex });
294 my $count = @expect;
295 if (! is(scalar @got, scalar @expect, "... and gave expected number ($count) of warnings")) {
296 if (@got < @expect) {
297 $count = @got;
298 note "Expected warnings not gotten:\n\t" . join "\n\t", @expect[$count .. $#expect];
299 }
300 else {
301 note "Unexpected warnings gotten:\n\t" . join("\n\t", @got[$count .. $#got]);
302 }
303 }
304 foreach my $i (0 .. $count - 1) {
305 if (like($got[$i], qr/\Q$expect[$i]/, "... and gave expected warning[$i]")) {
306 ok (0 == capture_warnings(sub {
307 $_ = "x";
6fe2934b 308 eval "no warnings '$warning_type'; $regex;" }
af01601c
KW
309 ),
310 "... and turning off '$warning_type' warnings suppressed it");
311 }
312 }
b42857f3 313 }
6fe2934b
KW
314 }
315}
b45f050a 316
cb79f740 317done_testing();