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