3 # This is a home for regular expression tests that don't fit into
4 # the format supported by re/regexp.t. If you want to add a test
5 # that does fit that format, add it to re/re_tests, not here.
19 @INC = ('../lib','.');
21 skip_all_if_miniperl("no dynamic loading on miniperl, no re");
25 plan tests => 123; # Update this when adding/deleting tests.
27 run_tests() unless caller;
34 my $message = "Call code from qr //";
35 local $_ = 'var="foo"';
38 ok(/$a$a/ && $b eq '9', $message);
41 ok(/$a$a/ && $b eq '11', $message);
45 like($@, qr/not allowed at runtime/, $message);
49 is($b, '14', $message);
54 my $lex_res = ($lex_b =~ qr/$lex_b(?{ $lex_c = $lex_a++ })/);
56 is($lex_res, 1, $message);
57 is($lex_a, 44, $message);
58 is($lex_c, 43, $message);
62 my $match = eval { /$a$c$a/ };
63 ok($@ && $@ =~ /Eval-group not allowed/ && !$match, $message);
64 is($b, '14', $message);
70 $lex_res = ($lex_b =~ qr/17(?{ $lex_c = $lex_a++ })/);
72 is($lex_res, 1, $message);
73 is($lex_a, 44, $message);
74 is($lex_c, 43, $message);
79 our $a = bless qr /foo/ => 'Foo';
80 ok 'goodfood' =~ $a, "Reblessed qr // matches";
81 is($a, '(?^:foo)', "Reblessed qr // stringifies");
83 my $z = my $y = "\317\276"; # Byte representation of $x
85 ok $x =~ $a, "UTF-8 interpolation in qr //";
86 ok "a$a" =~ $x, "Stringified qr // preserves UTF-8";
87 ok "a$x" =~ /^a$a\z/, "Interpolated qr // preserves UTF-8";
88 ok "a$x" =~ /^a(??{$a})\z/,
89 "Postponed interpolation of qr // preserves UTF-8";
92 is(length qr /##/x, 9, "## in qr // doesn't corrupt memory; Bug 17776");
96 ok "$x$x" =~ /^$x(??{$x})\z/,
97 "Postponed UTF-8 string in UTF-8 re matches UTF-8";
98 ok "$y$x" =~ /^$y(??{$x})\z/,
99 "Postponed UTF-8 string in non-UTF-8 re matches UTF-8";
100 ok "$y$x" !~ /^$y(??{$y})\z/,
101 "Postponed non-UTF-8 string in non-UTF-8 re doesn't match UTF-8";
102 ok "$x$x" !~ /^$x(??{$y})\z/,
103 "Postponed non-UTF-8 string in UTF-8 re doesn't match UTF-8";
104 ok "$y$y" =~ /^$y(??{$y})\z/,
105 "Postponed non-UTF-8 string in non-UTF-8 re matches non-UTF8";
106 ok "$x$y" =~ /^$x(??{$y})\z/,
107 "Postponed non-UTF-8 string in UTF-8 re matches non-UTF8";
109 $y = $z; # Reset $y after upgrade.
110 ok "$x$y" !~ /^$x(??{$x})\z/,
111 "Postponed UTF-8 string in UTF-8 re doesn't match non-UTF-8";
112 ok "$y$y" !~ /^$y(??{$x})\z/,
113 "Postponed UTF-8 string in non-UTF-8 re doesn't match non-UTF-8";
120 # Test if $^N and $+ work in (?{{})
128 push @ctl_n, (defined $^N ? $^N : "undef");
129 push @plus, (defined $+ ? $+ : "undef");
140 # [ Expected result, Regex, Expected value(s) of $^N, Expected value(s) of $+ ]
141 [ 1, qr#^$nested_tags$#, "bla blubb bla", "a b a" ],
142 [ 1, qr#^($nested_tags)$#, "bla blubb <bla><blubb></blubb></bla>", "a b a" ],
143 [ 1, qr#^(|)$nested_tags$#, "bla blubb bla", "a b a" ],
144 [ 1, qr#^(?:|)$nested_tags$#, "bla blubb bla", "a b a" ],
145 [ 1, qr#^<(bl|bla)>$nested_tags<(/\1)>$#, "blubb /bla", "b /bla" ],
146 [ 1, qr#(??{"(|)"})$nested_tags$#, "bla blubb bla", "a b a" ],
147 [ 1, qr#^(??{"(bla|)"})$nested_tags$#, "bla blubb bla", "a b a" ],
148 [ 1, qr#^(??{"(|)"})(??{$nested_tags})$#, "bla blubb undef", "a b undef" ],
149 [ 1, qr#^(??{"(?:|)"})$nested_tags$#, "bla blubb bla", "a b a" ],
150 [ 1, qr#^((??{"(?:bla|)"}))((??{$nested_tags}))$#, "bla blubb <bla><blubb></blubb></bla>", "a b <bla><blubb></blubb></bla>" ],
151 [ 1, qr#^((??{"(?!)?"}))((??{$nested_tags}))$#, "bla blubb <bla><blubb></blubb></bla>", "a b <bla><blubb></blubb></bla>" ],
152 [ 1, qr#^((??{"(?:|<(/?bla)>)"}))((??{$nested_tags}))\1$#, "bla blubb <bla><blubb></blubb></bla>", "a b <bla><blubb></blubb></bla>" ],
153 [ 0, qr#^((??{"(?!)"}))?((??{$nested_tags}))(?!)$#, "bla blubb undef", "a b undef" ],
155 ) { #"#silence vim highlighting
159 my $match = (("<bla><blubb></blubb></bla>" =~ $test->[1]) ? 1 : 0);
160 push @ctl_n, (defined $^N ? $^N : "undef");
161 push @plus, (defined $+ ? $+ : "undef");
162 ok($test->[0] == $match, "match $c");
163 if ($test->[0] != $match) {
164 # unset @ctl_n and @plus
167 is("@ctl_n", $test->[2], "ctl_n $c");
168 is("@plus", $test->[3], "plus $c");
179 defined $_[0] ? $_[0] : "undef";
182 like("123", qr/^(\d)(((??{1 + $^N})))+$/, 'Bug 56194');
187 my $re = qr#(1)((??{ push @ctl_n, $f->($^N); push @plus, $f->($+); $^N + 1}))*(?{$^N})#;
188 my $re2 = qr#(1)((??{ push @ctl_n, $f->($^N); push @plus, $f->($+); $^N + 1}))*(?{$^N})(|a(b)c|def)(??{"$^R"})#;
189 my $re3 = qr#(1)((??{ push @ctl_n, $f->($^N); push @plus, $f->($+); $^N + 1})){2}(?{$^N})(|a(b)c|def)(??{"$^R"})#;
191 local $re5 = qr#(1)((??{ push @ctl_n, $f->($^N); push @plus, $f->($+); $^N + 1})){2}(?{$^N})#;
192 my $re6 = qr#(??{ push @ctl_n, $f->($^N); push @plus, $f->($+); $^N + 1})#;
193 my $re7 = qr#(??{ push @ctl_n, $f->($^N); push @plus, $f->($+); $^N + 1})#;
201 # Expected values of $^N
202 # Expected values of $+
203 # Expected values of $1, $2, $3, $4 and $5
207 qr#^(1)((??{ push @ctl_n, $f->($^N); push @plus, $f->($+); $^N + 1}))+(??{$^N})$#,
210 "\$1 = 1, \$2 = 3, \$3 = undef, \$4 = undef, \$5 = undef",
214 qr#^(1)((??{ push @ctl_n, $f->($^N); push @plus, $f->($+); $^N + 1}))+(abc|def|)?(??{$+})$#,
217 "\$1 = 1, \$2 = 3, \$3 = undef, \$4 = undef, \$5 = undef",
221 qr#^(1)((??{ push @ctl_n, $f->($^N); push @plus, $f->($+); $^N + 1}))+(|abc|def)?(??{$+})$#,
224 "\$1 = 1, \$2 = 3, \$3 = undef, \$4 = undef, \$5 = undef",
228 qr#^(1)((??{ push @ctl_n, $f->($^N); push @plus, $f->($+); $^N + 1}))+(abc|def|)?(??{$^N})$#,
231 "\$1 = 1, \$2 = 3, \$3 = undef, \$4 = undef, \$5 = undef",
235 qr#^(1)((??{ push @ctl_n, $f->($^N); push @plus, $f->($+); $^N + 1}))+(|abc|def)?(??{$^N})$#,
238 "\$1 = 1, \$2 = 3, \$3 = undef, \$4 = undef, \$5 = undef",
242 qr#^($re)(|a(b)c|def)(??{$^R})$#,
245 "\$1 = 123, \$2 = 1, \$3 = 3, \$4 = abc, \$5 = b",
252 "\$1 = 123abc3, \$2 = 1, \$3 = 3, \$4 = abc, \$5 = b",
259 "\$1 = 123abc3, \$2 = 1, \$3 = 3, \$4 = abc, \$5 = b",
263 qr#^(??{$re5})(|abc|def)(??{"$^R"})$#,
266 "\$1 = abc, \$2 = undef, \$3 = undef, \$4 = undef, \$5 = undef",
270 qr#^(??{$re5})(|a(b)c|def)(??{"$^R"})$#,
273 "\$1 = abc, \$2 = b, \$3 = undef, \$4 = undef, \$5 = undef",
277 qr#^((\d+)((??{push @ctl_n, $f->($^N); push @plus, $f->($+);$^N + 1}))((??{push @ctl_n, $f->($^N); push @plus, $f->($+);$^N + 1}))((??{push @ctl_n, $f->($^N); push @plus, $f->($+);$^N + 1})))$#,
278 "1234 123 12 1 2 3 1234",
279 "1234 123 12 1 2 3 4",
280 "\$1 = 1234, \$2 = 1, \$3 = 2, \$4 = 3, \$5 = 4",
284 qr#^(\d+)($re6)($re6)($re6)$re6(($re6)$re6)$#,
285 "1234556 123455 12345 1234 123 12 1 2 3 4 4 5 56",
286 "1234556 123455 12345 1234 123 12 1 2 3 4 4 5 5",
287 "\$1 = 1, \$2 = 2, \$3 = 3, \$4 = 4, \$5 = 56",
291 qr#^((??{$re8}))($re7)($re7)($re7)$re7($re7)($re7(\2))$#,
292 "12345562 1234556 123455 12345 1234 123 12 1 2 3 4 4 5 62",
293 "12345562 1234556 123455 12345 1234 123 12 1 2 3 4 4 5 2",
294 "\$1 = 1, \$2 = 2, \$3 = 3, \$4 = 4, \$5 = 5",
301 my $match = $test->[0] =~ $test->[1];
302 my $str = join(", ", '$1 = '.$f->($1), '$2 = '.$f->($2), '$3 = '.$f->($3), '$4 = '.$f->($4),'$5 = '.$f->($5));
303 push @ctl_n, $f->($^N);
304 push @plus, $f->($+);
305 ok($match, "match $c; Bug 56194");
307 # unset $str, @ctl_n and @plus
311 is("@ctl_n", $test->[2], "ctl_n $c; Bug 56194");
312 is("@plus", $test->[3], "plus $c; Bug 56194");
313 is($str, $test->[4], "str $c; Bug 56194");
317 skip "test segfaults on perl < 5.10", 4;
324 local $re4 = qr#(1)((??{push @ctl_n, $f->($^N); push @plus, $f->($+);$^N + 1})){2}(?{$^N})(|abc|def)(??{"$^R"})#;
326 my $match = "123abc3" =~ m/^(??{$re4})$/;
327 my $str = join(", ", '$1 = '.$f->($1), '$2 = '.$f->($2), '$3 = '.$f->($3), '$4 = '.$f->($4),'$5 = '.$f->($5),'$^R = '.$f->($^R));
328 push @ctl_n, $f->($^N);
329 push @plus, $f->($+);
330 ok($match, 'Bug 56194');
337 is("@ctl_n", "1 2 undef", 'Bug 56194');
338 is("@plus", "1 2 undef", 'Bug 56194');
340 "\$1 = undef, \$2 = undef, \$3 = undef, \$4 = undef, \$5 = undef, \$^R = undef",
345 } # End of sub run_tests