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','.');
20 do "re/ReTest.pl" or die $@;
24 plan tests => 123; # Update this when adding/deleting tests.
26 run_tests() unless caller;
33 local $Message = "Call code from qr //";
34 local $_ = 'var="foo"';
37 ok /$a$a/ && $b eq '9';
40 ok /$a$a/ && $b eq '11';
44 ok $@ && $@ =~ /not allowed at runtime/;
53 my $lex_res = ($lex_b =~ qr/$lex_b(?{ $lex_c = $lex_a++ })/);
61 my $match = eval { /$a$c$a/ };
62 ok $@ && $@ =~ /Eval-group not allowed/ && !$match;
69 $lex_res = ($lex_b =~ qr/17(?{ $lex_c = $lex_a++ })/);
78 our $a = bless qr /foo/ => 'Foo';
79 ok 'goodfood' =~ $a, "Reblessed qr // matches";
80 iseq $a, '(?^:foo)', "Reblessed qr // stringifies";
82 my $z = my $y = "\317\276"; # Byte representation of $x
84 ok $x =~ $a, "UTF-8 interpolation in qr //";
85 ok "a$a" =~ $x, "Stringified qr // preserves UTF-8";
86 ok "a$x" =~ /^a$a\z/, "Interpolated qr // preserves UTF-8";
87 ok "a$x" =~ /^a(??{$a})\z/,
88 "Postponed interpolation of qr // preserves UTF-8";
90 local $BugId = '17776';
91 iseq length qr /##/x, 9, "## in qr // doesn't corrupt memory";
95 ok "$x$x" =~ /^$x(??{$x})\z/,
96 "Postponed UTF-8 string in UTF-8 re matches UTF-8";
97 ok "$y$x" =~ /^$y(??{$x})\z/,
98 "Postponed UTF-8 string in non-UTF-8 re matches UTF-8";
99 ok "$y$x" !~ /^$y(??{$y})\z/,
100 "Postponed non-UTF-8 string in non-UTF-8 re doesn't match UTF-8";
101 ok "$x$x" !~ /^$x(??{$y})\z/,
102 "Postponed non-UTF-8 string in UTF-8 re doesn't match UTF-8";
103 ok "$y$y" =~ /^$y(??{$y})\z/,
104 "Postponed non-UTF-8 string in non-UTF-8 re matches non-UTF8";
105 ok "$x$y" =~ /^$x(??{$y})\z/,
106 "Postponed non-UTF-8 string in UTF-8 re matches non-UTF8";
108 $y = $z; # Reset $y after upgrade.
109 ok "$x$y" !~ /^$x(??{$x})\z/,
110 "Postponed UTF-8 string in UTF-8 re doesn't match non-UTF-8";
111 ok "$y$y" !~ /^$y(??{$x})\z/,
112 "Postponed UTF-8 string in non-UTF-8 re doesn't match non-UTF-8";
119 local $Message = 'Test if $^N and $+ work in (?{{})';
127 push @ctl_n, (defined $^N ? $^N : "undef");
128 push @plus, (defined $+ ? $+ : "undef");
139 # [ Expected result, Regex, Expected value(s) of $^N, Expected value(s) of $+ ]
140 [ 1, qr#^$nested_tags$#, "bla blubb bla", "a b a" ],
141 [ 1, qr#^($nested_tags)$#, "bla blubb <bla><blubb></blubb></bla>", "a b a" ],
142 [ 1, qr#^(|)$nested_tags$#, "bla blubb bla", "a b a" ],
143 [ 1, qr#^(?:|)$nested_tags$#, "bla blubb bla", "a b a" ],
144 [ 1, qr#^<(bl|bla)>$nested_tags<(/\1)>$#, "blubb /bla", "b /bla" ],
145 [ 1, qr#(??{"(|)"})$nested_tags$#, "bla blubb bla", "a b a" ],
146 [ 1, qr#^(??{"(bla|)"})$nested_tags$#, "bla blubb bla", "a b a" ],
147 [ 1, qr#^(??{"(|)"})(??{$nested_tags})$#, "bla blubb undef", "a b undef" ],
148 [ 1, qr#^(??{"(?:|)"})$nested_tags$#, "bla blubb bla", "a b a" ],
149 [ 1, qr#^((??{"(?:bla|)"}))((??{$nested_tags}))$#, "bla blubb <bla><blubb></blubb></bla>", "a b <bla><blubb></blubb></bla>" ],
150 [ 1, qr#^((??{"(?!)?"}))((??{$nested_tags}))$#, "bla blubb <bla><blubb></blubb></bla>", "a b <bla><blubb></blubb></bla>" ],
151 [ 1, qr#^((??{"(?:|<(/?bla)>)"}))((??{$nested_tags}))\1$#, "bla blubb <bla><blubb></blubb></bla>", "a b <bla><blubb></blubb></bla>" ],
152 [ 0, qr#^((??{"(?!)"}))?((??{$nested_tags}))(?!)$#, "bla blubb undef", "a b undef" ],
154 ) { #"#silence vim highlighting
158 my $match = (("<bla><blubb></blubb></bla>" =~ $test->[1]) ? 1 : 0);
159 push @ctl_n, (defined $^N ? $^N : "undef");
160 push @plus, (defined $+ ? $+ : "undef");
161 ok($test->[0] == $match, "match $c");
162 if ($test->[0] != $match) {
163 # unset @ctl_n and @plus
166 iseq("@ctl_n", $test->[2], "ctl_n $c");
167 iseq("@plus", $test->[3], "plus $c");
173 local $BugId = '56194';
178 defined $_[0] ? $_[0] : "undef";
181 ok("123" =~ m/^(\d)(((??{1 + $^N})))+$/);
186 my $re = qr#(1)((??{ push @ctl_n, $f->($^N); push @plus, $f->($+); $^N + 1}))*(?{$^N})#;
187 my $re2 = qr#(1)((??{ push @ctl_n, $f->($^N); push @plus, $f->($+); $^N + 1}))*(?{$^N})(|a(b)c|def)(??{"$^R"})#;
188 my $re3 = qr#(1)((??{ push @ctl_n, $f->($^N); push @plus, $f->($+); $^N + 1})){2}(?{$^N})(|a(b)c|def)(??{"$^R"})#;
190 local $re5 = qr#(1)((??{ push @ctl_n, $f->($^N); push @plus, $f->($+); $^N + 1})){2}(?{$^N})#;
191 my $re6 = qr#(??{ push @ctl_n, $f->($^N); push @plus, $f->($+); $^N + 1})#;
192 my $re7 = qr#(??{ push @ctl_n, $f->($^N); push @plus, $f->($+); $^N + 1})#;
200 # Expected values of $^N
201 # Expected values of $+
202 # Expected values of $1, $2, $3, $4 and $5
206 qr#^(1)((??{ push @ctl_n, $f->($^N); push @plus, $f->($+); $^N + 1}))+(??{$^N})$#,
209 "\$1 = 1, \$2 = 3, \$3 = undef, \$4 = undef, \$5 = undef",
213 qr#^(1)((??{ push @ctl_n, $f->($^N); push @plus, $f->($+); $^N + 1}))+(abc|def|)?(??{$+})$#,
216 "\$1 = 1, \$2 = 3, \$3 = undef, \$4 = undef, \$5 = undef",
220 qr#^(1)((??{ push @ctl_n, $f->($^N); push @plus, $f->($+); $^N + 1}))+(|abc|def)?(??{$+})$#,
223 "\$1 = 1, \$2 = 3, \$3 = undef, \$4 = undef, \$5 = undef",
227 qr#^(1)((??{ push @ctl_n, $f->($^N); push @plus, $f->($+); $^N + 1}))+(abc|def|)?(??{$^N})$#,
230 "\$1 = 1, \$2 = 3, \$3 = undef, \$4 = undef, \$5 = undef",
234 qr#^(1)((??{ push @ctl_n, $f->($^N); push @plus, $f->($+); $^N + 1}))+(|abc|def)?(??{$^N})$#,
237 "\$1 = 1, \$2 = 3, \$3 = undef, \$4 = undef, \$5 = undef",
241 qr#^($re)(|a(b)c|def)(??{$^R})$#,
244 "\$1 = 123, \$2 = 1, \$3 = 3, \$4 = abc, \$5 = b",
251 "\$1 = 123abc3, \$2 = 1, \$3 = 3, \$4 = abc, \$5 = b",
258 "\$1 = 123abc3, \$2 = 1, \$3 = 3, \$4 = abc, \$5 = b",
262 qr#^(??{$re5})(|abc|def)(??{"$^R"})$#,
265 "\$1 = abc, \$2 = undef, \$3 = undef, \$4 = undef, \$5 = undef",
269 qr#^(??{$re5})(|a(b)c|def)(??{"$^R"})$#,
272 "\$1 = abc, \$2 = b, \$3 = undef, \$4 = undef, \$5 = undef",
276 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})))$#,
277 "1234 123 12 1 2 3 1234",
278 "1234 123 12 1 2 3 4",
279 "\$1 = 1234, \$2 = 1, \$3 = 2, \$4 = 3, \$5 = 4",
283 qr#^(\d+)($re6)($re6)($re6)$re6(($re6)$re6)$#,
284 "1234556 123455 12345 1234 123 12 1 2 3 4 4 5 56",
285 "1234556 123455 12345 1234 123 12 1 2 3 4 4 5 5",
286 "\$1 = 1, \$2 = 2, \$3 = 3, \$4 = 4, \$5 = 56",
290 qr#^((??{$re8}))($re7)($re7)($re7)$re7($re7)($re7(\2))$#,
291 "12345562 1234556 123455 12345 1234 123 12 1 2 3 4 4 5 62",
292 "12345562 1234556 123455 12345 1234 123 12 1 2 3 4 4 5 2",
293 "\$1 = 1, \$2 = 2, \$3 = 3, \$4 = 4, \$5 = 5",
300 my $match = $test->[0] =~ $test->[1];
301 my $str = join(", ", '$1 = '.$f->($1), '$2 = '.$f->($2), '$3 = '.$f->($3), '$4 = '.$f->($4),'$5 = '.$f->($5));
302 push @ctl_n, $f->($^N);
303 push @plus, $f->($+);
304 ok($match, "match $c");
306 # unset $str, @ctl_n and @plus
310 iseq("@ctl_n", $test->[2], "ctl_n $c");
311 iseq("@plus", $test->[3], "plus $c");
312 iseq($str, $test->[4], "str $c");
316 skip "test segfaults on perl < 5.10", 4;
323 local $re4 = qr#(1)((??{push @ctl_n, $f->($^N); push @plus, $f->($+);$^N + 1})){2}(?{$^N})(|abc|def)(??{"$^R"})#;
325 my $match = "123abc3" =~ m/^(??{$re4})$/;
326 my $str = join(", ", '$1 = '.$f->($1), '$2 = '.$f->($2), '$3 = '.$f->($3), '$4 = '.$f->($4),'$5 = '.$f->($5),'$^R = '.$f->($^R));
327 push @ctl_n, $f->($^N);
328 push @plus, $f->($+);
336 iseq("@ctl_n", "1 2 undef");
337 iseq("@plus", "1 2 undef");
338 iseq($str, "\$1 = undef, \$2 = undef, \$3 = undef, \$4 = undef, \$5 = undef, \$^R = undef");
342 } # End of sub run_tests