This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
t/op/taint.t: Add test
[perl5.git] / t / re / regexp_unicode_prop.t
CommitLineData
84281c31
A
1#!./perl
2#
3# Tests that have to do with checking whether characters have (or not have)
4# certain Unicode properties; belong (or not belong) to blocks, scripts, etc.
5#
6
7use strict;
8use warnings;
9use 5.010;
10
d82cefba
KW
11my @warnings;
12local $SIG {__WARN__} = sub {push @warnings, "@_"};
13
f3b02925 14BEGIN {
b5efbd1f 15 chdir 't' if -d 't';
f3b02925
NC
16 require './test.pl';
17 skip_all_if_miniperl("no dynamic loading on miniperl, no File::Spec (used by charnames)");
18}
84281c31
A
19
20sub run_tests;
21
3a0825d5
KW
22sub get_str_name($) {
23 my $char = shift;
24
25 my ($str, $name);
26
27 if ($char =~ /^\\/) {
28 $str = eval qq ["$char"];
29 $name = qq ["$char"];
30 }
31 elsif ($char =~ /^0x([0-9A-Fa-f]+)$/) {
32 $str = chr hex $1;
33 $name = "chr ($char)";
34 }
35 else {
36 $str = $char;
37 $name = qq ["$char"];
38 }
39
40 return ($str, $name);
41}
42
84281c31
A
43#
44# This is the data to test.
45#
46# This is a hash; keys are the property to test.
47# Values are arrays containing characters to test. The characters can
48# have the following formats:
49# '\N{CHARACTER NAME}' - Use character with that name
50# '\x{1234}' - Use character with that hex escape
51# '0x1234' - Use chr() to get that character
52# "a" - Character to use
53#
54# If a character entry starts with ! the character does not belong to the class
55#
56# If the class is just single letter, we use both \pL and \p{L}
57#
58
59use charnames ':full';
60
61my @CLASSES = (
62 L => ["a", "A"],
63 Ll => ["b", "!B"],
64 Lu => ["!c", "C"],
65 IsLl => ["d", "!D"],
66 IsLu => ["!e", "E"],
67 LC => ["f", "!1"],
68 'L&' => ["g", "!2"],
69 'Lowercase Letter' => ["h", "!H"],
70
71 Common => ["!i", "3"],
72 Inherited => ["!j", '\x{300}'],
73
74 InBasicLatin => ['\N{LATIN CAPITAL LETTER A}'],
75 InLatin1Supplement => ['\N{LATIN CAPITAL LETTER A WITH GRAVE}'],
76 InLatinExtendedA => ['\N{LATIN CAPITAL LETTER A WITH MACRON}'],
77 InLatinExtendedB => ['\N{LATIN SMALL LETTER B WITH STROKE}'],
78 InKatakana => ['\N{KATAKANA LETTER SMALL A}'],
79 IsLatin => ["0x100", "0x212b"],
80 IsHebrew => ["0x5d0", "0xfb4f"],
81 IsGreek => ["0x37a", "0x386", "!0x387", "0x388",
82 "0x38a", "!0x38b", "0x38c"],
83 HangulSyllables => ['\x{AC00}'],
84 'Script=Latin' => ['\x{0100}'],
85 'Block=LatinExtendedA' => ['\x{0100}'],
86 'Category=UppercaseLetter' => ['\x{0100}'],
87
88 #
89 # It's ok to repeat class names.
90 #
91 InLatin1Supplement =>
425224e1 92 ['!\N{U+7f}', '\N{U+80}', '\N{U+ff}', '!\x{100}'],
84281c31 93 InLatinExtendedA =>
425224e1 94 ['!\N{U+7f}', '!\N{U+80}', '!\N{U+ff}', '\x{100}'],
84281c31
A
95
96 #
97 # Properties are case-insensitive, and may have whitespace,
98 # dashes and underscores.
99 #
425224e1 100 'in-latin1_SUPPLEMENT' => ['\N{U+80}',
84281c31
A
101 '\N{LATIN SMALL LETTER Y WITH DIAERESIS}'],
102 ' ^ In Latin 1 Supplement '
425224e1
KW
103 => ['!\N{U+80}', '\N{COFFIN}'],
104 'latin-1 supplement' => ['\N{U+80}', "0xDF"],
84281c31
A
105
106);
107
327cef2f
KW
108my @USER_DEFINED_PROPERTIES;
109my @USER_CASELESS_PROPERTIES;
110my @DEFERRED;
111BEGIN {
112
113 # We defined these at compile time, so that the subroutines that they
114 # refer to aren't known, so that we can test properties not known until
115 # runtime
116
117 @USER_DEFINED_PROPERTIES = (
3b9fb374
KW
118 #
119 # User defined properties
120 #
121 InKana1 => ['\x{3040}', '!\x{303F}'],
122 InKana2 => ['\x{3040}', '!\x{303F}'],
123 InKana3 => ['\x{3041}', '!\x{3040}'],
124 InNotKana => ['\x{3040}', '!\x{3041}'],
125 InConsonant => ['d', '!e'],
126 IsSyriac1 => ['\x{0712}', '!\x{072F}'],
127 IsSyriac1KanaMark => ['\x{309A}', '!\x{3090}'],
128 IsSyriac1KanaMark => ['\x{0730}', '!\x{0712}'],
129 '# User-defined character properties may lack \n at the end',
130 InGreekSmall => ['\N{GREEK SMALL LETTER PI}',
131 '\N{GREEK SMALL LETTER FINAL SIGMA}'],
132 InGreekCapital => ['\N{GREEK CAPITAL LETTER PI}', '!\x{03A2}'],
133 Dash => ['-'],
134 ASCII_Hex_Digit => ['!-', 'A'],
135 IsAsciiHexAndDash => ['-', 'A'],
3b9fb374
KW
136 );
137
327cef2f 138 @USER_CASELESS_PROPERTIES = (
3b9fb374
KW
139 #
140 # User defined properties which differ depending on /i. Second entry
141 # is false normally, true under /i
142 #
143 'IsMyUpper' => ["M", "!m" ],
327cef2f 144 'pkg::IsMyLower' => ["a", "!A" ],
3b9fb374 145 );
19c4061a 146
84281c31 147
327cef2f
KW
148 # Now create a list of properties whose definitions won't be known at
149 # runtime. The qr// below thus will have forward references to them, and
150 # when matched at runtime will not know what's in the property definition
151 my @DEFERRABLE_USER_DEFINED_PROPERTIES;
152 push @DEFERRABLE_USER_DEFINED_PROPERTIES, @USER_DEFINED_PROPERTIES;
153 push @DEFERRABLE_USER_DEFINED_PROPERTIES, @USER_CASELESS_PROPERTIES;
154 for (my $i = 0; $i < @DEFERRABLE_USER_DEFINED_PROPERTIES; $i+=2) {
155 my $property = $DEFERRABLE_USER_DEFINED_PROPERTIES[$i];
156 if ($property =~ / ^ \# /x) {
157 $i++;
158 redo;
159 }
160
161 # Only do this for the properties in the list that are user-defined
162 next if ($property !~ / ( ^ | :: ) I[ns] /x);
163
164 push @DEFERRED, qr/\p{$property}/,
165 $DEFERRABLE_USER_DEFINED_PROPERTIES[$i+1];
166 }
167}
168
169# These override the official ones, so if found before defined, the official
170# ones prevail, so can't test deferred definition
171my @OVERRIDING_USER_DEFINED_PROPERTIES = (
172 InLatin1 => ['\x{0100}', '!\x{00FF}'],
173);
174
84281c31
A
175#
176# From the short properties we populate POSIX-like classes.
177#
178my %SHORT_PROPERTIES = (
179 'Ll' => ['m', '\N{CYRILLIC SMALL LETTER A}'],
180 'Lu' => ['M', '\N{GREEK CAPITAL LETTER ALPHA}'],
181 'Lo' => ['\N{HIRAGANA LETTER SMALL A}'],
99870f4d
KW
182 # is also in other alphabetic
183 'Mn' => ['\N{HEBREW POINT RAFE}'],
84281c31
A
184 'Nd' => ["0", '\N{ARABIC-INDIC DIGIT ZERO}'],
185 'Pc' => ["_"],
186 'Po' => ["!"],
187 'Zs' => [" "],
188 'Cc' => ['\x{00}'],
189);
190
191#
192# Illegal properties
193#
d658a8a8
DM
194my @ILLEGAL_PROPERTIES =
195 qw[q qrst f foo isfoo infoo ISfoo INfoo Is::foo In::foo];
84281c31
A
196
197my %d;
198
199while (my ($class, $chars) = each %SHORT_PROPERTIES) {
200 push @{$d {IsAlpha}} => map {$class =~ /^[LM]/ ? $_ : "!$_"} @$chars;
201 push @{$d {IsAlnum}} => map {$class =~ /^[LMN]./ ? $_ : "!$_"} @$chars;
202 push @{$d {IsASCII}} => map {length ($_) == 1 || $_ eq '\x{00}'
203 ? $_ : "!$_"} @$chars;
204 push @{$d {IsCntrl}} => map {$class =~ /^C/ ? $_ : "!$_"} @$chars;
205 push @{$d {IsBlank}} => map {$class =~ /^Z[lps]/ ? $_ : "!$_"} @$chars;
206 push @{$d {IsDigit}} => map {$class =~ /^Nd$/ ? $_ : "!$_"} @$chars;
207 push @{$d {IsGraph}} => map {$class =~ /^([LMNPS]|Co)/
208 ? $_ : "!$_"} @$chars;
209 push @{$d {IsPrint}} => map {$class =~ /^([LMNPS]|Co|Zs)/
210 ? $_ : "!$_"} @$chars;
211 push @{$d {IsLower}} => map {$class =~ /^Ll$/ ? $_ : "!$_"} @$chars;
212 push @{$d {IsUpper}} => map {$class =~ /^L[ut]/ ? $_ : "!$_"} @$chars;
213 push @{$d {IsPunct}} => map {$class =~ /^P/ ? $_ : "!$_"} @$chars;
214 push @{$d {IsWord}} => map {$class =~ /^[LMN]/ || $_ eq "_"
215 ? $_ : "!$_"} @$chars;
216 push @{$d {IsSpace}} => map {$class =~ /^Z/ ||
425224e1
KW
217 length ($_) == 1 && utf8::native_to_unicode(ord ($_)) >= 0x09
218 && utf8::native_to_unicode(ord ($_)) <= 0x0D
84281c31
A
219 ? $_ : "!$_"} @$chars;
220}
221
84281c31
A
222push @CLASSES => "# Short properties" => %SHORT_PROPERTIES,
223 "# POSIX like properties" => %d,
327cef2f
KW
224 "# User defined properties" => @USER_DEFINED_PROPERTIES,
225 "# Overriding user defined properties" => @OVERRIDING_USER_DEFINED_PROPERTIES;
84281c31
A
226
227
228#
229# Calculate the number of tests.
230#
231my $count = 0;
232for (my $i = 0; $i < @CLASSES; $i += 2) {
233 $i ++, redo if $CLASSES [$i] =~ /^\h*#\h*(.*)/;
72d70e56 234 $count += 2 * (length $CLASSES [$i] == 1 ? 4 : 2) * @{$CLASSES [$i + 1]};
84281c31 235}
72d70e56
NC
236$count += 4 * @ILLEGAL_PROPERTIES;
237$count += 4 * grep {length $_ == 1} @ILLEGAL_PROPERTIES;
238$count += 8 * @USER_CASELESS_PROPERTIES;
327cef2f 239$count += 1 * @DEFERRED / 2;
d82cefba 240$count += 1; # No warnings generated
84281c31 241
72d70e56 242plan(tests => $count);
84281c31
A
243
244run_tests unless caller ();
245
246sub match {
19c4061a
KW
247 my ($char, $match, $nomatch, $caseless) = @_;
248 $caseless = "" unless defined $caseless;
249 $caseless = 'i' if $caseless;
84281c31 250
3a0825d5 251 my ($str, $name) = get_str_name($char);
84281c31 252
19c4061a 253 undef $@;
707250fe
KW
254 my $pat = "qr/$match/$caseless";
255 my $match_pat = eval $pat;
170b30c3
KW
256 if (is($@, '', "$pat compiled correctly to a regexp: $@")) {
257 like($str, $match_pat, "$name correctly matched");
258 }
19c4061a
KW
259
260 undef $@;
707250fe
KW
261 $pat = "qr/$nomatch/$caseless";
262 my $nomatch_pat = eval $pat;
170b30c3
KW
263 if (is($@, '', "$pat compiled correctly to a regexp: $@")) {
264 unlike($str, $nomatch_pat, "$name correctly did not match");
265 }
84281c31
A
266}
267
268sub run_tests {
269
327cef2f
KW
270 for (my $i = 0; $i < @DEFERRED; $i+=2) {
271 my ($str, $name) = get_str_name($DEFERRED[$i+1][0]);
272 like($str, $DEFERRED[$i],
273 "$name correctly matched $DEFERRED[$i] (defn. not known until runtime)");
274 }
275
84281c31
A
276 while (@CLASSES) {
277 my $class = shift @CLASSES;
278 if ($class =~ /^\h*#\h*(.*)/) {
279 print "# $1\n";
280 next;
281 }
282 last unless @CLASSES;
283 my $chars = shift @CLASSES;
284 my @in = grep {!/^!./} @$chars;
285 my @out = map {s/^!(?=.)//; $_} grep { /^!./} @$chars;
286 my $in_pat = eval qq ['\\p{$class}'];
287 my $out_pat = eval qq ['\\P{$class}'];
288
289 match $_, $in_pat, $out_pat for @in;
290 match $_, $out_pat, $in_pat for @out;
291
9c8ea558 292 if (1 == length $class) { # Repeat without braces if name length 1
84281c31
A
293 my $in_pat = eval qq ['\\p$class'];
294 my $out_pat = eval qq ['\\P$class'];
295
296 match $_, $in_pat, $out_pat for @in;
297 match $_, $out_pat, $in_pat for @out;
298 }
299 }
300
301
84281c31
A
302 print "# Illegal properties\n";
303 foreach my $p (@ILLEGAL_PROPERTIES) {
4003ea29
KW
304 my $pat;
305 if ($p =~ /::/) {
306 $pat = qr /^Illegal user-defined property name/;
307 }
308 else {
309 $pat = qr /^Can't find Unicode property definition/;
310 }
311
84281c31
A
312 undef $@;
313 my $r = eval "'a' =~ /\\p{$p}/; 1";
72d70e56
NC
314 is($r, undef, "Unknown Unicode property \\p{$p}");
315 like($@, $pat, "Unknown Unicode property \\p{$p}");
84281c31
A
316 undef $@;
317 my $s = eval "'a' =~ /\\P{$p}/; 1";
72d70e56
NC
318 is($s, undef, "Unknown Unicode property \\p{$p}");
319 like($@, $pat, "Unknown Unicode property \\p{$p}");
84281c31
A
320 if (length $p == 1) {
321 undef $@;
322 my $r = eval "'a' =~ /\\p$p/; 1";
72d70e56
NC
323 is($r, undef, "Unknown Unicode property \\p$p");
324 like($@, $pat, "Unknown Unicode property \\p$p");
84281c31
A
325 undef $@;
326 my $s = eval "'a' =~ /\\P$p/; 1";
72d70e56
NC
327 is($r, undef, "Unknown Unicode property \\P$p");
328 like($@, $pat, "Unknown Unicode property \\P$p");
84281c31
A
329 }
330 }
19c4061a
KW
331
332 print "# User-defined properties with /i differences\n";
49fdc6e8 333 while (my $class = shift @USER_CASELESS_PROPERTIES) {
19c4061a
KW
334 my $chars_ref = shift @USER_CASELESS_PROPERTIES;
335 my @in = grep {!/^!./} @$chars_ref;
336 my @out = map {s/^!(?=.)//; $_} grep { /^!./} @$chars_ref;
337 my $in_pat = eval qq ['\\p{$class}'];
338 my $out_pat = eval qq ['\\P{$class}'];
339
1e51e719
KW
340 # Verify that adding /i does change the out set to match.
341 match $_, $in_pat, $out_pat, 'i' for @out;
342
343 # Verify that adding /i doesn't change the in set.
344 match $_, $in_pat, $out_pat, 'i' for @in;
345
19c4061a
KW
346 # Verify works as regularly for not /i
347 match $_, $in_pat, $out_pat for @in;
348 match $_, $out_pat, $in_pat for @out;
349
19c4061a 350
19c4061a 351 }
84281c31
A
352}
353
354
355#
356# User defined properties
357#
358
359sub InKana1 {<<'--'}
3603040 309F
36130A0 30FF
362--
363
364sub InKana2 {<<'--'}
365+utf8::InHiragana
366+utf8::InKatakana
367--
368
369sub InKana3 {<<'--'}
370+utf8::InHiragana
371+utf8::InKatakana
372-utf8::IsCn
373--
374
375sub InNotKana {<<'--'}
376!utf8::InHiragana
377-utf8::InKatakana
378+utf8::IsCn
379--
380
425224e1
KW
381sub InConsonant {
382
383 my $return = "+utf8::Lowercase\n&utf8::ASCII\n";
384 $return .= sprintf("-%X\n", ord "a");
385 $return .= sprintf("-%X\n", ord "e");
386 $return .= sprintf("-%X\n", ord "i");
387 $return .= sprintf("-%X\n", ord "o");
388 $return .= sprintf("-%X\n", ord "u");
389 return $return;
390}
84281c31
A
391
392sub IsSyriac1 {<<'--'}
3930712 072C
3940730 074A
395--
396
84281c31
A
397sub InGreekSmall {return "03B1\t03C9"}
398sub InGreekCapital {return "0391\t03A9\n-03A2"}
399
d658a8a8 400sub IsAsciiHexAndDash {<<'--'}
84281c31
A
401+utf8::ASCII_Hex_Digit
402+utf8::Dash
403--
404
eb765580
KW
405sub InLatin1 {
406 return "0100\t10FFFF";
407}
408
19c4061a
KW
409sub IsMyUpper {
410 my $caseless = shift;
425224e1
KW
411 return "+utf8::"
412 . (($caseless)
413 ? 'Alphabetic'
414 : 'Uppercase')
415 . "\n&utf8::ASCII";
19c4061a
KW
416}
417
3b9fb374
KW
418sub pkg::IsMyLower {
419 my $caseless = shift;
420 return "+utf8::"
421 . (($caseless)
422 ? 'Alphabetic'
423 : 'Lowercase')
424 . "\n&utf8::ASCII";
425}
710d3eb3 426
8fd2c59a
KW
427# Verify that can use user-defined properties inside another one
428sub IsSyriac1KanaMark {<<'--'}
429+main::IsSyriac1
430+main::InKana3
431&utf8::IsMark
432--
433
d658a8a8
DM
434# fake user-defined properties; these subs shouldn't be called, because
435# their names don't start with In or Is
436
437sub f { die }
438sub foo { die }
439sub isfoo { die }
440sub infoo { die }
441sub ISfoo { die }
442sub INfoo { die }
443sub Is::foo { die }
444sub In::foo { die }
0a441b11 445
d82cefba
KW
446if (! is(@warnings, 0, "No warnings were generated")) {
447 diag join "\n", @warnings, "\n";
448}
449
0a441b11 4501;
84281c31 451__END__