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