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.
12 local $SIG {__WARN__} = sub {push @warnings, "@_"};
17 skip_all_if_miniperl("no dynamic loading on miniperl, no File::Spec (used by charnames)");
28 $str = eval qq ["$char"];
31 elsif ($char =~ /^0x([0-9A-Fa-f]+)$/) {
33 $name = "chr ($char)";
44 # This is the data to test.
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
54 # If a character entry starts with ! the character does not belong to the class
56 # If the class is just single letter, we use both \pL and \p{L}
59 use charnames ':full';
69 'Lowercase Letter' => ["h", "!H"],
71 Common => ["!i", "3"],
72 Inherited => ["!j", '\x{300}'],
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}'],
89 # It's ok to repeat class names.
92 ['!\N{U+7f}', '\N{U+80}', '\N{U+ff}', '!\x{100}'],
94 ['!\N{U+7f}', '!\N{U+80}', '!\N{U+ff}', '\x{100}'],
97 # Properties are case-insensitive, and may have whitespace,
98 # dashes and underscores.
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"],
108 my @USER_DEFINED_PROPERTIES;
109 my @USER_CASELESS_PROPERTIES;
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
117 @USER_DEFINED_PROPERTIES = (
119 # User defined properties
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}'],
134 ASCII_Hex_Digit => ['!-', 'A'],
135 IsAsciiHexAndDash => ['-', 'A'],
138 @USER_CASELESS_PROPERTIES = (
140 # User defined properties which differ depending on /i. Second entry
141 # is false normally, true under /i
143 'IsMyUpper' => ["M", "!m" ],
144 'pkg::IsMyLower' => ["a", "!A" ],
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) {
161 # Only do this for the properties in the list that are user-defined
162 next if ($property !~ / ( ^ | :: ) I[ns] /x);
164 push @DEFERRED, qr/\p{$property}/,
165 $DEFERRABLE_USER_DEFINED_PROPERTIES[$i+1];
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}'],
176 # From the short properties we populate POSIX-like classes.
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}'],
194 my @ILLEGAL_PROPERTIES =
195 qw[q qrst f foo isfoo infoo ISfoo INfoo Is::foo In::foo];
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;
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;
229 # Calculate the number of tests.
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]};
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
242 plan(tests => $count);
244 run_tests unless caller ();
247 my ($char, $match, $nomatch, $caseless) = @_;
248 $caseless = "" unless defined $caseless;
249 $caseless = 'i' if $caseless;
251 my ($str, $name) = get_str_name($char);
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");
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");
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)");
277 my $class = shift @CLASSES;
278 if ($class =~ /^\h*#\h*(.*)/) {
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}'];
289 match $_, $in_pat, $out_pat for @in;
290 match $_, $out_pat, $in_pat for @out;
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'];
296 match $_, $in_pat, $out_pat for @in;
297 match $_, $out_pat, $in_pat for @out;
302 print "# Illegal properties\n";
303 foreach my $p (@ILLEGAL_PROPERTIES) {
306 $pat = qr /^Illegal user-defined property name/;
309 $pat = qr /^Can't find Unicode property definition/;
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}");
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) {
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");
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");
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}'];
340 # Verify that adding /i does change the out set to match.
341 match $_, $in_pat, $out_pat, 'i' for @out;
343 # Verify that adding /i doesn't change the in set.
344 match $_, $in_pat, $out_pat, 'i' for @in;
346 # Verify works as regularly for not /i
347 match $_, $in_pat, $out_pat for @in;
348 match $_, $out_pat, $in_pat for @out;
356 # User defined properties
375 sub InNotKana {<<'--'}
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");
392 sub IsSyriac1 {<<'--'}
397 sub InGreekSmall {return "03B1\t03C9"}
398 sub InGreekCapital {return "0391\t03A9\n-03A2"}
400 sub IsAsciiHexAndDash {<<'--'}
401 +utf8::ASCII_Hex_Digit
406 return "0100\t10FFFF";
410 my $caseless = shift;
419 my $caseless = shift;
427 # Verify that can use user-defined properties inside another one
428 sub IsSyriac1KanaMark {<<'--'}
434 # fake user-defined properties; these subs shouldn't be called, because
435 # their names don't start with In or Is
446 if (! is(@warnings, 0, "No warnings were generated")) {
447 diag join "\n", @warnings, "\n";