File: 031-structured-match.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 (47 lines) | stat: -rw-r--r-- 1,137 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
use strict;
use warnings;
use Test::More;
use Path::Dispatcher;

my $outer = Path::Dispatcher::Rule::Regex->new(
    regex  => qr/^(\w+) /,
    prefix => 1,
);

my $inner = Path::Dispatcher::Rule::Regex->new(
    regex => qr/^(\w+)/,
    block => sub { return shift }
);

my $dispatcher = Path::Dispatcher->new(
    rules => [
        Path::Dispatcher::Rule::Under->new(
            predicate => $outer,
            rules     => [ $inner ],
        ),
    ],
);

my $match = $dispatcher->run("hello world");
my $parent = $match->parent;

ok($parent, 'we have a parent too');
ok($match, 'matched');

is($parent->pos(1), 'hello', 'outer capture');
is($match->pos(1), 'world', 'inner capture');

is($parent->rule, $outer, 'outer rule');
is($match->rule, $inner, 'inner rule');

is_deeply($parent->positional_captures, ['hello'], 'all pos captures');
is_deeply($match->positional_captures, ['world'], 'all pos captures');

is($parent->path->path, 'hello world', 'outer path');
is($match->path->path, 'world', 'inner path');

is($parent->leftover, 'world', 'outer leftover');
is($match->leftover, undef, 'no inner leftover');

done_testing;