This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Re: sprintf 64 test
[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
7253e4e3
RK
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(
7253e4e3 29 '/[[=foo=]]/' => 'POSIX syntax [= =] is reserved for future extensions in regex; marked by {#} in m/[[=foo=]{#}]/',
b45f050a 30
7253e4e3 31 '/(?<= .*)/' => 'Variable length lookbehind not implemented in regex; marked by {#} in m/(?<= .*){#}/',
b45f050a 32
7253e4e3 33 '/(?<= x{1000})/' => 'Lookbehind longer than 255 not implemented in regex; marked by {#} in m/(?<= x{1000}){#}/',
b45f050a 34
7253e4e3 35 '/(?@)/' => 'Sequence (?@...) not implemented in regex; marked by {#} in m/(?@{#})/',
b45f050a 36
7253e4e3 37 '/(?{ 1/' => 'Sequence (?{...}) not terminated or not {}-balanced in regex; marked by {#} in m/(?{{#} 1/',
b45f050a 38
7253e4e3 39 '/(?(1x))/' => 'Switch condition not recognized in regex; marked by {#} in m/(?(1x{#}))/',
b45f050a 40
7253e4e3 41 '/(?(1)x|y|z)/' => 'Switch (?(condition)... contains too many branches in regex; marked by {#} in m/(?(1)x|y|{#}z)/',
b45f050a 42
7253e4e3 43 '/(?(x)y|x)/' => 'Unknown switch condition (?(x) in regex; marked by {#} in m/(?({#}x)y|x)/',
b45f050a 44
7253e4e3 45 '/(?/' => 'Sequence (? incomplete in regex; marked by {#} in m/(?{#}/',
b45f050a 46
7253e4e3
RK
47 '/(?;x/' => 'Sequence (?;...) not recognized in regex; marked by {#} in m/(?;{#}x/',
48 '/(?<;x/' => 'Sequence (?<;...) not recognized in regex; marked by {#} in m/(?<;{#}x/',
b45f050a 49
7253e4e3 50 '/((x)/' => 'Unmatched ( in regex; marked by {#} in m/({#}(x)/',
b45f050a 51
7253e4e3 52 "/x{$inf_p1}/" => "Quantifier in {,} bigger than $inf_m1 in regex; marked by {#} in m/x{{#}$inf_p1}/",
b45f050a 53
7253e4e3 54 '/x{3,1}/' => 'Can\'t do {n,m} with n > m in regex; marked by {#} in m/x{3,1}{#}/',
b45f050a 55
7253e4e3 56 '/x**/' => 'Nested quantifiers in regex; marked by {#} in m/x**{#}/',
b45f050a 57
7253e4e3 58 '/x[/' => 'Unmatched [ in regex; marked by {#} in m/x[{#}/',
b45f050a 59
7253e4e3 60 '/*/', => 'Quantifier follows nothing in regex; marked by {#} in m/*{#}/',
b45f050a 61
7253e4e3 62 '/\p{x/' => 'Missing right brace on \p{} in regex; marked by {#} in m/\p{{#}x/',
b45f050a 63
169da838 64 '/[\p{x]/' => 'Missing right brace on \p{} in regex; marked by {#} in m/[\p{{#}x]/',
b45f050a 65
7253e4e3 66 '/(x)\2/' => 'Reference to nonexistent group in regex; marked by {#} in m/(x)\2{#}/',
b45f050a 67
40809656 68 'my $m = "\\\"; $m =~ $m', => 'Trailing \ in regex m/\/',
b45f050a 69
7253e4e3 70 '/\x{1/' => 'Missing right brace on \x{} in regex; marked by {#} in m/\x{{#}1/',
b45f050a 71
169da838 72 '/[\x{X]/' => 'Missing right brace on \x{} in regex; marked by {#} in m/[\x{{#}X]/',
b45f050a 73
7253e4e3 74 '/[[:barf:]]/' => 'POSIX class [:barf:] unknown in regex; marked by {#} in m/[[:barf:]{#}]/',
b45f050a 75
7253e4e3 76 '/[[=barf=]]/' => 'POSIX syntax [= =] is reserved for future extensions in regex; marked by {#} in m/[[=barf=]{#}]/',
b45f050a 77
7253e4e3 78 '/[[.barf.]]/' => 'POSIX syntax [. .] is reserved for future extensions in regex; marked by {#} in m/[[.barf.]{#}]/',
b45f050a 79
7253e4e3 80 '/[z-a]/' => 'Invalid [] range "z-a" in regex; marked by {#} in m/[z-a{#}]/',
5528c7ba
RGS
81
82 '/\p/' => 'Empty \p{} in regex; marked by {#} in m/\p{#}/',
83
84 '/\P{}/' => 'Empty \P{} in regex; marked by {#} in m/\P{{#}}/',
b45f050a
JF
85);
86
87##
88## Key-value pairs of code/error of code that should have non-fatal warnings.
89##
90@warning = (
7253e4e3 91 "m/(?p{ 'a' })/" => "(?p{}) is deprecated - use (??{}) in regex; marked by {#} in m/(?p{#}{ 'a' })/",
b45f050a 92
7253e4e3 93 'm/\b*/' => '\b* matches null string many times in regex; marked by {#} in m/\b*{#}/',
b45f050a 94
7253e4e3 95 'm/[:blank:]/' => 'POSIX syntax [: :] belongs inside character classes in regex; marked by {#} in m/[:blank:]{#}/',
b45f050a 96
7253e4e3 97 "m'[\\y]'" => 'Unrecognized escape \y in character class passed through in regex; marked by {#} in m/[\y{#}]/',
b45f050a 98
7253e4e3
RK
99 'm/[a-\d]/' => 'False [] range "a-\d" in regex; marked by {#} in m/[a-\d{#}]/',
100 'm/[\w-x]/' => 'False [] range "\w-" in regex; marked by {#} in m/[\w-{#}x]/',
f81125e2
JP
101 'm/[a-\pM]/' => 'False [] range "a-\pM" in regex; marked by {#} in m/[a-\pM{#}]/',
102 'm/[\pM-x]/' => 'False [] range "\pM-" in regex; marked by {#} in m/[\pM-{#}x]/',
7253e4e3 103 "m'\\y'" => 'Unrecognized escape \y passed through in regex; marked by {#} in m/\y{#}/',
b45f050a
JF
104);
105
106my $total = (@death + @warning)/2;
107
40809656
PP
108# utf8 is a noop on EBCDIC platforms, it is not fatal
109my $Is_EBCDIC = (ord('A') == 193);
110if ($Is_EBCDIC) {
111 my @utf8_death = grep(/utf8/, @death);
7387eed8 112 $total = $total - @utf8_death;
40809656
PP
113}
114
b45f050a
JF
115print "1..$total\n";
116
117my $count = 0;
118
119while (@death)
120{
b45f050a
JF
121 my $regex = shift @death;
122 my $result = shift @death;
40809656
PP
123 # skip the utf8 test on EBCDIC since they do not die
124 next if ($Is_EBCDIC && $regex =~ /utf8/);
125 $count++;
b45f050a 126
b45f050a
JF
127 $_ = "x";
128 eval $regex;
129 if (not $@) {
69f2e79d 130 print "# oops, $regex didn't die\nnot ok $count\n";
b45f050a
JF
131 next;
132 }
133 chomp $@;
b45f050a
JF
134 $result =~ s/{\#}/$marker1/;
135 $result =~ s/{\#}/$marker2/;
197cf9b9 136 $result .= " at ";
69f2e79d
MG
137 if ($@ !~ /^\Q$result/) {
138 print "# For $regex, expected:\n# $result\n# Got:\n# $@\n#\nnot ";
b45f050a 139 }
d8176a88 140 print "ok $count - $regex\n";
b45f050a
JF
141}
142
143
144our $warning;
145$SIG{__WARN__} = sub { $warning = shift };
146
147while (@warning)
148{
149 $count++;
150 my $regex = shift @warning;
151 my $result = shift @warning;
152
153 undef $warning;
154 $_ = "x";
155 eval $regex;
156
157 if ($@)
158 {
69f2e79d 159 print "# oops, $regex died with:\n#\t$@#\nnot ok $count\n";
b45f050a
JF
160 next;
161 }
162
163 if (not $warning)
164 {
69f2e79d 165 print "# oops, $regex didn't generate a warning\nnot ok $count\n";
b45f050a
JF
166 next;
167 }
b45f050a
JF
168 $result =~ s/{\#}/$marker1/;
169 $result =~ s/{\#}/$marker2/;
197cf9b9 170 $result .= " at ";
69f2e79d 171 if ($warning !~ /^\Q$result/)
b45f050a 172 {
69f2e79d
MG
173 print <<"EOM";
174# For $regex, expected:
175# $result
176# Got:
177# $warning
178#
179not ok $count
180EOM
b45f050a
JF
181 next;
182 }
d8176a88 183 print "ok $count - $regex\n";
b45f050a
JF
184}
185
186
187