File: 120_keep_pattern.t

package info (click to toggle)
libtest-regexp-perl 2017040101-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 220 kB
  • sloc: perl: 1,331; makefile: 2
file content (146 lines) | stat: -rwxr-xr-x 4,285 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/perl

use strict;
use warnings;
no  warnings 'syntax';


use 5.010;

use lib ".";

use Test::Tester;
use Test::Regexp;
use t::Common;

sub init_data;

my @data   = init_data;


my $seen;
foreach my $data (@data) {
    my ($subject, $pattern, $match, $expected, $captures) = @$data;

    my $match_val = $match =~ /[ym1]/i;
    my $match_res;
    my ($premature, @results) = run_tests sub {
        $match_res = match subject       =>  $subject,
                           keep_pattern  =>  $pattern,
                           match         =>  $match_val,
                           captures      =>  $captures,
    };

    check results   => \@results,
          premature => $premature,
          expected  => $expected,
          match     => $match_val,
          match_res => $match_res,
          pattern   => $pattern,
          subject   => $subject,
          keep      =>  1,
    ;

    if ($match_res && ! $seen ++) {
        my ($premature, @results) = run_tests sub {
            $match_res = match subject         =>  $subject,
                               keep_pattern    =>  $pattern,
                               no_keep_message => 1,
                               match           =>  $match_val,
                               captures        =>  $captures,
        };

        check results   => \@results,
              premature => $premature,
              expected  => $expected,
              match     => $match_val,
              match_res => $match_res,
              pattern   => $pattern,
              subject   => $subject,
        ;
    }
}

#
# Data taken from 'meta state_flowers'
#

sub init_data {(
    # Match without captures.
    ['Rose',              qr {\w+},                    'y', 'PPPP', []],

    # Match with just numbered captures.
    ['Black Eyed Susan',  qr {(\w+)\s+(\w+)\s+(\w+)},  'y', 'PPPPPPP',
      [qw [Black Eyed Susan]]],

    # Match with just named captures.
    ['Sego Lily',         qr {(?<a>\w+)\s+(?<b>\w+)},  'y', 'PPPPPPPPPP',
      [[a => 'Sego'], [b => 'Lily']]],

    # Mix named and numbered captures.
    ['California Poppy',  qr {(?<state>\w+)\s+(\w+)},  'y', 'PPPPPPPP',
      [[state => 'California'], 'Poppy']],

    # Repeat named capture.
    ['Indian Paintbrush', qr {(?<s>\w+)\s+(?<s>\w+)},  'y', 'PPPPPPPPP',
      [[s => 'Indian'], [s => 'Paintbrush']]],

    #
    # Failures.
    #

    # No captures, but a result.
    ['Violet',            qr {\w+},                    'y', 'PPPFF',
      ['Violet']],

    # Capture, no result.
    ['Mayflower',         qr {(\w+)},                  'y', 'PPPF', []],

    # Capture, wrong result.
    ['Magnolia',          qr {(\w+)},                  'y', 'PPPFP',
      ['Violet']],

    # Named capture, numbered results.
    ['Hawaiian Hibiscus', qr {(?<a>\w+)\s+(?<b>\w+)},  'y', 'PPFPPP',
      [qw [Hawaiian Hibiscus]]],

    # Numbered capture, named results.
    ['Cherokee Rose',     qr {(\w+)\s+(\w+)},          'y', 'PPFFFFFPPP',
      [[a => 'Cherokee'], [b => 'Rose']]],

    # Wrong capture names.
    ['American Dogwood',  qr {(?<a>\w+)\s+(?<b>\w+)},  'y', 'PPFPFPPPPP',
      [[b => 'American'], [a => 'Dogwood']]],

    # Wrong order of captures.
    ['Mountain Laurel',   qr {(?<a>\w+)\s+(?<b>\w+)},  'y', 'PPPPPPPFFP',
      [[b => 'Laurel'], [a => 'Mountain']]],

    # Wrong order of captures - same name
    ['Yucca Flower',      qr {(?<a>\w+)\s+(?<a>\w+)},  'y', 'PPFFPPFFP',
      [[a => 'Flower'], [a => 'Yucca']]],

    # Too many numbered captures.
    ['Sagebrush',         qr {(\w+)},                  'y', 'PPPPFF',
      [qw [Sagebrush Violet]]],

    # Too many named captures.
    ['Apple Blossom',     qr {(?<a>\w+)\s+(?<a>\w+)},  'y', 'PPPPFFPPPFF',
      [[a => 'Apple'], [a => 'Blossom'], [a => 'Violet']]],

    # Not enough named captures.
    ['Wood Violet',       qr {(?<a>\w+)\s+(?<a>\w+)},  'y', 'PPPFPPF',
      [[a => 'Wood']]],

    # Incomplete match
    ['Forget Me Not',     qr {(?<a>\w+)\s+(?<b>\w+)},  'y', 'PFSSSSSSSS',
      [[a => 'Forget'], [b => 'Me']]],

    # Incomplete match
    ['Forget Me Not 2',   qr {(?<a>\w+)\s+(?<b>\w+)\s+(?<c>\w+)},
                                                       'y', 'PFSSSSSSSSSSS',
      [[a => 'Forget'], [b => 'Me'], [c => 'Not']]],
)}


__END__