summaryrefslogtreecommitdiff
path: root/t/op/reg_pmod.t
blob: 301aeefc6df8c1cb4ea5c898e26057a8c9524cb7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!./perl

BEGIN {
    chdir 't' if -d 't';
    @INC = '../lib';
    require './test.pl';
}

use strict;
use warnings;

our @tests = (
    # /p      Pattern   PRE     MATCH   POST
    [ '/p',   "456",    "123-", "456",  "-789"],
    [ '(?p)', "456",    "123-", "456",  "-789"],
    [ '',     "(456)",  "123-", "456",  "-789"],
    [ '',     "456",    undef,  undef,  undef ],
);

plan tests => 4 * @tests + 2;
my $W = "";

$SIG{__WARN__} = sub { $W.=join("",@_); };
sub _u($$) { "$_[0] is ".(defined $_[1] ? "'$_[1]'" : "undef") }

$_ = '123-456-789';
foreach my $test (@tests) {
    my ($p, $pat,$l,$m,$r) = @$test;
    my $test_name = $p eq '/p'   ? "/$pat/p"
                  : $p eq '(?p)' ? "/(?p)$pat/"
                  :                "/$pat/";

    #
    # Cannot use if/else due to the scope invalidating ${^MATCH} and friends.
    #
    my $ok = ok $p eq '/p'   ? /$pat/p
              : $p eq '(?p)' ? /(?p)$pat/
              :                /$pat/
              => $test_name;
    SKIP: {
        skip "/$pat/$p failed to match", 3
            unless $ok;
        is(${^PREMATCH},  $l,_u "$test_name: ^PREMATCH",$l);
        is(${^MATCH},     $m,_u "$test_name: ^MATCH",$m );
        is(${^POSTMATCH}, $r,_u "$test_name: ^POSTMATCH",$r );
    }
}
is($W,"","No warnings should be produced");
ok(!defined ${^MATCH}, "No /p in scope so ^MATCH is undef");