This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
propagate /msix and (?msix) etc flags into (??{})
[perl5.git] / ext / re / t / reflags.t
CommitLineData
1e215989
FC
1#!./perl
2
3BEGIN {
4 require Config;
5 if (($Config::Config{'extensions'} !~ /\bre\b/) ){
6 print "1..0 # Skip -- Perl configured without re module\n";
7 exit 0;
8 }
9}
10
11use strict;
12
ec841a27 13use Test::More tests => 62;
45a507fa 14
342c8524 15my @flags = qw( a d l u );
1e215989
FC
16
17use re '/i';
18ok "Foo" =~ /foo/, 'use re "/i"';
ec841a27 19ok "Foo" =~ /(??{'foo'})/, 'use re "/i" (??{})';
1e215989
FC
20no re '/i';
21ok "Foo" !~ /foo/, 'no re "/i"';
ec841a27 22ok "Foo" !~ /(??{'foo'})/, 'no re "/i" (??{})';
1e215989
FC
23use re '/x';
24ok "foo" =~ / foo /, 'use re "/x"';
ec841a27 25ok "foo" =~ / (??{' foo '}) /, 'use re "/x" (??{})';
1e215989
FC
26no re '/x';
27ok "foo" !~ / foo /, 'no re "/x"';
ec841a27
DM
28ok "foo" !~ /(??{' foo '})/, 'no re "/x" (??{})';
29ok "foo" !~ / (??{'foo'}) /, 'no re "/x" (??{})';
1e215989
FC
30use re '/s';
31ok "\n" =~ /./, 'use re "/s"';
ec841a27 32ok "\n" =~ /(??{'.'})/, 'use re "/s" (??{})';
1e215989
FC
33no re '/s';
34ok "\n" !~ /./, 'no re "/s"';
ec841a27 35ok "\n" !~ /(??{'.'})/, 'no re "/s" (??{})';
1e215989
FC
36use re '/m';
37ok "\nfoo" =~ /^foo/, 'use re "/m"';
ec841a27 38ok "\nfoo" =~ /(??{'^'})foo/, 'use re "/m" (??{})';
1e215989
FC
39no re '/m';
40ok "\nfoo" !~ /^foo/, 'no re "/m"';
ec841a27 41ok "\nfoo" !~ /(??{'^'})foo/, 'no re "/m" (??{})';
1e215989
FC
42
43use re '/xism';
44ok qr// =~ /(?=.*x)(?=.*i)(?=.*s)(?=.*m)/, 'use re "/multiple"';
45no re '/ix';
46ok qr// =~ /(?!.*x)(?!.*i)(?=.*s)(?=.*m)/, 'no re "/i" only turns off /ix';
47no re '/sm';
48
49{
50 use re '/x';
51 ok 'frelp' =~ /f r e l p/, "use re '/x' in a lexical scope"
52}
53ok 'f r e l p' =~ /f r e l p/,
54 "use re '/x' turns off when it drops out of scope";
55
56SKIP: {
57 if (
58 !$Config::Config{d_setlocale}
59 || $Config::Config{ccflags} =~ /\bD?NO_LOCALE\b/
60 ) {
61 skip "no locale support", 7
62 }
63 use locale;
64 use re '/u';
65 is qr//, '(?^u:)', 'use re "/u" with active locale';
66 no re '/u';
67 is qr//, '(?^l:)', 'no re "/u" reverts to /l with locale in scope';
68 no re '/l';
69 is qr//, '(?^l:)', 'no re "/l" is a no-op with locale in scope';
70 use re '/d';
71 is qr//, '(?^:)', 'use re "/d" with locale in scope';
72 no re '/l';
73 no re '/u';
74 is qr//, '(?^:)',
75 'no re "/l" and "/u" are no-ops when not on (locale scope)';
76 no re "/d";
77 is qr//, '(?^l:)', 'no re "/d" reverts to /l with locale in scope';
78 use re "/u";
79 no re "/d";
80 is qr//, '(?^u:)', 'no re "/d" is a no-op when not on (locale scope)';
81}
82
83{
84 use feature "unicode_strings";
85 use re '/d';
86 is qr//, '(?^:)', 'use re "/d" in Unicode scope';
87 no re '/d';
88 is qr//, '(?^u:)', 'no re "/d" reverts to /u in Unicode scope';
89 no re '/u';
90 is qr//, '(?^u:)', 'no re "/u" is a no-op in Unicode scope';
91 no re '/d';
92 is qr//, '(?^u:)', 'no re "/d" is a no-op when not on';
93 use re '/u';
94 no feature 'unicode_strings';
95 is qr//, '(?^u:)', 'use re "/u" is not tied to unicode_strings feature';
96}
97
98use re '/u';
99is qr//, '(?^u:)', 'use re "/u"';
100no re '/u';
101is qr//, '(?^:)', 'no re "/u" reverts to /d';
102no re '/u';
103is qr//, '(?^:)', 'no re "/u" is a no-op when not on';
104no re '/d';
105is qr//, '(?^:)', 'no re "/d" is a no-op when not on';
106
107{
108 local $SIG{__WARN__} = sub {
109 ok $_[0] =~ /Unknown regular expression flag "\x{100}"/,
110 "warning with unknown regexp flags in use re '/flags'"
111 };
112 import re "/\x{100}"
113}
114
115# use re '/flags' in combination with explicit flags
116use re '/xi';
117ok "A\n\n" =~ / a.$/sm, 'use re "/xi" in combination with explicit /sm';
118{
1e215989 119 use re '/u';
686ef6c0 120 is qr//d, '(?^ix:)', 'explicit /d in re "/u" scope';
1e215989 121 use re '/d';
686ef6c0 122 is qr//u, '(?^uix:)', 'explicit /u in re "/d" scope';
1e215989 123}
96ef02be
FC
124no re '/x';
125
342c8524
KW
126# Verify one and two a's work
127use re '/ia';
128is qr//, '(?^ai:)', 'use re "/ia"';
129no re '/ia';
130is qr//, '(?^:)', 'no re "/ia"';
131use re '/aai';
132is qr//, '(?^aai:)', 'use re "/aai"';
133no re '/aai';
134is qr//, '(?^:)', 'no re "/aai"';
135
45a507fa 136# use re "/adul" combinations
96ef02be 137{
45a507fa 138 my $w;
96ef02be 139 local $SIG{__WARN__} = sub { $w = shift };
45a507fa
KW
140 for my $i (@flags) {
141 for my $j (@flags) {
142 $w = "";
143 eval "use re '/$i$j'";
144 if ($i eq $j) {
b4ab316d
KW
145 if ($i eq 'a') {
146 is ($w, "", "no warning with use re \"/aa\", $w");
147 }
148 else {
149 like $w, qr/The \"$i\" flag may not appear twice/,
150 "warning with use re \"/$i$i\"";
151 }
45a507fa
KW
152 }
153 else {
b4ab316d
KW
154 if ($j =~ /$i/) {
155 # If one is a subset of the other, re.pm uses the longest one.
156 like $w, qr/The "$j" and "$i" flags are exclusive/,
157 "warning with eval \"use re \"/$j$i\"";
158 }
159 else {
160 like $w, qr/The "$i" and "$j" flags are exclusive/,
161 "warning with eval \"use re \"/$i$j\"";
162 }
45a507fa
KW
163 }
164 }
165 }
b4ab316d
KW
166
167 $w = "";
342c8524
KW
168 eval "use re '/axaa'";
169 like $w, qr/The "a" flag may only appear a maximum of twice/,
170 "warning with eval \"use re \"/axaa\"";
b4ab316d
KW
171
172
96ef02be 173}