This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
t/re/regexp_unicode_prop.t: Avoid a TEST crash if failure
[perl5.git] / t / re / regexp_unicode_prop.t
index 25bb781..2d5b36c 100644 (file)
@@ -8,6 +8,9 @@ use strict;
 use warnings;
 use 5.010;
 
+my @warnings;
+local $SIG {__WARN__} = sub {push @warnings, "@_"};
+
 BEGIN {
     chdir 't' if -d 't';
     require './test.pl';
@@ -16,6 +19,27 @@ BEGIN {
 
 sub run_tests;
 
+sub get_str_name($) {
+    my $char = shift;
+
+    my ($str, $name);
+
+    if ($char =~ /^\\/) {
+        $str  = eval qq ["$char"];
+        $name =      qq ["$char"];
+    }
+    elsif ($char =~ /^0x([0-9A-Fa-f]+)$/) {
+        $str  =  chr hex $1;
+        $name = "chr ($char)";
+    }
+    else {
+        $str  =      $char;
+        $name = qq ["$char"];
+    }
+
+    return ($str, $name);
+}
+
 #
 # This is the data to test.
 #
@@ -81,34 +105,37 @@ my @CLASSES = (
 
 );
 
-my @USER_DEFINED_PROPERTIES = (
-   #
-   # User defined properties
-   #
-   InKana1                   => ['\x{3040}', '!\x{303F}'],
-   InKana2                   => ['\x{3040}', '!\x{303F}'],
-   InKana3                   => ['\x{3041}', '!\x{3040}'],
-   InNotKana                 => ['\x{3040}', '!\x{3041}'],
-   InConsonant               => ['d',        '!e'],
-   IsSyriac1                 => ['\x{0712}', '!\x{072F}'],
-   IsSyriac1KanaMark         => ['\x{309A}', '!\x{3090}'],
-   IsSyriac1KanaMark         => ['\x{0730}', '!\x{0712}'],
-   '# User-defined character properties may lack \n at the end',
-   InGreekSmall              => ['\N{GREEK SMALL LETTER PI}',
-                                 '\N{GREEK SMALL LETTER FINAL SIGMA}'],
-   InGreekCapital            => ['\N{GREEK CAPITAL LETTER PI}', '!\x{03A2}'],
-   Dash                      => ['-'],
-   ASCII_Hex_Digit           => ['!-', 'A'],
-   IsAsciiHexAndDash         => ['-', 'A'],
-);
-
-my @USER_CASELESS_PROPERTIES = (
-   #
-   # User defined properties which differ depending on /i.  Second entry is
-   # false regularly, true under /i
-   #
-   'IsMyUpper'                => ["M", "!m" ],
-);
+    my @USER_DEFINED_PROPERTIES = (
+        #
+        # User defined properties
+        #
+        InKana1                   => ['\x{3040}', '!\x{303F}'],
+        InKana2                   => ['\x{3040}', '!\x{303F}'],
+        InKana3                   => ['\x{3041}', '!\x{3040}'],
+        InNotKana                 => ['\x{3040}', '!\x{3041}'],
+        InConsonant               => ['d',        '!e'],
+        IsSyriac1                 => ['\x{0712}', '!\x{072F}'],
+        IsSyriac1KanaMark         => ['\x{309A}', '!\x{3090}'],
+        IsSyriac1KanaMark         => ['\x{0730}', '!\x{0712}'],
+        '# User-defined character properties may lack \n at the end',
+        InGreekSmall              => ['\N{GREEK SMALL LETTER PI}',
+                                        '\N{GREEK SMALL LETTER FINAL SIGMA}'],
+        InGreekCapital            => ['\N{GREEK CAPITAL LETTER PI}', '!\x{03A2}'],
+        Dash                      => ['-'],
+        ASCII_Hex_Digit           => ['!-', 'A'],
+        IsAsciiHexAndDash         => ['-', 'A'],
+
+        # This overrides the official one
+        InLatin1                  => ['\x{0100}', '!\x{00FF}'],
+    );
+
+    my @USER_CASELESS_PROPERTIES = (
+        #
+        # User defined properties which differ depending on /i.  Second entry
+        # is false normally, true under /i
+        #
+        'IsMyUpper'                => ["M", "!m" ],
+    );
 
 
 #
@@ -174,6 +201,8 @@ for (my $i = 0; $i < @CLASSES; $i += 2) {
 $count += 4 * @ILLEGAL_PROPERTIES;
 $count += 4 * grep {length $_ == 1} @ILLEGAL_PROPERTIES;
 $count += 8 * @USER_CASELESS_PROPERTIES;
+$count += 1;    # Test for pkg:IsMyLower
+$count += 1;    # No warnings generated
 
 plan(tests => $count);
 
@@ -184,32 +213,21 @@ sub match {
     $caseless = "" unless defined $caseless;
     $caseless = 'i' if $caseless;
 
-    my ($str, $name);
-
-    if ($char =~ /^\\/) {
-        $str  = eval qq ["$char"];
-        $name =      qq ["$char"];
-    }
-    elsif ($char =~ /^0x([0-9A-Fa-f]+)$/) {
-        $str  =  chr hex $1;
-        $name = "chr ($char)";
-    }
-    else {
-        $str  =      $char;
-        $name = qq ["$char"];
-    }
+    my ($str, $name) = get_str_name($char);
 
     undef $@;
     my $pat = "qr/$match/$caseless";
     my $match_pat = eval $pat;
-    is($@, '', "$pat compiled correctly to a regexp: $@");
-    like($str, $match_pat, "$name correctly matched");
+    if (is($@, '', "$pat compiled correctly to a regexp: $@")) {
+        like($str, $match_pat, "$name correctly matched");
+    }
 
     undef $@;
     $pat = "qr/$nomatch/$caseless";
     my $nomatch_pat = eval $pat;
-    is($@, '', "$pat compiled correctly to a regexp: $@");
-    unlike($str, $nomatch_pat, "$name correctly did not match");
+    if (is($@, '', "$pat compiled correctly to a regexp: $@")) {
+        unlike($str, $nomatch_pat, "$name correctly did not match");
+    }
 }
 
 sub run_tests {
@@ -271,7 +289,7 @@ sub run_tests {
     }
 
     print "# User-defined properties with /i differences\n";
-    foreach my $class (shift @USER_CASELESS_PROPERTIES) {
+    while (my $class = shift @USER_CASELESS_PROPERTIES) {
         my $chars_ref = shift @USER_CASELESS_PROPERTIES;
         my @in      =                       grep {!/^!./} @$chars_ref;
         my @out     = map {s/^!(?=.)//; $_} grep { /^!./} @$chars_ref;
@@ -341,6 +359,10 @@ sub IsAsciiHexAndDash {<<'--'}
 +utf8::Dash
 --
 
+sub InLatin1 {
+    return "0100\t10FFFF";
+}
+
 sub IsMyUpper {
     my $caseless = shift;
     return "+utf8::"
@@ -350,6 +372,26 @@ sub IsMyUpper {
            . "\n&utf8::ASCII";
 }
 
+{   # This has to be done here and not like the others, because we have to
+    # make sure that the property is not known until after the regex is
+    # compiled.  It was previously getting confused about the pkg and /i
+    # combination
+
+    my $mylower = qr/\p{pkg::IsMyLower}/i;
+
+sub pkg::IsMyLower {
+    my $caseless = shift;
+    return "+utf8::"
+        . (($caseless)
+            ? 'Alphabetic'
+            : 'Lowercase')
+        . "\n&utf8::ASCII";
+}
+
+    like("A", $mylower, "Not available until runtime user-defined property with pkg:: and /i works");
+
+}
+
 # Verify that can use user-defined properties inside another one
 sub IsSyriac1KanaMark {<<'--'}
 +main::IsSyriac1
@@ -368,4 +410,10 @@ sub ISfoo   { die }
 sub INfoo   { die }
 sub Is::foo { die }
 sub In::foo { die }
+
+if (! is(@warnings, 0, "No warnings were generated")) {
+    diag join "\n", @warnings, "\n";
+}
+
+1;
 __END__