This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
t/lib/feature/implicit: Generalize for EBCDIC platforms
[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 =>
f3b02925
NC
68 $::IS_EBCDIC ? ['!\x{7f}', '\x{80}', '!\x{100}']
69 : ['!\x{7f}', '\x{80}', '\x{ff}', '!\x{100}'],
84281c31
A
70 InLatinExtendedA =>
71 ['!\x{7f}', '!\x{80}', '!\x{ff}', '\x{100}'],
72
73 #
74 # Properties are case-insensitive, and may have whitespace,
75 # dashes and underscores.
76 #
77 'in-latin1_SUPPLEMENT' => ['\x{80}',
78 '\N{LATIN SMALL LETTER Y WITH DIAERESIS}'],
79 ' ^ In Latin 1 Supplement '
80 => ['!\x{80}', '\N{COFFIN}'],
81 'latin-1 supplement' => ['\x{80}', "0xDF"],
82
83);
84
85my @USER_DEFINED_PROPERTIES = (
86 #
87 # User defined properties
88 #
89 InKana1 => ['\x{3040}', '!\x{303F}'],
90 InKana2 => ['\x{3040}', '!\x{303F}'],
91 InKana3 => ['\x{3041}', '!\x{3040}'],
92 InNotKana => ['\x{3040}', '!\x{3041}'],
93 InConsonant => ['d', '!e'],
94 IsSyriac1 => ['\x{0712}', '!\x{072F}'],
8fd2c59a
KW
95 IsSyriac1KanaMark => ['\x{309A}', '!\x{3090}'],
96 IsSyriac1KanaMark => ['\x{0730}', '!\x{0712}'],
9c8ea558 97 '# User-defined character properties may lack \n at the end',
84281c31
A
98 InGreekSmall => ['\N{GREEK SMALL LETTER PI}',
99 '\N{GREEK SMALL LETTER FINAL SIGMA}'],
100 InGreekCapital => ['\N{GREEK CAPITAL LETTER PI}', '!\x{03A2}'],
101 Dash => ['-'],
102 ASCII_Hex_Digit => ['!-', 'A'],
d658a8a8 103 IsAsciiHexAndDash => ['-', 'A'],
84281c31
A
104);
105
19c4061a
KW
106my @USER_CASELESS_PROPERTIES = (
107 #
108 # User defined properties which differ depending on /i. Second entry is
109 # false regularly, true under /i
110 #
111 'IsMyUpper' => ["M", "!m" ],
112);
113
84281c31
A
114
115#
116# From the short properties we populate POSIX-like classes.
117#
118my %SHORT_PROPERTIES = (
119 'Ll' => ['m', '\N{CYRILLIC SMALL LETTER A}'],
120 'Lu' => ['M', '\N{GREEK CAPITAL LETTER ALPHA}'],
121 'Lo' => ['\N{HIRAGANA LETTER SMALL A}'],
99870f4d
KW
122 # is also in other alphabetic
123 'Mn' => ['\N{HEBREW POINT RAFE}'],
84281c31
A
124 'Nd' => ["0", '\N{ARABIC-INDIC DIGIT ZERO}'],
125 'Pc' => ["_"],
126 'Po' => ["!"],
127 'Zs' => [" "],
128 'Cc' => ['\x{00}'],
129);
130
131#
132# Illegal properties
133#
d658a8a8
DM
134my @ILLEGAL_PROPERTIES =
135 qw[q qrst f foo isfoo infoo ISfoo INfoo Is::foo In::foo];
84281c31
A
136
137my %d;
138
139while (my ($class, $chars) = each %SHORT_PROPERTIES) {
140 push @{$d {IsAlpha}} => map {$class =~ /^[LM]/ ? $_ : "!$_"} @$chars;
141 push @{$d {IsAlnum}} => map {$class =~ /^[LMN]./ ? $_ : "!$_"} @$chars;
142 push @{$d {IsASCII}} => map {length ($_) == 1 || $_ eq '\x{00}'
143 ? $_ : "!$_"} @$chars;
144 push @{$d {IsCntrl}} => map {$class =~ /^C/ ? $_ : "!$_"} @$chars;
145 push @{$d {IsBlank}} => map {$class =~ /^Z[lps]/ ? $_ : "!$_"} @$chars;
146 push @{$d {IsDigit}} => map {$class =~ /^Nd$/ ? $_ : "!$_"} @$chars;
147 push @{$d {IsGraph}} => map {$class =~ /^([LMNPS]|Co)/
148 ? $_ : "!$_"} @$chars;
149 push @{$d {IsPrint}} => map {$class =~ /^([LMNPS]|Co|Zs)/
150 ? $_ : "!$_"} @$chars;
151 push @{$d {IsLower}} => map {$class =~ /^Ll$/ ? $_ : "!$_"} @$chars;
152 push @{$d {IsUpper}} => map {$class =~ /^L[ut]/ ? $_ : "!$_"} @$chars;
153 push @{$d {IsPunct}} => map {$class =~ /^P/ ? $_ : "!$_"} @$chars;
154 push @{$d {IsWord}} => map {$class =~ /^[LMN]/ || $_ eq "_"
155 ? $_ : "!$_"} @$chars;
156 push @{$d {IsSpace}} => map {$class =~ /^Z/ ||
157 length ($_) == 1 && ord ($_) >= 0x09
158 && ord ($_) <= 0x0D
159 ? $_ : "!$_"} @$chars;
160}
161
f3b02925 162delete $d {IsASCII} if $::IS_EBCDIC;
84281c31
A
163
164push @CLASSES => "# Short properties" => %SHORT_PROPERTIES,
165 "# POSIX like properties" => %d,
166 "# User defined properties" => @USER_DEFINED_PROPERTIES;
167
168
169#
170# Calculate the number of tests.
171#
172my $count = 0;
173for (my $i = 0; $i < @CLASSES; $i += 2) {
174 $i ++, redo if $CLASSES [$i] =~ /^\h*#\h*(.*)/;
72d70e56 175 $count += 2 * (length $CLASSES [$i] == 1 ? 4 : 2) * @{$CLASSES [$i + 1]};
84281c31 176}
72d70e56
NC
177$count += 4 * @ILLEGAL_PROPERTIES;
178$count += 4 * grep {length $_ == 1} @ILLEGAL_PROPERTIES;
179$count += 8 * @USER_CASELESS_PROPERTIES;
84281c31 180
72d70e56 181plan(tests => $count);
84281c31
A
182
183run_tests unless caller ();
184
185sub match {
19c4061a
KW
186 my ($char, $match, $nomatch, $caseless) = @_;
187 $caseless = "" unless defined $caseless;
188 $caseless = 'i' if $caseless;
84281c31
A
189
190 my ($str, $name);
191
8d4c8383
BF
192 if ($char =~ /^\\/) {
193 $str = eval qq ["$char"];
194 $name = qq ["$char"];
195 }
196 elsif ($char =~ /^0x([0-9A-Fa-f]+)$/) {
197 $str = chr hex $1;
198 $name = "chr ($char)";
199 }
200 else {
201 $str = $char;
202 $name = qq ["$char"];
84281c31
A
203 }
204
19c4061a 205 undef $@;
707250fe
KW
206 my $pat = "qr/$match/$caseless";
207 my $match_pat = eval $pat;
208 is($@, '', "$pat compiled correctly to a regexp: $@");
72d70e56 209 like($str, $match_pat, "$name correctly matched");
19c4061a
KW
210
211 undef $@;
707250fe
KW
212 $pat = "qr/$nomatch/$caseless";
213 my $nomatch_pat = eval $pat;
214 is($@, '', "$pat compiled correctly to a regexp: $@");
72d70e56 215 unlike($str, $nomatch_pat, "$name correctly did not match");
84281c31
A
216}
217
218sub run_tests {
219
220 while (@CLASSES) {
221 my $class = shift @CLASSES;
222 if ($class =~ /^\h*#\h*(.*)/) {
223 print "# $1\n";
224 next;
225 }
226 last unless @CLASSES;
227 my $chars = shift @CLASSES;
228 my @in = grep {!/^!./} @$chars;
229 my @out = map {s/^!(?=.)//; $_} grep { /^!./} @$chars;
230 my $in_pat = eval qq ['\\p{$class}'];
231 my $out_pat = eval qq ['\\P{$class}'];
232
233 match $_, $in_pat, $out_pat for @in;
234 match $_, $out_pat, $in_pat for @out;
235
9c8ea558 236 if (1 == length $class) { # Repeat without braces if name length 1
84281c31
A
237 my $in_pat = eval qq ['\\p$class'];
238 my $out_pat = eval qq ['\\P$class'];
239
240 match $_, $in_pat, $out_pat for @in;
241 match $_, $out_pat, $in_pat for @out;
242 }
243 }
244
245
246 my $pat = qr /^Can't find Unicode property definition/;
247 print "# Illegal properties\n";
248 foreach my $p (@ILLEGAL_PROPERTIES) {
249 undef $@;
250 my $r = eval "'a' =~ /\\p{$p}/; 1";
72d70e56
NC
251 is($r, undef, "Unknown Unicode property \\p{$p}");
252 like($@, $pat, "Unknown Unicode property \\p{$p}");
84281c31
A
253 undef $@;
254 my $s = eval "'a' =~ /\\P{$p}/; 1";
72d70e56
NC
255 is($s, undef, "Unknown Unicode property \\p{$p}");
256 like($@, $pat, "Unknown Unicode property \\p{$p}");
84281c31
A
257 if (length $p == 1) {
258 undef $@;
259 my $r = eval "'a' =~ /\\p$p/; 1";
72d70e56
NC
260 is($r, undef, "Unknown Unicode property \\p$p");
261 like($@, $pat, "Unknown Unicode property \\p$p");
84281c31
A
262 undef $@;
263 my $s = eval "'a' =~ /\\P$p/; 1";
72d70e56
NC
264 is($r, undef, "Unknown Unicode property \\P$p");
265 like($@, $pat, "Unknown Unicode property \\P$p");
84281c31
A
266 }
267 }
19c4061a
KW
268
269 print "# User-defined properties with /i differences\n";
270 foreach my $class (shift @USER_CASELESS_PROPERTIES) {
271 my $chars_ref = shift @USER_CASELESS_PROPERTIES;
272 my @in = grep {!/^!./} @$chars_ref;
273 my @out = map {s/^!(?=.)//; $_} grep { /^!./} @$chars_ref;
274 my $in_pat = eval qq ['\\p{$class}'];
275 my $out_pat = eval qq ['\\P{$class}'];
276
277 # Verify works as regularly for not /i
278 match $_, $in_pat, $out_pat for @in;
279 match $_, $out_pat, $in_pat for @out;
280
281 # Verify that adding /i doesn't change the in set.
282 match $_, $in_pat, $out_pat, 'i' for @in;
283
284 # Verify that adding /i does change the out set to match.
285 match $_, $in_pat, $out_pat, 'i' for @out;
286 }
84281c31
A
287}
288
289
290#
291# User defined properties
292#
293
294sub InKana1 {<<'--'}
2953040 309F
29630A0 30FF
297--
298
299sub InKana2 {<<'--'}
300+utf8::InHiragana
301+utf8::InKatakana
302--
303
304sub InKana3 {<<'--'}
305+utf8::InHiragana
306+utf8::InKatakana
307-utf8::IsCn
308--
309
310sub InNotKana {<<'--'}
311!utf8::InHiragana
312-utf8::InKatakana
313+utf8::IsCn
314--
315
316sub InConsonant {<<'--'} # Not EBCDIC-aware.
3170061 007f
318-0061
319-0065
320-0069
321-006f
322-0075
323--
324
325sub IsSyriac1 {<<'--'}
3260712 072C
3270730 074A
328--
329
84281c31
A
330sub InGreekSmall {return "03B1\t03C9"}
331sub InGreekCapital {return "0391\t03A9\n-03A2"}
332
d658a8a8 333sub IsAsciiHexAndDash {<<'--'}
84281c31
A
334+utf8::ASCII_Hex_Digit
335+utf8::Dash
336--
337
19c4061a
KW
338sub IsMyUpper {
339 my $caseless = shift;
340 if ($caseless) {
341 return "0041\t005A\n0061\t007A"
342 }
343 else {
344 return "0041\t005A"
345 }
346}
347
8fd2c59a
KW
348# Verify that can use user-defined properties inside another one
349sub IsSyriac1KanaMark {<<'--'}
350+main::IsSyriac1
351+main::InKana3
352&utf8::IsMark
353--
354
d658a8a8
DM
355# fake user-defined properties; these subs shouldn't be called, because
356# their names don't start with In or Is
357
358sub f { die }
359sub foo { die }
360sub isfoo { die }
361sub infoo { die }
362sub ISfoo { die }
363sub INfoo { die }
364sub Is::foo { die }
365sub In::foo { die }
84281c31 366__END__