Skip to content

Instantly share code, notes, and snippets.

@Getty
Created April 20, 2011 03:09
Show Gist options
  • Select an option

  • Save Getty/930262 to your computer and use it in GitHub Desktop.

Select an option

Save Getty/930262 to your computer and use it in GitHub Desktop.
use strict;
use warnings;
use Path::Dispatcher;
use Test::More;
my @calls;
my $dispatcher = Path::Dispatcher->new(
rules => [
Path::Dispatcher::Rule::Under->new(
predicate => Path::Dispatcher::Rule::Tokens->new(
tokens => ['twitter'],
prefix => 1,
),
rules => [
Path::Dispatcher::Rule::Chain->new(
block => sub { push @calls, 'twitter_base' },
),
Path::Dispatcher::Rule::Tokens->new(
tokens => ['add'],
block => sub { push @calls, 'twitter_add' },
),
Path::Dispatcher::Rule::Under->new(
predicate => Path::Dispatcher::Rule::Tokens->new(
tokens => [qr/.+/],
prefix => 1,
# under_block => sub { push @calls, 'twitter_account:'.shift->pos(1) } }, <suggestion>
),
rules => [
Path::Dispatcher::Rule::Chain->new(
block => sub { push @calls, 'twitter_account:' },
# this block actually needs the match from the predicate Path::Dispatcher::Rule::Tokens
# see suggestion above
),
Path::Dispatcher::Rule::Tokens->new(
tokens => ['update',qr/.+/],
block => sub { push @calls, 'twitter_update:'.(shift->pos(2)) },
# here it should be shift->pos(2<), cause i want all following arguments
),
Path::Dispatcher::Rule::Tokens->new(
tokens => ['command'],
block => sub { push @calls, 'twitter_command' },
),
],
),
],
),
],
);
$dispatcher->run("twitter add");
is_deeply([splice @calls], [ 'twitter_base', 'twitter_add' ]);
$dispatcher->run("twitter add update");
is_deeply([splice @calls], []);
$dispatcher->run("twitter add raudssus update test tweet!");
is_deeply([splice @calls], []);
$dispatcher->run("twitter raudssus test tweet!");
is_deeply([splice @calls], []);
$dispatcher->run("twitter raudssus update onewordeasy");
is_deeply([splice @calls], [ 'twitter_base', 'twitter_account:raudssus', 'twitter_update:onewordeasy' ]);
$dispatcher->run("twitter raudssus update My Test Tweet!!!");
is_deeply([splice @calls], [ 'twitter_base', 'twitter_account:raudssus', 'twitter_update:My Test Tweet!!!' ]);
$dispatcher->run("twitter raudssus update");
is_deeply([splice @calls], []);
$dispatcher->run("twitter raudssus command");
is_deeply([splice @calls], [ 'twitter_base', 'twitter_account:raudssus', 'twitter_command' ]);
done_testing;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment