This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
regcomp.c: Change warning category to just 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) {
36 $element =~ s/{\#}/<-- HERE/;
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(
7253e4e3 51 '/[[=foo=]]/' => 'POSIX syntax [= =] is reserved for future extensions in regex; marked by {#} in 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
7253e4e3 57 '/(?@)/' => 'Sequence (?@...) not implemented in regex; marked by {#} in m/(?@{#})/',
b45f050a 58
9da1dd8f 59 '/(?{ 1/' => 'Missing right curly or square bracket',
b45f050a 60
7253e4e3 61 '/(?(1x))/' => 'Switch condition not recognized in regex; marked by {#} in m/(?(1x{#}))/',
b45f050a 62
7253e4e3 63 '/(?(1)x|y|z)/' => 'Switch (?(condition)... contains too many branches in regex; marked by {#} in m/(?(1)x|y|{#}z)/',
b45f050a 64
7253e4e3 65 '/(?(x)y|x)/' => 'Unknown switch condition (?(x) in regex; marked by {#} in m/(?({#}x)y|x)/',
b45f050a 66
7253e4e3 67 '/(?/' => 'Sequence (? incomplete in regex; marked by {#} in m/(?{#}/',
b45f050a 68
7253e4e3 69 '/(?;x/' => 'Sequence (?;...) not recognized in regex; marked by {#} in m/(?;{#}x/',
1f4f6bf1 70 '/(?<;x/' => 'Group name must start with a non-digit word character in regex; marked by {#} in m/(?<;{#}x/',
cc74c5bd
TS
71 '/(?\ix/' => 'Sequence (?\...) not recognized in regex; marked by {#} in m/(?\{#}ix/',
72 '/(?\mx/' => 'Sequence (?\...) not recognized in regex; marked by {#} in m/(?\{#}mx/',
73 '/(?\:x/' => 'Sequence (?\...) not recognized in regex; marked by {#} in m/(?\{#}:x/',
74 '/(?\=x/' => 'Sequence (?\...) not recognized in regex; marked by {#} in m/(?\{#}=x/',
75 '/(?\!x/' => 'Sequence (?\...) not recognized in regex; marked by {#} in m/(?\{#}!x/',
76 '/(?\<=x/' => 'Sequence (?\...) not recognized in regex; marked by {#} in m/(?\{#}<=x/',
77 '/(?\<!x/' => 'Sequence (?\...) not recognized in regex; marked by {#} in m/(?\{#}<!x/',
78 '/(?\>x/' => 'Sequence (?\...) not recognized in regex; marked by {#} in m/(?\{#}>x/',
9de15fec
KW
79 '/(?^-i:foo)/' => 'Sequence (?^-...) not recognized in regex; marked by {#} in m/(?^-{#}i:foo)/',
80 '/(?^-i)foo/' => 'Sequence (?^-...) not recognized in regex; marked by {#} in m/(?^-{#}i)foo/',
81 '/(?^d:foo)/' => 'Sequence (?^d...) not recognized in regex; marked by {#} in m/(?^d{#}:foo)/',
82 '/(?^d)foo/' => 'Sequence (?^d...) not recognized in regex; marked by {#} in m/(?^d{#})foo/',
0c96c706
KW
83 '/(?^lu:foo)/' => 'Regexp modifiers "l" and "u" are mutually exclusive in regex; marked by {#} in m/(?^lu{#}:foo)/',
84 '/(?^lu)foo/' => 'Regexp modifiers "l" and "u" are mutually exclusive in regex; marked by {#} in m/(?^lu{#})foo/',
85'/(?da:foo)/' => 'Regexp modifiers "d" and "a" are mutually exclusive in regex; marked by {#} in m/(?da{#}:foo)/',
86'/(?lil:foo)/' => 'Regexp modifier "l" may not appear twice in regex; marked by {#} in m/(?lil{#}:foo)/',
87'/(?aaia:foo)/' => 'Regexp modifier "a" may appear a maximum of twice in regex; marked by {#} in m/(?aaia{#}:foo)/',
9442e3b8 88'/(?i-l:foo)/' => 'Regexp modifier "l" may not appear after the "-" in regex; marked by {#} in m/(?i-l{#}:foo)/',
cc74c5bd 89
7253e4e3 90 '/((x)/' => 'Unmatched ( in regex; marked by {#} in m/({#}(x)/',
b45f050a 91
7253e4e3 92 "/x{$inf_p1}/" => "Quantifier in {,} bigger than $inf_m1 in regex; marked by {#} in m/x{{#}$inf_p1}/",
b45f050a 93
b45f050a 94
7253e4e3 95 '/x**/' => 'Nested quantifiers in regex; marked by {#} in m/x**{#}/',
b45f050a 96
7253e4e3 97 '/x[/' => 'Unmatched [ in regex; marked by {#} in m/x[{#}/',
b45f050a 98
7253e4e3 99 '/*/', => 'Quantifier follows nothing in regex; marked by {#} in m/*{#}/',
b45f050a 100
7253e4e3 101 '/\p{x/' => 'Missing right brace on \p{} in regex; marked by {#} in m/\p{{#}x/',
b45f050a 102
169da838 103 '/[\p{x]/' => 'Missing right brace on \p{} in regex; marked by {#} in m/[\p{{#}x]/',
b45f050a 104
7253e4e3 105 '/(x)\2/' => 'Reference to nonexistent group in regex; marked by {#} in m/(x)\2{#}/',
b45f050a 106
40809656 107 'my $m = "\\\"; $m =~ $m', => 'Trailing \ in regex m/\/',
b45f050a 108
b8de99ca
KW
109 '/\x{1/' => 'Missing right brace on \x{} in regex; marked by {#} in m/\x{1{#}/',
110 '/\x{X/' => 'Missing right brace on \x{} in regex; marked by {#} in m/\x{{#}X/',
b45f050a 111
169da838 112 '/[\x{X]/' => 'Missing right brace on \x{} in regex; marked by {#} in m/[\x{{#}X]/',
b8de99ca 113 '/[\x{A]/' => 'Missing right brace on \x{} in regex; marked by {#} in m/[\x{A{#}]/',
b45f050a 114
b8de99ca
KW
115 '/\o{1/' => 'Missing right brace on \o{ in regex; marked by {#} in m/\o{1{#}/',
116 '/\o{X/' => 'Missing right brace on \o{ in regex; marked by {#} in m/\o{{#}X/',
9ffc7554
KW
117
118 '/[\o{X]/' => 'Missing right brace on \o{ in regex; marked by {#} in m/[\o{{#}X]/',
b8de99ca 119 '/[\o{7]/' => 'Missing right brace on \o{ in regex; marked by {#} in m/[\o{7{#}]/',
9ffc7554 120
7253e4e3 121 '/[[:barf:]]/' => 'POSIX class [:barf:] unknown in regex; marked by {#} in m/[[:barf:]{#}]/',
b45f050a 122
7253e4e3 123 '/[[=barf=]]/' => 'POSIX syntax [= =] is reserved for future extensions in regex; marked by {#} in m/[[=barf=]{#}]/',
b45f050a 124
7253e4e3 125 '/[[.barf.]]/' => 'POSIX syntax [. .] is reserved for future extensions in regex; marked by {#} in m/[[.barf.]{#}]/',
b42857f3 126
7253e4e3 127 '/[z-a]/' => 'Invalid [] range "z-a" in regex; marked by {#} in m/[z-a{#}]/',
5528c7ba
RGS
128
129 '/\p/' => 'Empty \p{} in regex; marked by {#} in m/\p{#}/',
130
131 '/\P{}/' => 'Empty \P{} in regex; marked by {#} in m/\P{{#}}/',
3f4fde43
KW
132 '/(?[[[:word]]])/' => "Unmatched ':' in POSIX class in regex; marked by {#} in m/(?[[[:word{#}]]])/",
133 '/(?[[:word]])/' => "Unmatched ':' in POSIX class in regex; marked by {#} in m/(?[[:word{#}]])/",
134 '/(?[[[:digit: ])/' => "Unmatched '[' in POSIX class in regex; marked by {#} in m/(?[[[:digit:{#} ])/",
135 '/(?[[:digit: ])/' => "Unmatched '[' in POSIX class in regex; marked by {#} in m/(?[[:digit:{#} ])/",
136 '/(?[[[::]]])/' => "POSIX class [::] unknown in regex; marked by {#} in m/(?[[[::]{#}]])/",
137 '/(?[[[:w:]]])/' => "POSIX class [:w:] unknown in regex; marked by {#} in m/(?[[[:w:]{#}]])/",
138 '/(?[[:w:]])/' => "POSIX class [:w:] unknown in regex; marked by {#} in m/(?[[:w:]{#}])/",
139 '/(?[a])/' => 'Unexpected character in regex; marked by {#} in m/(?[a{#}])/',
140 '/(?[\t])/l' => '(?[...]) not valid in locale in regex; marked by {#} in m/(?[{#}\t])/',
141 '/(?[ + \t ])/' => 'Unexpected binary operator \'+\' with no preceding operand in regex; marked by {#} in m/(?[ +{#} \t ])/',
142 '/(?[ \cK - ( + \t ) ])/' => 'Unexpected binary operator \'+\' with no preceding operand in regex; marked by {#} in m/(?[ \cK - ( +{#} \t ) ])/',
143 '/(?[ \cK ( \t ) ])/' => 'Unexpected \'(\' with no preceding operator in regex; marked by {#} in m/(?[ \cK ({#} \t ) ])/',
144 '/(?[ \cK \t ])/' => 'Operand with no preceding operator in regex; marked by {#} in m/(?[ \cK \t{#} ])/',
145 '/(?[ \0004 ])/' => 'Need exactly 3 octal digits in regex; marked by {#} in m/(?[ \0004 {#}])/',
146 '/(?[ \05 ])/' => 'Need exactly 3 octal digits in regex; marked by {#} in m/(?[ \05 {#}])/',
147 '/(?[ \o{1038} ])/' => 'Non-octal character in regex; marked by {#} in m/(?[ \o{1038{#}} ])/',
148 '/(?[ \o{} ])/' => 'Number with no digits in regex; marked by {#} in m/(?[ \o{}{#} ])/',
149 '/(?[ \x{defg} ])/' => 'Non-hex character in regex; marked by {#} in m/(?[ \x{defg{#}} ])/',
150 '/(?[ \xabcdef ])/' => 'Use \\x{...} for more than two hex characters in regex; marked by {#} in m/(?[ \xabc{#}def ])/',
151 '/(?[ \x{} ])/' => 'Number with no digits in regex; marked by {#} in m/(?[ \x{}{#} ])/',
152 '/(?[ \cK + ) ])/' => 'Unexpected \')\' in regex; marked by {#} in m/(?[ \cK + ){#} ])/',
153 '/(?[ \cK + ])/' => 'Incomplete expression within \'(?[ ])\' in regex; marked by {#} in m/(?[ \cK + {#}])/',
154 '/(?[ \p{foo} ])/' => 'Property \'foo\' is unknown in regex; marked by {#} in m/(?[ \p{foo}{#} ])/',
155 '/(?[ \p{ foo = bar } ])/' => 'Property \'foo = bar\' is unknown in regex; marked by {#} in m/(?[ \p{ foo = bar }{#} ])/',
156 '/(?[ \8 ])/' => 'Unrecognized escape \8 in character class in regex; marked by {#} in m/(?[ \8{#} ])/',
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 ])/',
162 '/(?[ ])/' => 'Incomplete expression within \'(?[ ])\' in regex; marked by {#} in m/(?[ {#}])/',
163 'm/(?[[a-\d]])/' => 'False [] range "a-\d" in regex; marked by {#} in m/(?[[a-\d{#}]])/',
164 'm/(?[[\w-x]])/' => 'False [] range "\w-" in regex; marked by {#} in m/(?[[\w-{#}x]])/',
165 'm/(?[[a-\pM]])/' => 'False [] range "a-\pM" in regex; marked by {#} in m/(?[[a-\pM{#}]])/',
166 'm/(?[[\pM-x]])/' => 'False [] range "\pM-" in regex; marked by {#} in m/(?[[\pM-{#}x]])/',
902994e4 167 'm/(?[[\N{LATIN CAPITAL LETTER A WITH MACRON AND GRAVE}]])/' => '\N{} in character class restricted to one character in regex; marked by {#} in m/(?[[\N{U+100.300{#}}]])/',
b45f050a 168);
902994e4 169# Tests involving a user-defined charnames translator are in pat_advanced.t
b45f050a 170
af01601c
KW
171# In the following arrays of warnings, the value can be an array of things to
172# expect. If the empty string, it means no warning should be raised.
173
b45f050a 174##
6fe2934b 175## Key-value pairs of code/error of code that should have non-fatal regexp warnings.
b45f050a 176##
cb79f740 177my @warning = (
7253e4e3 178 'm/\b*/' => '\b* matches null string many times in regex; marked by {#} in m/\b*{#}/',
b45f050a 179
7253e4e3 180 'm/[:blank:]/' => 'POSIX syntax [: :] belongs inside character classes in regex; marked by {#} in m/[:blank:]{#}/',
b45f050a 181
7253e4e3 182 "m'[\\y]'" => 'Unrecognized escape \y in character class passed through in regex; marked by {#} in m/[\y{#}]/',
b45f050a 183
7253e4e3
RK
184 'm/[a-\d]/' => 'False [] range "a-\d" in regex; marked by {#} in m/[a-\d{#}]/',
185 'm/[\w-x]/' => 'False [] range "\w-" in regex; marked by {#} in m/[\w-{#}x]/',
f81125e2
JP
186 'm/[a-\pM]/' => 'False [] range "a-\pM" in regex; marked by {#} in m/[a-\pM{#}]/',
187 'm/[\pM-x]/' => 'False [] range "\pM-" in regex; marked by {#} in m/[\pM-{#}x]/',
7253e4e3 188 "m'\\y'" => 'Unrecognized escape \y passed through in regex; marked by {#} in m/\y{#}/',
31c15ce5 189 '/x{3,1}/' => 'Quantifier {n,m} with n > m can\'t match in regex; marked by {#} in m/x{3,1}{#}/',
5e0a247b
KW
190 '/\08/' => '\'\08\' resolved to \'\o{0}8\' in regex; marked by {#} in m/\08{#}/',
191 '/\018/' => '\'\018\' resolved to \'\o{1}8\' in regex; marked by {#} in m/\018{#}/',
192 '/[\08]/' => '\'\08\' resolved to \'\o{0}8\' in regex; marked by {#} in m/[\08{#}]/',
193 '/[\018]/' => '\'\018\' resolved to \'\o{1}8\' in regex; marked by {#} in m/[\018{#}]/',
6fe2934b
KW
194);
195
196my @experimental_regex_sets = (
3f4fde43 197 '/(?[ \t ])/' => 'The regex_sets feature is experimental in regex; marked by {#} in m/(?[{#} \t ])/',
b45f050a
JF
198);
199
cb79f740
NC
200while (my ($regex, $expect) = splice @death, 0, 2) {
201 my $expect = fixup_expect($expect);
3f4fde43 202 no warnings 'experimental::regex_sets';
40809656 203 # skip the utf8 test on EBCDIC since they do not die
cb79f740 204 next if $::IS_EBCDIC && $regex =~ /utf8/;
b45f050a 205
cb79f740
NC
206 warning_is(sub {
207 $_ = "x";
208 eval $regex;
40e98def
KW
209 like($@, qr/\Q$expect/, $regex);
210 }, undef, "... and died without any other warnings");
b45f050a
JF
211}
212
6fe2934b
KW
213foreach my $ref (\@warning, \@experimental_regex_sets) {
214 my $warning_type = ($ref == \@warning)
215 ? 'regexp'
216 : 'experimental::regex_sets';
b42857f3 217 while (my ($regex, $expect) = splice @$ref, 0, 2) {
af01601c
KW
218 my @expect = fixup_expect($expect);
219 {
220 $_ = "x";
221 no warnings;
222 eval $regex;
223 }
224 if (is($@, "", "$regex did not die")) {
225 my @got = capture_warnings(sub {
226 $_ = "x";
227 eval $regex });
228 my $count = @expect;
229 if (! is(scalar @got, scalar @expect, "... and gave expected number ($count) of warnings")) {
230 if (@got < @expect) {
231 $count = @got;
232 note "Expected warnings not gotten:\n\t" . join "\n\t", @expect[$count .. $#expect];
233 }
234 else {
235 note "Unexpected warnings gotten:\n\t" . join("\n\t", @got[$count .. $#got]);
236 }
237 }
238 foreach my $i (0 .. $count - 1) {
239 if (like($got[$i], qr/\Q$expect[$i]/, "... and gave expected warning[$i]")) {
240 ok (0 == capture_warnings(sub {
241 $_ = "x";
6fe2934b 242 eval "no warnings '$warning_type'; $regex;" }
af01601c
KW
243 ),
244 "... and turning off '$warning_type' warnings suppressed it");
245 }
246 }
b42857f3 247 }
6fe2934b
KW
248 }
249}
b45f050a 250
cb79f740 251done_testing();