File: 04_match.t

package info (click to toggle)
libregexp-assemble-perl 0.38-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 608 kB
  • sloc: perl: 2,272; makefile: 8
file content (191 lines) | stat: -rw-r--r-- 5,918 bytes parent folder | download | duplicates (7)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# 04_match.t
#
# Test suite for Regexp::Assemble
# Tests to see than an assembled regexp matches all that it is supposed to
#
# copyright (C) 2004-2006 David Landgren

use strict;
eval qq{
    use Test::More tests => 1381;
};
if( $@ ) {
    warn "# Test::More not available, no tests performed\n";
    print "1..1\nok 1\n";
    exit 0;
}

use Regexp::Assemble;

my $fixed = 'The scalar remains the same';
$_ = $fixed;

# Bug #17507 as noted by barbie
#
# There appears to be a problem with the substitute key on Windows, for
# at least Perl 5.6.1, which causes this test script to terminate
# immediately on encountering the character.
my $subchr    = 0x1a;
my $win32_56x = ($^O eq 'MSWin32' && $] < 5.008) ? 1 : 0;
diag("enabling defensive workaround for $] on $^O") if $win32_56x;

{
    for my $outer ( 0 .. 15 ) {
        my $re = Regexp::Assemble->new->anchor_string->chomp(0);
        for my $inner ( 0 .. 15 ) {
            next if $win32_56x and $subchr == ($outer*16 + $inner);
            $re->add( quotemeta( chr( $outer*16 + $inner )));
        }
        for my $inner ( 0 .. 15 ) {
            if( $win32_56x and $subchr == ($outer*16 + $inner)) {
                 ok( 1, "faking $subchr for 5.6 on Win32" );
            }
            else {
                my $ch = chr($outer*16 + $inner);
                like( $ch, qr/$re/, "run $ch ($outer:$inner) $re" );
            }
        }
    }
}

for( 0 .. 255 ) {
    if( $win32_56x and $subchr == $_) {
        pass("Fake a single for 5.6 on Win32");
        next;
    }
    my $ch = chr($_);
    my $qm = Regexp::Assemble->new(chomp=>0)->anchor_string->add(quotemeta($ch));
    like( $ch, qr/$qm/, "quotemeta(chr($_))" );
}

for( 0 .. 127 ) {
    if( $win32_56x and $subchr == $_) {
        pass( "Fake a hi for 5.6 on Win32");
        pass( "Fake a lo for 5.6 on Win32");
        next;
    }
    my $lo = chr($_);
    my $hi = chr($_+128);
    my $qm = Regexp::Assemble->new(chomp => 0, anchor_string => 1)->add(
        quotemeta($lo),
        quotemeta($hi),
    );
    like( $lo, qr/$qm/, "$_: quotemeta($lo) lo" );
    like( $hi, qr/$qm/, "$_: quotemeta($hi) hi" );
}

sub match {
    my $re   = Regexp::Assemble->new;
    my $rela = Regexp::Assemble->new->lookahead(1);
    my $tag = shift;
    $re->add(@_);
    $rela->add(@_);
    my $reind = $re->clone;
    $reind = $re->clone->flags('x')->re(indent => 3);
    my $rered = $re->clone->reduce(0);
    my $str;
    for $str (@_) {
        like( $str, qr/^$re$/,     "-- $tag: $str" ) or diag( " fail $str\n# match by $re\n" );
        like( $str, qr/^$rela$/,   "LA $tag: $str" ) or diag( " fail $str\n# match by lookahead $rela\n" );
        like( $str, qr/^$reind$/x, "IN $tag: $str" ) or diag( " fail $str\n# match by indented $reind\n" );
        like( $str, qr/^$rered$/,  "RD $tag: $str" ) or diag( " fail $str\n# match by non-reduced $rered\n" );
    }
}

sub match_list {
    my $tag  = shift;
    my $patt = shift;
    my $test = shift;
    my $re   = Regexp::Assemble->new->add(@$patt);
    my $rela = Regexp::Assemble->new->lookahead(1)->add(@$patt);
    my $str;
    for $str (@$test) {
        ok( $str =~ /^$re$/, "re $tag: $str" ) or diag( "fail re $str\n# in $re\n" );
        ok( $str =~ /^$rela$/, "rela $tag: $str" ) or diag( "fail rela $str\n# in $rela\n" );
    }
}

{
    my $re = Regexp::Assemble->new( flags => 'i' )
        ->add( '^fg' )
        ->re;
    like( 'fgx', qr/$re/, 'fgx/i' );
    like( 'Fgx', qr/$re/, 'Fgx/i' );
    like( 'FGx', qr/$re/, 'FGx/i' );
    like( 'fGx', qr/$re/, 'fGx/i' );
    unlike( 'F', qr/$re/, 'F/i' );
}

{
    my $re = Regexp::Assemble->new( flags => 'x' )
        ->add( '^fish' )
        ->add( '^flash' )
        ->add( '^fetish' )
        ->add( '^foolish' )
        ->re( indent => 2 );
    like( 'fish', qr/$re/, 'fish/x' );
    like( 'flash', qr/$re/, 'flash/x' );
    like( 'fetish', qr/$re/, 'fetish/x' );
    like( 'foolish', qr/$re/, 'foolish/x' );
    unlike( 'fetch', qr/$re/, 'fetch/x' );
}

match_list( 'lookahead car.*',
    [qw[caret caress careful careless caring carion carry carried]],
    [qw[caret caress careful careless caring carion carry carried]],
);

match_list( 'a.x', [qw[ abx adx a.x ]] , [qw[ aax abx acx azx a4x a%x a+x a?x ]] );

match_list( 'POSIX', [qw[ X[0[:alpha:]%] Y[1-4[:punct:]a-c] ]] , [qw(X0 X% Xa Xf Y1 Y; Y! yc)] );

match_list( 'c.z', [qw[ c^z c-z c5z cmz ]] , [qw[ c^z c-z c5z cmz ]] );

match_list( '\d, \D', [ 'b\\d', 'b\\D' ] , [qw[ b4 bX b% b. b? ]] );

match_list( 'abcd',
    [qw[ abc abcd ac acd b bc bcd bd]],
    [qw[ abc abcd ac acd b bc bcd bd]],
);

match( 'foo', qw[ foo bar rat quux ]);

match( '.[ar]it 1', qw[ bait brit frit gait grit tait wait writ ]);

match( '.[ar]it 2', qw[ bait brit gait grit ]);

match( '.[ar]it 3', qw[ bit bait brit gait grit ]);

match( '.[ar]it 4', qw[ barit bait brit gait grit ]);

match( 't.*ough', qw[ tough though trough through thorough ]);

match( 'g.*it', qw[ gait git grapefruit grassquit grit guitguit ]);

match( 'show.*ess', qw[ showeriness showerless showiness showless ]);

match( 'd*', qw[ den-at dot-at den-pt dot-pt dx ]);

match( 'd*', qw[ den-at dot-at den-pt dot-pt d-at d-pt dx ]);

match( 'un*ed', qw[ unimped unimpeded unimpelled ]);

match( '(un)?*(ing)?ing', qw[
    sing swing sting sling
    singing swinging stinging slinging
    unsing unswing unsting unsling
    unsinging unswinging unstinging unslinging
]);

match( 's.*at 1', qw[ sat sweat sailbat ]);

match( 'm[eant]+', qw[ ma mae man mana manatee mane manent manna mannan mant
    manta mat mate matta matte me mean meant meat meet meeten men met meta
    metate mete ]);

match( 'ti[aeinost]+', qw[ tiao tie tien tin tine tinea tinean tineine
    tininess tinnet tinniness tinosa tinstone tint tinta tintie tintiness
    tintist tisane tit titanate titania titanite titano tite titi titian
    titien tittie ]);

is( $_, $fixed, '$_ has not been altered' );