File: 024-sequence.t

package info (click to toggle)
libpath-dispatcher-perl 1.08-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 512 kB
  • sloc: perl: 1,046; makefile: 2
file content (130 lines) | stat: -rw-r--r-- 3,831 bytes parent folder | download | duplicates (3)
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
use strict;
use warnings;
use Test::More;
use Path::Dispatcher;

my @calls;

my $dispatcher = Path::Dispatcher->new;
$dispatcher->add_rule(
    Path::Dispatcher::Rule::Sequence->new(
        rules => [
            Path::Dispatcher::Rule::Eq->new(
                string => 'foo',
            ),
            Path::Dispatcher::Rule::Eq->new(
                string => 'bar',
            ),
        ],
        block  => sub { push @calls, shift->positional_captures },
    ),
);

$dispatcher->run('foo bar');
is_deeply([splice @calls], [ ['foo', 'bar'] ], "correctly populated number vars from [str, str] token rule");

$dispatcher->add_rule(
    Path::Dispatcher::Rule::Sequence->new(
        rules => [
            Path::Dispatcher::Rule::Eq->new(
                string => 'foo',
            ),
            Path::Dispatcher::Rule::Regex->new(
                regex => qr/bar/,
            ),
        ],
        block  => sub { push @calls, shift->positional_captures },
    ),
);

$dispatcher->run('foo bar');
is_deeply([splice @calls], [ ['foo', 'bar'] ], "ran the first [str, str] rule");

$dispatcher->run('foo barbaz');
is_deeply([splice @calls], [ ['foo', 'barbaz'] ], "ran the second [str, regex] rule");

$dispatcher->run('foo bar baz');
is_deeply([splice @calls], [ ], "no matches");

$dispatcher->add_rule(
    Path::Dispatcher::Rule::Sequence->new(
        rules => [
            Path::Dispatcher::Rule::Alternation->new(
                rules => [
                    Path::Dispatcher::Rule::Eq->new(
                        string => 'Bat',
                    ),
                    Path::Dispatcher::Rule::Eq->new(
                        string => 'Super',
                    ),
                ],
            ),
            Path::Dispatcher::Rule::Eq->new(
                string => 'Man',
            ),
        ],
        block => sub { push @calls, shift->positional_captures },
    ),
);

$dispatcher->run('Super Man');
is_deeply([splice @calls], [ ['Super', 'Man'] ], "ran the [ [Str,Str], Str ] rule");

$dispatcher->run('Bat Man');
is_deeply([splice @calls], [ ['Bat', 'Man'] ], "ran the [ [Str,Str], Str ] rule");

$dispatcher->run('Aqua Man');
is_deeply([splice @calls], [ ], "no match");

$dispatcher->add_rule(
    Path::Dispatcher::Rule::Sequence->new(
        rules => [
            Path::Dispatcher::Rule::Alternation->new(
                rules => [
                    Path::Dispatcher::Rule::Alternation->new(
                        rules => [
                            Path::Dispatcher::Rule::Alternation->new(
                                rules => [
                                    Path::Dispatcher::Rule::Regex->new(
                                        regex => qr/Deep/,
                                    ),
                                ],
                            ),
                        ],
                    ),
                ],
            ),
            Path::Dispatcher::Rule::Eq->new(
                string => "Man",
            ),
        ],
        block => sub { push @calls, shift->positional_captures },
    ),
);

$dispatcher->run('Deep Man');
is_deeply([splice @calls], [ ['Deep', 'Man'] ], "alternations can be arbitrarily deep");

$dispatcher->run('Not Appearing in this Dispatcher Man');
is_deeply([splice @calls], [ ], "no match");

my $rule = Path::Dispatcher::Rule::Sequence->new(
    rules => [
        Path::Dispatcher::Rule::Eq->new(
            string         => 'path',
            case_sensitive => 0,
        ),
        Path::Dispatcher::Rule::Eq->new(
            string         => 'dispatcher',
            case_sensitive => 0,
        ),
    ],
    prefix    => 1,
    delimiter => '::',
);

my $match = $rule->match(Path::Dispatcher::Path->new('Path::Dispatcher::Rule::Tokens'));
is($match->leftover, 'Rule::Tokens');

done_testing;