This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Merge branch 'release-5.29.2' into blead
[perl5.git] / t / re / regexp_unicode_prop.t
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
7 use strict;
8 use warnings;
9 use 5.010;
10
11 my @warnings;
12 local $SIG {__WARN__} = sub {push @warnings, "@_"};
13
14 BEGIN {
15     chdir 't' if -d 't';
16     require './test.pl';
17     skip_all_if_miniperl("no dynamic loading on miniperl, no File::Spec (used by charnames)");
18 }
19
20 sub run_tests;
21
22 sub 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
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
59 use charnames ':full';
60
61 my @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        =>
92                             ['!\N{U+7f}',  '\N{U+80}',  '\N{U+ff}', '!\x{100}'],
93     InLatinExtendedA          =>
94                             ['!\N{U+7f}', '!\N{U+80}', '!\N{U+ff}',  '\x{100}'],
95
96     #
97     # Properties are case-insensitive, and may have whitespace,
98     # dashes and underscores.
99     #
100    'in-latin1_SUPPLEMENT'     => ['\N{U+80}',
101                                   '\N{LATIN SMALL LETTER Y WITH DIAERESIS}'],
102    '  ^  In Latin 1 Supplement  '
103                               => ['!\N{U+80}', '\N{COFFIN}'],
104    'latin-1   supplement'     => ['\N{U+80}', "0xDF"],
105
106 );
107
108 my @USER_DEFINED_PROPERTIES;
109 my @USER_CASELESS_PROPERTIES;
110 my @DEFERRED;
111 BEGIN {
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 = (
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'],
136     );
137
138     @USER_CASELESS_PROPERTIES = (
139         #
140         # User defined properties which differ depending on /i.  Second entry
141         # is false normally, true under /i
142         #
143         'IsMyUpper'                => ["M", "!m" ],
144         'pkg::IsMyLower'           => ["a", "!A" ],
145     );
146
147
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
171 my @OVERRIDING_USER_DEFINED_PROPERTIES = (
172    InLatin1                  => ['\x{0100}', '!\x{00FF}'],
173 );
174
175 #
176 # From the short properties we populate POSIX-like classes.
177 #
178 my %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}'],
182     # is also in other alphabetic
183     'Mn'  => ['\N{HEBREW POINT RAFE}'],
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 #
194 my @ILLEGAL_PROPERTIES =
195     qw[q qrst f foo isfoo infoo ISfoo INfoo Is::foo In::foo];
196
197 my %d;
198
199 while (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/ ||
217                                  length ($_) == 1 && utf8::native_to_unicode(ord ($_)) >= 0x09
218                                                   && utf8::native_to_unicode(ord ($_)) <= 0x0D
219                                                      ? $_ : "!$_"} @$chars;
220 }
221
222 push @CLASSES => "# Short properties"        => %SHORT_PROPERTIES,
223                  "# POSIX like properties"   => %d,
224                  "# User defined properties" => @USER_DEFINED_PROPERTIES,
225                  "# Overriding user defined properties" => @OVERRIDING_USER_DEFINED_PROPERTIES;
226
227
228 #
229 # Calculate the number of tests.
230 #
231 my $count = 0;
232 for (my $i = 0; $i < @CLASSES; $i += 2) {
233     $i ++, redo if $CLASSES [$i] =~ /^\h*#\h*(.*)/;
234     $count += 2 * (length $CLASSES [$i] == 1 ? 4 : 2) * @{$CLASSES [$i + 1]};
235 }
236 $count += 4 * @ILLEGAL_PROPERTIES;
237 $count += 4 * grep {length $_ == 1} @ILLEGAL_PROPERTIES;
238 $count += 8 * @USER_CASELESS_PROPERTIES;
239 $count += 1 * @DEFERRED / 2;
240 $count += 1;    # No warnings generated
241
242 plan(tests => $count);
243
244 run_tests unless caller ();
245
246 sub match {
247     my ($char, $match, $nomatch, $caseless) = @_;
248     $caseless = "" unless defined $caseless;
249     $caseless = 'i' if $caseless;
250
251     my ($str, $name) = get_str_name($char);
252
253     undef $@;
254     my $pat = "qr/$match/$caseless";
255     my $match_pat = eval $pat;
256     if (is($@, '', "$pat compiled correctly to a regexp: $@")) {
257         like($str, $match_pat, "$name correctly matched");
258     }
259
260     undef $@;
261     $pat = "qr/$nomatch/$caseless";
262     my $nomatch_pat = eval $pat;
263     if (is($@, '', "$pat compiled correctly to a regexp: $@")) {
264         unlike($str, $nomatch_pat, "$name correctly did not match");
265     }
266 }
267
268 sub run_tests {
269
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
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
292         if (1 == length $class) {   # Repeat without braces if name length 1
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
302     print "# Illegal properties\n";
303     foreach my $p (@ILLEGAL_PROPERTIES) {
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
312         undef $@;
313         my $r = eval "'a' =~ /\\p{$p}/; 1";
314         is($r, undef, "Unknown Unicode property \\p{$p}");
315         like($@, $pat, "Unknown Unicode property \\p{$p}");
316         undef $@;
317         my $s = eval "'a' =~ /\\P{$p}/; 1";
318         is($s, undef, "Unknown Unicode property \\p{$p}");
319         like($@, $pat, "Unknown Unicode property \\p{$p}");
320         if (length $p == 1) {
321             undef $@;
322             my $r = eval "'a' =~ /\\p$p/; 1";
323             is($r, undef, "Unknown Unicode property \\p$p");
324             like($@, $pat, "Unknown Unicode property \\p$p");
325             undef $@;
326             my $s = eval "'a' =~ /\\P$p/; 1";
327             is($r, undef, "Unknown Unicode property \\P$p");
328             like($@, $pat, "Unknown Unicode property \\P$p");
329         }
330     }
331
332     print "# User-defined properties with /i differences\n";
333     while (my $class = shift @USER_CASELESS_PROPERTIES) {
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
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
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
350
351     }
352 }
353
354
355 #
356 # User defined properties
357 #
358
359 sub InKana1 {<<'--'}
360 3040    309F
361 30A0    30FF
362 --
363
364 sub InKana2 {<<'--'}
365 +utf8::InHiragana
366 +utf8::InKatakana
367 --
368
369 sub InKana3 {<<'--'}
370 +utf8::InHiragana
371 +utf8::InKatakana
372 -utf8::IsCn
373 --
374
375 sub InNotKana {<<'--'}
376 !utf8::InHiragana
377 -utf8::InKatakana
378 +utf8::IsCn
379 --
380
381 sub 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 }
391
392 sub IsSyriac1 {<<'--'}
393 0712    072C
394 0730    074A
395 --
396
397 sub InGreekSmall   {return "03B1\t03C9"}
398 sub InGreekCapital {return "0391\t03A9\n-03A2"}
399
400 sub IsAsciiHexAndDash {<<'--'}
401 +utf8::ASCII_Hex_Digit
402 +utf8::Dash
403 --
404
405 sub InLatin1 {
406     return "0100\t10FFFF";
407 }
408
409 sub IsMyUpper {
410     my $caseless = shift;
411     return "+utf8::"
412            . (($caseless)
413                ? 'Alphabetic'
414                : 'Uppercase')
415            . "\n&utf8::ASCII";
416 }
417
418 sub pkg::IsMyLower {
419     my $caseless = shift;
420     return "+utf8::"
421         . (($caseless)
422             ? 'Alphabetic'
423             : 'Lowercase')
424         . "\n&utf8::ASCII";
425 }
426
427 # Verify that can use user-defined properties inside another one
428 sub IsSyriac1KanaMark {<<'--'}
429 +main::IsSyriac1
430 +main::InKana3
431 &utf8::IsMark
432 --
433
434 # fake user-defined properties; these subs shouldn't be called, because
435 # their names don't start with In or Is
436
437 sub f       { die }
438 sub foo     { die }
439 sub isfoo   { die }
440 sub infoo   { die }
441 sub ISfoo   { die }
442 sub INfoo   { die }
443 sub Is::foo { die }
444 sub In::foo { die }
445
446 if (! is(@warnings, 0, "No warnings were generated")) {
447     diag join "\n", @warnings, "\n";
448 }
449
450 1;
451 __END__