Commit | Line | Data |
---|---|---|
cde0cee5 YO |
1 | #!./perl |
2 | ||
3 | BEGIN { | |
4 | chdir 't' if -d 't'; | |
5 | @INC = '../lib'; | |
6 | require './test.pl'; | |
7 | } | |
8 | ||
9 | use strict; | |
10 | use warnings; | |
11 | ||
12 | our @tests = ( | |
f7819f85 A |
13 | # /p Pattern PRE MATCH POST |
14 | [ '/p', "456", "123-", "456", "-789"], | |
15 | [ '(?p)', "456", "123-", "456", "-789"], | |
16 | [ '', "(456)", "123-", "456", "-789"], | |
17 | [ '', "456", undef, undef, undef ], | |
cde0cee5 YO |
18 | ); |
19 | ||
20 | plan tests => 4 * @tests + 2; | |
21 | my $W = ""; | |
22 | ||
23 | $SIG{__WARN__} = sub { $W.=join("",@_); }; | |
24 | sub _u($$) { "$_[0] is ".(defined $_[1] ? "'$_[1]'" : "undef") } | |
25 | ||
26 | $_ = '123-456-789'; | |
27 | foreach my $test (@tests) { | |
87e95b7f | 28 | my ($p, $pat,$l,$m,$r) = @$test; |
f7819f85 A |
29 | my $test_name = $p eq '/p' ? "/$pat/p" |
30 | : $p eq '(?p)' ? "/(?p)$pat/" | |
31 | : "/$pat/"; | |
32 | ||
33 | # | |
34 | # Cannot use if/else due to the scope invalidating ${^MATCH} and friends. | |
35 | # | |
36 | my $ok = ok $p eq '/p' ? /$pat/p | |
37 | : $p eq '(?p)' ? /(?p)$pat/ | |
38 | : /$pat/ | |
39 | => $test_name; | |
cde0cee5 | 40 | SKIP: { |
87e95b7f | 41 | skip "/$pat/$p failed to match", 3 |
cde0cee5 YO |
42 | unless $ok; |
43 | is(${^PREMATCH}, $l,_u "$test_name: ^PREMATCH",$l); | |
44 | is(${^MATCH}, $m,_u "$test_name: ^MATCH",$m ); | |
45 | is(${^POSTMATCH}, $r,_u "$test_name: ^POSTMATCH",$r ); | |
46 | } | |
47 | } | |
48 | is($W,"","No warnings should be produced"); | |
87e95b7f | 49 | ok(!defined ${^MATCH}, "No /p in scope so ^MATCH is undef"); |