This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
t/re/regexp_unicode_prop.t: Reorder a few tests
[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
3b9fb374
KW
108 my @USER_DEFINED_PROPERTIES = (
109 #
110 # User defined properties
111 #
112 InKana1 => ['\x{3040}', '!\x{303F}'],
113 InKana2 => ['\x{3040}', '!\x{303F}'],
114 InKana3 => ['\x{3041}', '!\x{3040}'],
115 InNotKana => ['\x{3040}', '!\x{3041}'],
116 InConsonant => ['d', '!e'],
117 IsSyriac1 => ['\x{0712}', '!\x{072F}'],
118 IsSyriac1KanaMark => ['\x{309A}', '!\x{3090}'],
119 IsSyriac1KanaMark => ['\x{0730}', '!\x{0712}'],
120 '# User-defined character properties may lack \n at the end',
121 InGreekSmall => ['\N{GREEK SMALL LETTER PI}',
122 '\N{GREEK SMALL LETTER FINAL SIGMA}'],
123 InGreekCapital => ['\N{GREEK CAPITAL LETTER PI}', '!\x{03A2}'],
124 Dash => ['-'],
125 ASCII_Hex_Digit => ['!-', 'A'],
126 IsAsciiHexAndDash => ['-', 'A'],
127
128 # This overrides the official one
129 InLatin1 => ['\x{0100}', '!\x{00FF}'],
130 );
131
132 my @USER_CASELESS_PROPERTIES = (
133 #
134 # User defined properties which differ depending on /i. Second entry
135 # is false normally, true under /i
136 #
137 'IsMyUpper' => ["M", "!m" ],
138 );
19c4061a 139
84281c31
A
140
141#
142# From the short properties we populate POSIX-like classes.
143#
144my %SHORT_PROPERTIES = (
145 'Ll' => ['m', '\N{CYRILLIC SMALL LETTER A}'],
146 'Lu' => ['M', '\N{GREEK CAPITAL LETTER ALPHA}'],
147 'Lo' => ['\N{HIRAGANA LETTER SMALL A}'],
99870f4d
KW
148 # is also in other alphabetic
149 'Mn' => ['\N{HEBREW POINT RAFE}'],
84281c31
A
150 'Nd' => ["0", '\N{ARABIC-INDIC DIGIT ZERO}'],
151 'Pc' => ["_"],
152 'Po' => ["!"],
153 'Zs' => [" "],
154 'Cc' => ['\x{00}'],
155);
156
157#
158# Illegal properties
159#
d658a8a8
DM
160my @ILLEGAL_PROPERTIES =
161 qw[q qrst f foo isfoo infoo ISfoo INfoo Is::foo In::foo];
84281c31
A
162
163my %d;
164
165while (my ($class, $chars) = each %SHORT_PROPERTIES) {
166 push @{$d {IsAlpha}} => map {$class =~ /^[LM]/ ? $_ : "!$_"} @$chars;
167 push @{$d {IsAlnum}} => map {$class =~ /^[LMN]./ ? $_ : "!$_"} @$chars;
168 push @{$d {IsASCII}} => map {length ($_) == 1 || $_ eq '\x{00}'
169 ? $_ : "!$_"} @$chars;
170 push @{$d {IsCntrl}} => map {$class =~ /^C/ ? $_ : "!$_"} @$chars;
171 push @{$d {IsBlank}} => map {$class =~ /^Z[lps]/ ? $_ : "!$_"} @$chars;
172 push @{$d {IsDigit}} => map {$class =~ /^Nd$/ ? $_ : "!$_"} @$chars;
173 push @{$d {IsGraph}} => map {$class =~ /^([LMNPS]|Co)/
174 ? $_ : "!$_"} @$chars;
175 push @{$d {IsPrint}} => map {$class =~ /^([LMNPS]|Co|Zs)/
176 ? $_ : "!$_"} @$chars;
177 push @{$d {IsLower}} => map {$class =~ /^Ll$/ ? $_ : "!$_"} @$chars;
178 push @{$d {IsUpper}} => map {$class =~ /^L[ut]/ ? $_ : "!$_"} @$chars;
179 push @{$d {IsPunct}} => map {$class =~ /^P/ ? $_ : "!$_"} @$chars;
180 push @{$d {IsWord}} => map {$class =~ /^[LMN]/ || $_ eq "_"
181 ? $_ : "!$_"} @$chars;
182 push @{$d {IsSpace}} => map {$class =~ /^Z/ ||
425224e1
KW
183 length ($_) == 1 && utf8::native_to_unicode(ord ($_)) >= 0x09
184 && utf8::native_to_unicode(ord ($_)) <= 0x0D
84281c31
A
185 ? $_ : "!$_"} @$chars;
186}
187
84281c31
A
188push @CLASSES => "# Short properties" => %SHORT_PROPERTIES,
189 "# POSIX like properties" => %d,
190 "# User defined properties" => @USER_DEFINED_PROPERTIES;
191
192
193#
194# Calculate the number of tests.
195#
196my $count = 0;
197for (my $i = 0; $i < @CLASSES; $i += 2) {
198 $i ++, redo if $CLASSES [$i] =~ /^\h*#\h*(.*)/;
72d70e56 199 $count += 2 * (length $CLASSES [$i] == 1 ? 4 : 2) * @{$CLASSES [$i + 1]};
84281c31 200}
72d70e56
NC
201$count += 4 * @ILLEGAL_PROPERTIES;
202$count += 4 * grep {length $_ == 1} @ILLEGAL_PROPERTIES;
203$count += 8 * @USER_CASELESS_PROPERTIES;
710d3eb3 204$count += 1; # Test for pkg:IsMyLower
d82cefba 205$count += 1; # No warnings generated
84281c31 206
72d70e56 207plan(tests => $count);
84281c31
A
208
209run_tests unless caller ();
210
211sub match {
19c4061a
KW
212 my ($char, $match, $nomatch, $caseless) = @_;
213 $caseless = "" unless defined $caseless;
214 $caseless = 'i' if $caseless;
84281c31 215
3a0825d5 216 my ($str, $name) = get_str_name($char);
84281c31 217
19c4061a 218 undef $@;
707250fe
KW
219 my $pat = "qr/$match/$caseless";
220 my $match_pat = eval $pat;
170b30c3
KW
221 if (is($@, '', "$pat compiled correctly to a regexp: $@")) {
222 like($str, $match_pat, "$name correctly matched");
223 }
19c4061a
KW
224
225 undef $@;
707250fe
KW
226 $pat = "qr/$nomatch/$caseless";
227 my $nomatch_pat = eval $pat;
170b30c3
KW
228 if (is($@, '', "$pat compiled correctly to a regexp: $@")) {
229 unlike($str, $nomatch_pat, "$name correctly did not match");
230 }
84281c31
A
231}
232
233sub run_tests {
234
235 while (@CLASSES) {
236 my $class = shift @CLASSES;
237 if ($class =~ /^\h*#\h*(.*)/) {
238 print "# $1\n";
239 next;
240 }
241 last unless @CLASSES;
242 my $chars = shift @CLASSES;
243 my @in = grep {!/^!./} @$chars;
244 my @out = map {s/^!(?=.)//; $_} grep { /^!./} @$chars;
245 my $in_pat = eval qq ['\\p{$class}'];
246 my $out_pat = eval qq ['\\P{$class}'];
247
248 match $_, $in_pat, $out_pat for @in;
249 match $_, $out_pat, $in_pat for @out;
250
9c8ea558 251 if (1 == length $class) { # Repeat without braces if name length 1
84281c31
A
252 my $in_pat = eval qq ['\\p$class'];
253 my $out_pat = eval qq ['\\P$class'];
254
255 match $_, $in_pat, $out_pat for @in;
256 match $_, $out_pat, $in_pat for @out;
257 }
258 }
259
260
84281c31
A
261 print "# Illegal properties\n";
262 foreach my $p (@ILLEGAL_PROPERTIES) {
4003ea29
KW
263 my $pat;
264 if ($p =~ /::/) {
265 $pat = qr /^Illegal user-defined property name/;
266 }
267 else {
268 $pat = qr /^Can't find Unicode property definition/;
269 }
270
84281c31
A
271 undef $@;
272 my $r = eval "'a' =~ /\\p{$p}/; 1";
72d70e56
NC
273 is($r, undef, "Unknown Unicode property \\p{$p}");
274 like($@, $pat, "Unknown Unicode property \\p{$p}");
84281c31
A
275 undef $@;
276 my $s = eval "'a' =~ /\\P{$p}/; 1";
72d70e56
NC
277 is($s, undef, "Unknown Unicode property \\p{$p}");
278 like($@, $pat, "Unknown Unicode property \\p{$p}");
84281c31
A
279 if (length $p == 1) {
280 undef $@;
281 my $r = eval "'a' =~ /\\p$p/; 1";
72d70e56
NC
282 is($r, undef, "Unknown Unicode property \\p$p");
283 like($@, $pat, "Unknown Unicode property \\p$p");
84281c31
A
284 undef $@;
285 my $s = eval "'a' =~ /\\P$p/; 1";
72d70e56
NC
286 is($r, undef, "Unknown Unicode property \\P$p");
287 like($@, $pat, "Unknown Unicode property \\P$p");
84281c31
A
288 }
289 }
19c4061a
KW
290
291 print "# User-defined properties with /i differences\n";
49fdc6e8 292 while (my $class = shift @USER_CASELESS_PROPERTIES) {
19c4061a
KW
293 my $chars_ref = shift @USER_CASELESS_PROPERTIES;
294 my @in = grep {!/^!./} @$chars_ref;
295 my @out = map {s/^!(?=.)//; $_} grep { /^!./} @$chars_ref;
296 my $in_pat = eval qq ['\\p{$class}'];
297 my $out_pat = eval qq ['\\P{$class}'];
298
1e51e719
KW
299 # Verify that adding /i does change the out set to match.
300 match $_, $in_pat, $out_pat, 'i' for @out;
301
302 # Verify that adding /i doesn't change the in set.
303 match $_, $in_pat, $out_pat, 'i' for @in;
304
19c4061a
KW
305 # Verify works as regularly for not /i
306 match $_, $in_pat, $out_pat for @in;
307 match $_, $out_pat, $in_pat for @out;
308
19c4061a 309
19c4061a 310 }
84281c31
A
311}
312
313
314#
315# User defined properties
316#
317
318sub InKana1 {<<'--'}
3193040 309F
32030A0 30FF
321--
322
323sub InKana2 {<<'--'}
324+utf8::InHiragana
325+utf8::InKatakana
326--
327
328sub InKana3 {<<'--'}
329+utf8::InHiragana
330+utf8::InKatakana
331-utf8::IsCn
332--
333
334sub InNotKana {<<'--'}
335!utf8::InHiragana
336-utf8::InKatakana
337+utf8::IsCn
338--
339
425224e1
KW
340sub InConsonant {
341
342 my $return = "+utf8::Lowercase\n&utf8::ASCII\n";
343 $return .= sprintf("-%X\n", ord "a");
344 $return .= sprintf("-%X\n", ord "e");
345 $return .= sprintf("-%X\n", ord "i");
346 $return .= sprintf("-%X\n", ord "o");
347 $return .= sprintf("-%X\n", ord "u");
348 return $return;
349}
84281c31
A
350
351sub IsSyriac1 {<<'--'}
3520712 072C
3530730 074A
354--
355
84281c31
A
356sub InGreekSmall {return "03B1\t03C9"}
357sub InGreekCapital {return "0391\t03A9\n-03A2"}
358
d658a8a8 359sub IsAsciiHexAndDash {<<'--'}
84281c31
A
360+utf8::ASCII_Hex_Digit
361+utf8::Dash
362--
363
eb765580
KW
364sub InLatin1 {
365 return "0100\t10FFFF";
366}
367
19c4061a
KW
368sub IsMyUpper {
369 my $caseless = shift;
425224e1
KW
370 return "+utf8::"
371 . (($caseless)
372 ? 'Alphabetic'
373 : 'Uppercase')
374 . "\n&utf8::ASCII";
19c4061a
KW
375}
376
710d3eb3
KW
377{ # This has to be done here and not like the others, because we have to
378 # make sure that the property is not known until after the regex is
379 # compiled. It was previously getting confused about the pkg and /i
380 # combination
381
382 my $mylower = qr/\p{pkg::IsMyLower}/i;
383
3b9fb374
KW
384sub pkg::IsMyLower {
385 my $caseless = shift;
386 return "+utf8::"
387 . (($caseless)
388 ? 'Alphabetic'
389 : 'Lowercase')
390 . "\n&utf8::ASCII";
391}
710d3eb3
KW
392
393 like("A", $mylower, "Not available until runtime user-defined property with pkg:: and /i works");
394
395}
396
8fd2c59a
KW
397# Verify that can use user-defined properties inside another one
398sub IsSyriac1KanaMark {<<'--'}
399+main::IsSyriac1
400+main::InKana3
401&utf8::IsMark
402--
403
d658a8a8
DM
404# fake user-defined properties; these subs shouldn't be called, because
405# their names don't start with In or Is
406
407sub f { die }
408sub foo { die }
409sub isfoo { die }
410sub infoo { die }
411sub ISfoo { die }
412sub INfoo { die }
413sub Is::foo { die }
414sub In::foo { die }
0a441b11 415
d82cefba
KW
416if (! is(@warnings, 0, "No warnings were generated")) {
417 diag join "\n", @warnings, "\n";
418}
419
0a441b11 4201;
84281c31 421__END__