forked from bestpractical/path-dispatcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
001-api.t
57 lines (44 loc) · 1.53 KB
/
001-api.t
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
use strict;
use warnings;
use Test::More;
use Path::Dispatcher;
my @calls;
my $dispatcher = Path::Dispatcher->new;
$dispatcher->add_rule(
Path::Dispatcher::Rule::Regex->new(
regex => qr/foo/,
block => sub {
my $match = shift;
push @calls, [@_];
},
),
);
is_deeply([splice @calls], [], "no calls to the rule block yet");
my $dispatch = $dispatcher->dispatch('foo');
is_deeply([splice @calls], [], "no calls to the rule block yet");
isa_ok($dispatch, 'Path::Dispatcher::Dispatch');
$dispatch->run;
is_deeply([splice @calls], [ [] ], "finally invoked the rule block");
$dispatcher->run('foo');
is_deeply([splice @calls], [ [] ], "invoked the rule block on 'run'");
$dispatcher->add_rule(
Path::Dispatcher::Rule::Regex->new(
regex => qr/(bar)/,
block => sub {
my $match = shift;
push @calls, $match->positional_captures;
},
),
);
is_deeply([splice @calls], [], "no calls to the rule block yet");
$dispatch = $dispatcher->dispatch('bar');
is_deeply([splice @calls], [], "no calls to the rule block yet");
isa_ok($dispatch, 'Path::Dispatcher::Dispatch');
$dispatch->run;
is_deeply([splice @calls], [ ['bar'] ], "finally invoked the rule block");
$dispatcher->run('bar');
is_deeply([splice @calls], [ ['bar'] ], "invoked the rule block on 'run'");
isa_ok($dispatch, 'Path::Dispatcher::Dispatch');
$dispatch->run;
is_deeply([splice @calls], [ ['bar'] ], "invoked the rule block on 'run', makes sure ->pos etc are still correctly set");
done_testing;