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