[Bps-public-commit] Path-Dispatcher branch, pass-match, created. fab0d44046ec5f9534eed1dd2c0a4f75fb02788d
Shawn Moore
sartak at bestpractical.com
Sat Aug 21 20:38:45 EDT 2010
The branch, pass-match has been created
at fab0d44046ec5f9534eed1dd2c0a4f75fb02788d (commit)
- Log -----------------------------------------------------------------
commit eee4d854ec7b93a9fdf853e92fdad7cda6f0372a
Author: Shawn M Moore <sartak at bestpractical.com>
Date: Sat Aug 21 20:02:35 2010 -0400
Remove set_number_vars option
diff --git a/lib/Path/Dispatcher/Match.pm b/lib/Path/Dispatcher/Match.pm
index 06be61d..f8cec59 100644
--- a/lib/Path/Dispatcher/Match.pm
+++ b/lib/Path/Dispatcher/Match.pm
@@ -25,27 +25,16 @@ has result => (
is => 'rw',
);
-has set_number_vars => (
- is => 'rw',
- isa => 'Bool',
- lazy => 1,
- default => sub { ref(shift->result) eq 'ARRAY' },
-);
-
sub run {
my $self = shift;
my @args = @_;
local $_ = $self->path;
- if ($self->set_number_vars) {
- return $self->run_with_number_vars(
- sub { $self->rule->run(@args) },
- @{ $self->result },
- );
- }
-
- return $self->rule->run(@args);
+ return $self->run_with_number_vars(
+ sub { $self->rule->run(@args) },
+ @{ $self->result },
+ );
}
sub run_with_number_vars {
@@ -88,7 +77,6 @@ Path::Dispatcher::Match - the result of a successful rule match
$match->leftover # empty string (populated with prefix rules)
$match->rule # $rule
$match->result # ["attack", "dragon"] (decided by the rule)
- $match->set_number_vars # 1 (boolean indicating whether to set $1, $2, etc)
$match->run # causes the player to attack the dragon
$match->run_with_number_vars($code) # runs $code with $1=attack $2=dragon
@@ -117,19 +105,11 @@ path.
Arbitrary results generated by the rule. For example, L<Path::Dispatcher::Rule::Regex> rules' result is an array reference of capture variables.
-=head2 set_number_vars
-
-A boolean indicating whether invoking the rule should populate the number variables (C<$1>, C<$2>, etc) with the array reference of results.
-
-Default is true if the C<result> is an array reference; otherwise false.
-
=head1 METHODS
=head2 run
-Executes the rule's codeblock with the same arguments. If L</set_number_vars>
-is true, then L</run_with_number_vars> is used, otherwise the rule's codeblock
-is invoked directly.
+Executes the rule's codeblock with the same arguments.
=head2 run_with_number_vars coderef, $1, $2, ...
commit 174752cf57ef180e86dd4c8f07daf05083eb5eef
Author: Shawn M Moore <sartak at bestpractical.com>
Date: Sat Aug 21 20:06:39 2010 -0400
Temporary refactoring patch
diff --git a/lib/Path/Dispatcher/Match.pm b/lib/Path/Dispatcher/Match.pm
index f8cec59..4b40ed3 100644
--- a/lib/Path/Dispatcher/Match.pm
+++ b/lib/Path/Dispatcher/Match.pm
@@ -31,9 +31,14 @@ sub run {
local $_ = $self->path;
+ my @number_vars;
+ if (ref($self->result) eq 'ARRAY') {
+ @number_vars = @{ $self->result };
+ }
+
return $self->run_with_number_vars(
sub { $self->rule->run(@args) },
- @{ $self->result },
+ @number_vars,
);
}
commit 10cfbe80a9a4e9e728e114786f6a5162a401c6b5
Author: Shawn M Moore <sartak at bestpractical.com>
Date: Sat Aug 21 20:10:30 2010 -0400
Pass match object into rule blocks instead number vars (wip)
diff --git a/lib/Path/Dispatcher/Match.pm b/lib/Path/Dispatcher/Match.pm
index 4b40ed3..2bd9213 100644
--- a/lib/Path/Dispatcher/Match.pm
+++ b/lib/Path/Dispatcher/Match.pm
@@ -21,41 +21,11 @@ has rule => (
required => 1,
);
-has result => (
- is => 'rw',
-);
-
sub run {
my $self = shift;
- my @args = @_;
local $_ = $self->path;
-
- my @number_vars;
- if (ref($self->result) eq 'ARRAY') {
- @number_vars = @{ $self->result };
- }
-
- return $self->run_with_number_vars(
- sub { $self->rule->run(@args) },
- @number_vars,
- );
-}
-
-sub run_with_number_vars {
- my $self = shift;
- my $code = shift;
-
- # clear $1, $2, $3 so they don't pollute the number vars for the block
- "x" =~ /x/;
-
- # populate $1, $2, etc for the duration of $code
- # it'd be nice if we could use "local" but it seems to break tests
- my $i = 0;
- no strict 'refs';
- *{ ++$i } = \$_ for @_;
-
- $code->();
+ return $self->rule->run($self, @_);
}
__PACKAGE__->meta->make_immutable;
commit 2d9f17cd704d6e091df429f11b9a9d16a018cb0d
Author: Shawn M Moore <sartak at bestpractical.com>
Date: Sat Aug 21 20:13:24 2010 -0400
Be more specific: positional_captures not result
diff --git a/lib/Path/Dispatcher/Match.pm b/lib/Path/Dispatcher/Match.pm
index 2bd9213..2e9a309 100644
--- a/lib/Path/Dispatcher/Match.pm
+++ b/lib/Path/Dispatcher/Match.pm
@@ -21,6 +21,12 @@ has rule => (
required => 1,
);
+has positional_captures => (
+ is => 'rw',
+ isa => 'ArrayRef[Str]',
+ default => sub { [] },
+);
+
sub run {
my $self = shift;
diff --git a/lib/Path/Dispatcher/Rule.pm b/lib/Path/Dispatcher/Rule.pm
index 5e384cb..9164897 100644
--- a/lib/Path/Dispatcher/Rule.pm
+++ b/lib/Path/Dispatcher/Rule.pm
@@ -58,7 +58,7 @@ sub match {
my $match = $self->match_class->new(
path => $path,
rule => $self,
- result => $result,
+ positional_captures => $result,
leftover => $leftover,
);
commit b14aca96b503d8eba4f4aa353338379855a120a2
Author: Shawn M Moore <sartak at bestpractical.com>
Date: Sat Aug 21 20:13:36 2010 -0400
Fix up first test
diff --git a/t/001-api.t b/t/001-api.t
index 574f960..c1b83ba 100644
--- a/t/001-api.t
+++ b/t/001-api.t
@@ -10,7 +10,10 @@ my $dispatcher = Path::Dispatcher->new;
$dispatcher->add_rule(
Path::Dispatcher::Rule::Regex->new(
regex => qr/foo/,
- block => sub { push @calls, [@_] },
+ block => sub {
+ my $match = shift;
+ push @calls, [@_];
+ },
),
);
@@ -29,7 +32,10 @@ is_deeply([splice @calls], [ [] ], "invoked the rule block on 'run'");
$dispatcher->add_rule(
Path::Dispatcher::Rule::Regex->new(
regex => qr/(bar)/,
- block => sub { push @calls, [$1, $2] },
+ block => sub {
+ my $match = shift;
+ push @calls, $match->positional_captures;
+ },
),
);
@@ -40,14 +46,12 @@ is_deeply([splice @calls], [], "no calls to the rule block yet");
isa_ok($dispatch, 'Path::Dispatcher::Dispatch');
$dispatch->run;
-is_deeply([splice @calls], [ ['bar', undef] ], "finally invoked the rule block");
+is_deeply([splice @calls], [ ['bar'] ], "finally invoked the rule block");
$dispatcher->run('bar');
-is_deeply([splice @calls], [ ['bar', undef] ], "invoked the rule block on 'run'");
-
-"foo" =~ /foo/;
+is_deeply([splice @calls], [ ['bar'] ], "invoked the rule block on 'run'");
isa_ok($dispatch, 'Path::Dispatcher::Dispatch');
$dispatch->run;
-is_deeply([splice @calls], [ ['bar', undef] ], "invoked the rule block on 'run', makes sure \$1 etc are still correctly set");
+is_deeply([splice @calls], [ ['bar'] ], "invoked the rule block on 'run', makes sure \$1 etc are still correctly set");
commit 153ecb565f7051e3f5a561a490af5e55c8f63dec
Author: Shawn M Moore <sartak at bestpractical.com>
Date: Sat Aug 21 20:14:00 2010 -0400
Fix up second test file
diff --git a/t/002-rule.t b/t/002-rule.t
index 3bc94e7..6c4e890 100644
--- a/t/002-rule.t
+++ b/t/002-rule.t
@@ -1,7 +1,7 @@
#!/usr/bin/env perl
use strict;
use warnings;
-use Test::More tests => 5;
+use Test::More tests => 4;
use Path::Dispatcher::Rule;
my @calls;
@@ -17,7 +17,7 @@ my $rule = Path::Dispatcher::Rule::Regex->new(
);
isa_ok($rule->match(Path::Dispatcher::Path->new('foobar')), 'Path::Dispatcher::Match');
-is_deeply($rule->match(Path::Dispatcher::Path->new('foobar'))->result, ['fo', 'ob']);
+is_deeply($rule->match(Path::Dispatcher::Path->new('foobar'))->positional_captures, ['fo', 'ob']);
is_deeply([splice @calls], [], "block not called on match");
$rule->run;
@@ -26,12 +26,3 @@ is_deeply([splice @calls], [{
args => [],
}], "block called on ->run");
-# make sure ->run grabs $1
-"bah" =~ /^(\w+)/;
-
-$rule->run;
-is_deeply([splice @calls], [{
- vars => ["bah", undef, undef],
- args => [],
-}], "block called on ->run");
-
commit 9e9dbbb657a25b06676c3cfffd848d5edf208aa4
Author: Shawn M Moore <sartak at bestpractical.com>
Date: Sat Aug 21 20:16:42 2010 -0400
Validate positional_captures
diff --git a/lib/Path/Dispatcher/Rule.pm b/lib/Path/Dispatcher/Rule.pm
index 9164897..2735b46 100644
--- a/lib/Path/Dispatcher/Rule.pm
+++ b/lib/Path/Dispatcher/Rule.pm
@@ -27,16 +27,16 @@ sub match {
my $self = shift;
my $path = shift;
- my ($result, $leftover);
+ my ($positional_captures, $leftover);
if ($self->prefix) {
- ($result, $leftover) = $self->_prefix_match($path);
+ ($positional_captures, $leftover) = $self->_prefix_match($path);
}
else {
- ($result, $leftover) = $self->_match($path);
+ ($positional_captures, $leftover) = $self->_match($path);
}
- if (!$result) {
+ if (!$positional_captures) {
$self->trace(leftover => $leftover, match => undef, path => $path)
if $ENV{'PATH_DISPATCHER_TRACE'};
return;
@@ -48,17 +48,19 @@ sub match {
# later we will stick them into a regular expression to populate $1 etc
# which will blow up later!
- if (ref($result) eq 'ARRAY') {
- for (@$result) {
- die "Invalid result '$_', results must be plain strings"
- if ref($_);
- }
+ if (ref($positional_captures) ne 'ARRAY') {
+ die "Invalid result '$_', the positional captures must be an array reference";
+ }
+
+ for (@$positional_captures) {
+ die "Invalid result '$_', results must be plain strings"
+ if ref($_);
}
my $match = $self->match_class->new(
path => $path,
rule => $self,
- positional_captures => $result,
+ positional_captures => $positional_captures,
leftover => $leftover,
);
commit 7bed2ae64648597890d203c598d2d29cfe419caa
Author: Shawn M Moore <sartak at bestpractical.com>
Date: Sat Aug 21 20:17:15 2010 -0400
Default positional_captures to []
diff --git a/lib/Path/Dispatcher/Rule.pm b/lib/Path/Dispatcher/Rule.pm
index 2735b46..b3f322c 100644
--- a/lib/Path/Dispatcher/Rule.pm
+++ b/lib/Path/Dispatcher/Rule.pm
@@ -43,10 +43,7 @@ sub match {
}
$leftover = '' if !defined($leftover);
-
- # make sure that the returned values are PLAIN STRINGS
- # later we will stick them into a regular expression to populate $1 etc
- # which will blow up later!
+ $positional_captures = [] if !defined($positional_captures);
if (ref($positional_captures) ne 'ARRAY') {
die "Invalid result '$_', the positional captures must be an array reference";
commit be03c6fb5fc8e792842728ca011a9faf021f0fde
Author: Shawn M Moore <sartak at bestpractical.com>
Date: Sat Aug 21 20:32:54 2010 -0400
Stronger API for returning results from matching
diff --git a/lib/Path/Dispatcher/Rule.pm b/lib/Path/Dispatcher/Rule.pm
index b3f322c..1a8ba1d 100644
--- a/lib/Path/Dispatcher/Rule.pm
+++ b/lib/Path/Dispatcher/Rule.pm
@@ -27,38 +27,21 @@ sub match {
my $self = shift;
my $path = shift;
- my ($positional_captures, $leftover);
+ my $result;
if ($self->prefix) {
- ($positional_captures, $leftover) = $self->_prefix_match($path);
+ $result = $self->_prefix_match($path);
}
else {
- ($positional_captures, $leftover) = $self->_match($path);
+ $result = $self->_match($path);
}
- if (!$positional_captures) {
- $self->trace(leftover => $leftover, match => undef, path => $path)
- if $ENV{'PATH_DISPATCHER_TRACE'};
- return;
- }
-
- $leftover = '' if !defined($leftover);
- $positional_captures = [] if !defined($positional_captures);
-
- if (ref($positional_captures) ne 'ARRAY') {
- die "Invalid result '$_', the positional captures must be an array reference";
- }
-
- for (@$positional_captures) {
- die "Invalid result '$_', results must be plain strings"
- if ref($_);
- }
+ return if !$result;
my $match = $self->match_class->new(
path => $path,
rule => $self,
- positional_captures => $positional_captures,
- leftover => $leftover,
+ %$result,
);
$self->trace(match => $match) if $ENV{'PATH_DISPATCHER_TRACE'};
diff --git a/lib/Path/Dispatcher/Rule/Alternation.pm b/lib/Path/Dispatcher/Rule/Alternation.pm
index d329a82..b58d01d 100644
--- a/lib/Path/Dispatcher/Rule/Alternation.pm
+++ b/lib/Path/Dispatcher/Rule/Alternation.pm
@@ -9,13 +9,13 @@ sub _match {
my $path = shift;
my @rules = $self->rules;
- return 0 if @rules == 0;
+ return if @rules == 0;
for my $rule (@rules) {
- return 1 if $rule->match($path);
+ return {} if $rule->match($path);
}
- return 0;
+ return;
}
sub complete {
diff --git a/lib/Path/Dispatcher/Rule/Always.pm b/lib/Path/Dispatcher/Rule/Always.pm
index 8828f9e..8c567ad 100644
--- a/lib/Path/Dispatcher/Rule/Always.pm
+++ b/lib/Path/Dispatcher/Rule/Always.pm
@@ -5,7 +5,10 @@ extends 'Path::Dispatcher::Rule';
sub _match {
my $self = shift;
my $path = shift;
- return (1, $path->path);
+
+ return {
+ leftover => $path->path,
+ };
}
__PACKAGE__->meta->make_immutable;
diff --git a/lib/Path/Dispatcher/Rule/Empty.pm b/lib/Path/Dispatcher/Rule/Empty.pm
index a026139..30a9020 100644
--- a/lib/Path/Dispatcher/Rule/Empty.pm
+++ b/lib/Path/Dispatcher/Rule/Empty.pm
@@ -5,8 +5,8 @@ extends 'Path::Dispatcher::Rule';
sub _match {
my $self = shift;
my $path = shift;
- return 0 if length $path->path;
- return (1, $path->path);
+ return if length $path->path;
+ return { leftover => $path->path };
}
__PACKAGE__->meta->make_immutable;
diff --git a/lib/Path/Dispatcher/Rule/Enum.pm b/lib/Path/Dispatcher/Rule/Enum.pm
index e88dc09..dd20ea4 100644
--- a/lib/Path/Dispatcher/Rule/Enum.pm
+++ b/lib/Path/Dispatcher/Rule/Enum.pm
@@ -20,14 +20,16 @@ sub _match {
if ($self->case_sensitive) {
for my $value (@{ $self->enum }) {
- return 1 if $path->path eq $value;
+ return {} if $path->path eq $value;
}
}
else {
for my $value (@{ $self->enum }) {
- return 1 if lc($path->path) eq lc($value);
+ return {} if lc($path->path) eq lc($value);
}
}
+
+ return;
}
sub _prefix_match {
@@ -38,16 +40,24 @@ sub _prefix_match {
if ($self->case_sensitive) {
for my $value (@{ $self->enum }) {
- return (1, substr($path->path, length($self->string)))
- if $truncated eq $value;
+ next unless $truncated eq $value;
+
+ return {
+ leftover => substr($path->path, length($self->string)),
+ };
}
}
else {
for my $value (@{ $self->enum }) {
- return (1, substr($path->path, length($self->string)))
- if lc($truncated) eq lc($value);
+ next unless lc($truncated) eq lc($value);
+
+ return {
+ leftover => substr($path->path, length($self->string)),
+ };
}
}
+
+ return;
}
sub complete {
diff --git a/lib/Path/Dispatcher/Rule/Eq.pm b/lib/Path/Dispatcher/Rule/Eq.pm
index 96870d4..e0a7554 100644
--- a/lib/Path/Dispatcher/Rule/Eq.pm
+++ b/lib/Path/Dispatcher/Rule/Eq.pm
@@ -19,11 +19,13 @@ sub _match {
my $path = shift;
if ($self->case_sensitive) {
- return $path->path eq $self->string;
+ return unless $path->path eq $self->string;
}
else {
- return lc($path->path) eq lc($self->string);
+ return unless lc($path->path) eq lc($self->string);
}
+
+ return {};
}
sub _prefix_match {
@@ -33,13 +35,15 @@ sub _prefix_match {
my $truncated = substr($path->path, 0, length($self->string));
if ($self->case_sensitive) {
- return 0 unless $truncated eq $self->string;
+ return unless $truncated eq $self->string;
}
else {
- return 0 unless lc($truncated) eq lc($self->string);
+ return unless lc($truncated) eq lc($self->string);
}
- return (1, substr($path->path, length($self->string)));
+ return {
+ leftover => substr($path->path, length($self->string)),
+ };
}
sub complete {
diff --git a/lib/Path/Dispatcher/Rule/Intersection.pm b/lib/Path/Dispatcher/Rule/Intersection.pm
index a1f6a99..29e82fc 100644
--- a/lib/Path/Dispatcher/Rule/Intersection.pm
+++ b/lib/Path/Dispatcher/Rule/Intersection.pm
@@ -9,13 +9,13 @@ sub _match {
my $path = shift;
my @rules = $self->rules;
- return 0 if @rules == 0;
+ return if @rules == 0;
for my $rule (@rules) {
- return 0 unless $rule->match($path);
+ return unless $rule->match($path);
}
- return 1;
+ return {};
}
__PACKAGE__->meta->make_immutable;
diff --git a/lib/Path/Dispatcher/Rule/Metadata.pm b/lib/Path/Dispatcher/Rule/Metadata.pm
index 357ceff..e25b47b 100644
--- a/lib/Path/Dispatcher/Rule/Metadata.pm
+++ b/lib/Path/Dispatcher/Rule/Metadata.pm
@@ -21,9 +21,11 @@ sub _match {
# wow, offensive.. but powerful
my $metadata_path = $path->clone_path($got);
- return 0 unless $self->matcher->match($metadata_path);
+ return unless $self->matcher->match($metadata_path);
- return 1, $path->path;
+ return {
+ leftover => $path->path,
+ };
}
sub readable_attributes {
diff --git a/lib/Path/Dispatcher/Rule/Regex.pm b/lib/Path/Dispatcher/Rule/Regex.pm
index baec99a..cc7770c 100644
--- a/lib/Path/Dispatcher/Rule/Regex.pm
+++ b/lib/Path/Dispatcher/Rule/Regex.pm
@@ -16,11 +16,10 @@ sub _match {
# if $' is in the program at all, then it slows down every single regex
# we only want to include it if we have to
- if ($self->prefix) {
- return \@matches, eval q{$'};
+ return {
+ positional_captures => \@matches,
+ ($self->prefix ? (leftover => eval q{$'}) : ()),
}
-
- return \@matches;
}
sub readable_attributes { shift->regex }
diff --git a/lib/Path/Dispatcher/Rule/Sequence.pm b/lib/Path/Dispatcher/Rule/Sequence.pm
index 7cdc213..c50ecf0 100644
--- a/lib/Path/Dispatcher/Rule/Sequence.pm
+++ b/lib/Path/Dispatcher/Rule/Sequence.pm
@@ -42,7 +42,10 @@ sub _match {
return if @$tokens && !$self->prefix; # had tokens left over
my $leftover = $self->untokenize(@$tokens);
- return $matched, $leftover;
+ return {
+ leftover => $self->untokenize(@$tokens),
+ positional_captures => $matched,
+ };
}
sub complete {
diff --git a/lib/Path/Dispatcher/Rule/Tokens.pm b/lib/Path/Dispatcher/Rule/Tokens.pm
index dfbd05d..6e01f57 100644
--- a/lib/Path/Dispatcher/Rule/Tokens.pm
+++ b/lib/Path/Dispatcher/Rule/Tokens.pm
@@ -53,7 +53,13 @@ sub _match {
return if @$got && !$self->prefix; # had tokens left over
my $leftover = $self->untokenize(@$got);
- return $matched, $leftover;
+
+ return if !$matched;
+
+ return {
+ positional_captures => $matched,
+ leftover => $leftover,
+ };
}
sub complete {
diff --git a/t/007-coderef-matcher.t b/t/007-coderef-matcher.t
index 8823c9a..80779ff 100644
--- a/t/007-coderef-matcher.t
+++ b/t/007-coderef-matcher.t
@@ -9,8 +9,8 @@ my (@matches, @calls);
my $dispatcher = Path::Dispatcher->new;
$dispatcher->add_rule(
Path::Dispatcher::Rule::CodeRef->new(
- matcher => sub { push @matches, $_; length > 5 },
- block => sub { push @calls, [@_] },
+ matcher => sub { push @matches, $_; length > 5 ? {} : 0 },
+ block => sub { my $match = shift; push @calls, [@_] },
),
);
commit da1eacb0a28183d34204d5e318fecf3941a03482
Author: Shawn M Moore <sartak at bestpractical.com>
Date: Sat Aug 21 20:34:33 2010 -0400
shift off the match
diff --git a/t/009-args.t b/t/009-args.t
index 2a3f794..7473e13 100644
--- a/t/009-args.t
+++ b/t/009-args.t
@@ -10,7 +10,7 @@ my $dispatcher = Path::Dispatcher->new;
$dispatcher->add_rule(
Path::Dispatcher::Rule::Regex->new(
regex => qr/foo/,
- block => sub { push @calls, [@_] },
+ block => sub { my $match = shift; push @calls, [@_] },
),
);
commit 8f6a3be524e979af975016954cc6a4e4d5d504a1
Author: Shawn M Moore <sartak at bestpractical.com>
Date: Sat Aug 21 20:34:38 2010 -0400
$match->positional_captures not [$1, $2, $3]
diff --git a/t/013-tokens.t b/t/013-tokens.t
index c3815c7..fb535b1 100644
--- a/t/013-tokens.t
+++ b/t/013-tokens.t
@@ -10,25 +10,25 @@ my $dispatcher = Path::Dispatcher->new;
$dispatcher->add_rule(
Path::Dispatcher::Rule::Tokens->new(
tokens => ['foo', 'bar'],
- block => sub { push @calls, [$1, $2, $3] },
+ block => sub { push @calls, shift->positional_captures },
),
);
$dispatcher->run('foo bar');
-is_deeply([splice @calls], [ ['foo', 'bar', undef] ], "correctly populated number vars from [str, str] token rule");
+is_deeply([splice @calls], [ ['foo', 'bar'] ], "correctly populated number vars from [str, str] token rule");
$dispatcher->add_rule(
Path::Dispatcher::Rule::Tokens->new(
tokens => ['foo', qr/bar/],
- block => sub { push @calls, [$1, $2, $3] },
+ block => sub { push @calls, shift->positional_captures },
),
);
$dispatcher->run('foo bar');
-is_deeply([splice @calls], [ ['foo', 'bar', undef] ], "ran the first [str, str] rule");
+is_deeply([splice @calls], [ ['foo', 'bar'] ], "ran the first [str, str] rule");
$dispatcher->run('foo barbaz');
-is_deeply([splice @calls], [ ['foo', 'barbaz', undef] ], "ran the second [str, regex] rule");
+is_deeply([splice @calls], [ ['foo', 'barbaz'] ], "ran the second [str, regex] rule");
$dispatcher->run('foo bar baz');
is_deeply([splice @calls], [], "no matches");
@@ -36,15 +36,15 @@ is_deeply([splice @calls], [], "no matches");
$dispatcher->add_rule(
Path::Dispatcher::Rule::Tokens->new(
tokens => [["Bat", "Super"], "Man"],
- block => sub { push @calls, [$1, $2, $3] },
+ block => sub { push @calls, shift->positional_captures },
),
);
$dispatcher->run('Super Man');
-is_deeply([splice @calls], [ ['Super', 'Man', undef] ], "ran the [ [Str,Str], Str ] rule");
+is_deeply([splice @calls], [ ['Super', 'Man'] ], "ran the [ [Str,Str], Str ] rule");
$dispatcher->run('Bat Man');
-is_deeply([splice @calls], [ ['Bat', 'Man', undef] ], "ran the [ [Str,Str], Str ] rule");
+is_deeply([splice @calls], [ ['Bat', 'Man'] ], "ran the [ [Str,Str], Str ] rule");
$dispatcher->run('Aqua Man');
is_deeply([splice @calls], [ ], "no match");
@@ -52,12 +52,12 @@ is_deeply([splice @calls], [ ], "no match");
$dispatcher->add_rule(
Path::Dispatcher::Rule::Tokens->new(
tokens => [[[[qr/Deep/]]], "Man"],
- block => sub { push @calls, [$1, $2, $3] },
+ block => sub { push @calls, shift->positional_captures },
),
);
$dispatcher->run('Deep Man');
-is_deeply([splice @calls], [ ['Deep', 'Man', undef] ], "alternations can be arbitrarily deep");
+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");
commit c543bdedf3752d2469595efc0da177323fe7c5b7
Author: Shawn M Moore <sartak at bestpractical.com>
Date: Sat Aug 21 20:34:51 2010 -0400
test is obsolete
diff --git a/t/903-number-vars.t b/t/903-number-vars.t
deleted file mode 100644
index f08791e..0000000
--- a/t/903-number-vars.t
+++ /dev/null
@@ -1,38 +0,0 @@
-#!/usr/bin/env perl
-use strict;
-use warnings;
-use Test::More tests => 7;
-use Test::Exception;
-use Path::Dispatcher;
-
-my @vars;
-
-"abc" =~ /(.)(.)(.)/;
-is_deeply([$1, $2, $3, $4], ["a", "b", "c", undef]);
-
-my $dispatcher = Path::Dispatcher->new(
- rules => [
- Path::Dispatcher::Rule::Tokens->new(
- tokens => ['bus', 'train'],
- block => sub { push @vars, [$1, $2, $3] },
- ),
- ],
-);
-
-is_deeply([splice @vars], []);
-is_deeply([$1, $2, $3, $4], ["a", "b", "c", undef]);
-
-my $dispatch = $dispatcher->dispatch("bus train");
-
-is_deeply([splice @vars], []);
-is_deeply([$1, $2, $3, $4], ["a", "b", "c", undef]);
-
-$dispatch->run;
-
-is_deeply([splice @vars], [['bus', 'train', undef]]);
-
-TODO: {
- local $TODO = "we stomp on number vars..";
- is_deeply([$1, $2, $3, $4], ["a", "b", "c", undef]);
-};
-
commit fc2542524ad333e4bac81284480d75c0370c2e68
Author: Shawn M Moore <sartak at bestpractical.com>
Date: Sat Aug 21 20:36:07 2010 -0400
undef is a valid number variable value
"ac" =~ /(a)(b)?(c)/ will produce ("a", undef, "b")
diff --git a/lib/Path/Dispatcher/Match.pm b/lib/Path/Dispatcher/Match.pm
index 2e9a309..c5fe2fa 100644
--- a/lib/Path/Dispatcher/Match.pm
+++ b/lib/Path/Dispatcher/Match.pm
@@ -23,7 +23,7 @@ has rule => (
has positional_captures => (
is => 'rw',
- isa => 'ArrayRef[Str]',
+ isa => 'ArrayRef[Str|Undef]',
default => sub { [] },
);
commit 817ee9db60261732083b970f8895d850ad25cd66
Author: Shawn M Moore <sartak at bestpractical.com>
Date: Sat Aug 21 20:37:09 2010 -0400
Fix uses of ->result
diff --git a/t/014-tokens-prefix.t b/t/014-tokens-prefix.t
index 962a82c..a263bcb 100644
--- a/t/014-tokens-prefix.t
+++ b/t/014-tokens-prefix.t
@@ -17,6 +17,6 @@ ok($rule->match(Path::Dispatcher::Path->new('foo bar')), "prefix matches the ful
my $match = $rule->match(Path::Dispatcher::Path->new('foo bar baz'));
ok($match, "prefix matches a prefix of the path");
-is_deeply($match->result, ["foo", "bar"]);
+is_deeply($match->positional_captures, ["foo", "bar"]);
is($match->leftover, "baz");
diff --git a/t/015-regex-prefix.t b/t/015-regex-prefix.t
index 0ae9f94..eae193c 100644
--- a/t/015-regex-prefix.t
+++ b/t/015-regex-prefix.t
@@ -19,6 +19,6 @@ my $match = $rule->match(Path::Dispatcher::Path->new('foobar:baz'));
ok($match, "matched foobar:baz");
-is_deeply($match->result, ["foo", "bar"], "match returns just the results");
+is_deeply($match->positional_captures, ["foo", "bar"], "match returns just the results");
is($match->leftover, ':baz', "leftovers");
diff --git a/t/022-numbers-undef.t b/t/022-numbers-undef.t
index d4eff58..5d9ca56 100644
--- a/t/022-numbers-undef.t
+++ b/t/022-numbers-undef.t
@@ -8,12 +8,12 @@ my @recaptures;
my $rule = Path::Dispatcher::Rule::Regex->new(
regex => qr/^(foo)(bar)?(baz)$/,
block => sub {
- push @recaptures, $1, $2, $3;
+ push @recaptures, @{ shift->positional_captures };
},
);
my $match = $rule->match(Path::Dispatcher::Path->new("foobaz"));
-is_deeply($match->result, ['foo', undef, 'baz']);
+is_deeply($match->positional_captures, ['foo', undef, 'baz']);
$match->run;
is_deeply(\@recaptures, ['foo', undef, 'baz']);
commit 0ddd07823e375314a8514552aa60db9dd1004535
Author: Shawn M Moore <sartak at bestpractical.com>
Date: Sat Aug 21 20:40:16 2010 -0400
Better error when the _match result is mistyped
diff --git a/lib/Path/Dispatcher/Rule.pm b/lib/Path/Dispatcher/Rule.pm
index 1a8ba1d..3081c03 100644
--- a/lib/Path/Dispatcher/Rule.pm
+++ b/lib/Path/Dispatcher/Rule.pm
@@ -38,6 +38,10 @@ sub match {
return if !$result;
+ if (ref($result) ne 'HASH') {
+ die "Results returned from _match must be a hashref";
+ }
+
my $match = $self->match_class->new(
path => $path,
rule => $self,
diff --git a/t/901-return-values.t b/t/901-return-values.t
index 5822f7b..9df6416 100644
--- a/t/901-return-values.t
+++ b/t/901-return-values.t
@@ -15,5 +15,5 @@ my $dispatcher = Path::Dispatcher->new(
throws_ok {
$dispatcher->dispatch('foo');
-} qr/Invalid result 'HASH\(\w+\)', results must be plain strings/;
+} qr/Results returned from _match must be a hashref/;
commit fab0d44046ec5f9534eed1dd2c0a4f75fb02788d
Author: Shawn M Moore <sartak at bestpractical.com>
Date: Sat Aug 21 20:40:26 2010 -0400
More positional_captures stuff
diff --git a/t/024-sequence.t b/t/024-sequence.t
index 6cc0b9e..757a0cc 100644
--- a/t/024-sequence.t
+++ b/t/024-sequence.t
@@ -17,12 +17,12 @@ $dispatcher->add_rule(
string => 'bar',
),
],
- block => sub { push @calls, [$1, $2, $3] },
+ block => sub { push @calls, shift->positional_captures },
),
);
$dispatcher->run('foo bar');
-is_deeply([splice @calls], [ ['foo', 'bar', undef] ], "correctly populated number vars from [str, str] token rule");
+is_deeply([splice @calls], [ ['foo', 'bar'] ], "correctly populated number vars from [str, str] token rule");
$dispatcher->add_rule(
Path::Dispatcher::Rule::Sequence->new(
@@ -34,15 +34,15 @@ $dispatcher->add_rule(
regex => qr/bar/,
),
],
- block => sub { push @calls, [$1, $2, $3] },
+ block => sub { push @calls, shift->positional_captures },
),
);
$dispatcher->run('foo bar');
-is_deeply([splice @calls], [ ['foo', 'bar', undef] ], "ran the first [str, str] rule");
+is_deeply([splice @calls], [ ['foo', 'bar'] ], "ran the first [str, str] rule");
$dispatcher->run('foo barbaz');
-is_deeply([splice @calls], [ ['foo', 'barbaz', undef] ], "ran the second [str, regex] rule");
+is_deeply([splice @calls], [ ['foo', 'barbaz'] ], "ran the second [str, regex] rule");
$dispatcher->run('foo bar baz');
is_deeply([splice @calls], [ ], "no matches");
@@ -64,15 +64,15 @@ $dispatcher->add_rule(
string => 'Man',
),
],
- block => sub { push @calls, [$1, $2, $3] },
+ block => sub { push @calls, shift->positional_captures },
),
);
$dispatcher->run('Super Man');
-is_deeply([splice @calls], [ ['Super', 'Man', undef] ], "ran the [ [Str,Str], Str ] rule");
+is_deeply([splice @calls], [ ['Super', 'Man'] ], "ran the [ [Str,Str], Str ] rule");
$dispatcher->run('Bat Man');
-is_deeply([splice @calls], [ ['Bat', 'Man', undef] ], "ran the [ [Str,Str], Str ] rule");
+is_deeply([splice @calls], [ ['Bat', 'Man'] ], "ran the [ [Str,Str], Str ] rule");
$dispatcher->run('Aqua Man');
is_deeply([splice @calls], [ ], "no match");
@@ -99,12 +99,12 @@ $dispatcher->add_rule(
string => "Man",
),
],
- block => sub { push @calls, [$1, $2, $3] },
+ block => sub { push @calls, shift->positional_captures },
),
);
$dispatcher->run('Deep Man');
-is_deeply([splice @calls], [ ['Deep', 'Man', undef] ], "alternations can be arbitrarily deep");
+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");
diff --git a/t/025-sequence-custom-rule.t b/t/025-sequence-custom-rule.t
index cf1f726..d03b51d 100644
--- a/t/025-sequence-custom-rule.t
+++ b/t/025-sequence-custom-rule.t
@@ -23,22 +23,22 @@ my $dispatcher = Path::Dispatcher->new(
Path::Dispatcher::Rule::Eq->new(string => 'use'),
MyApp::Dispatcher::Rule::Language->new,
],
- block => sub { push @calls, [$1, $2, $3] },
+ block => sub { push @calls, shift->positional_captures },
),
],
);
$dispatcher->run("use perl");
-is_deeply([splice @calls], [["use", "perl", undef]]);
+is_deeply([splice @calls], [["use", "perl"]]);
$dispatcher->run("use python");
-is_deeply([splice @calls], [["use", "python", undef]]);
+is_deeply([splice @calls], [["use", "python"]]);
$dispatcher->run("use php");
-is_deeply([splice @calls], [["use", "php", undef]]);
+is_deeply([splice @calls], [["use", "php"]]);
$dispatcher->run("use ruby");
-is_deeply([splice @calls], [["use", "ruby", undef]]);
+is_deeply([splice @calls], [["use", "ruby"]]);
$dispatcher->run("use c++");
is_deeply([splice @calls], []);
@@ -63,7 +63,7 @@ $dispatcher = Path::Dispatcher->new(
MyApp::Dispatcher::Rule::Language->new,
Path::Dispatcher::Rule::Eq->new(string => 'please'),
],
- block => sub { push @calls, [$1, $2, $3, $4] },
+ block => sub { push @calls, shift->positional_captures },
),
],
);
@@ -72,25 +72,25 @@ $dispatcher->run("use perl");
is_deeply([splice @calls], []);
$dispatcher->run("use perl please");
-is_deeply([splice @calls], [["use", "perl", "please", undef]]);
+is_deeply([splice @calls], [["use", "perl", "please"]]);
$dispatcher->run("use python");
is_deeply([splice @calls], []);
$dispatcher->run("use python please");
-is_deeply([splice @calls], [["use", "python", "please", undef]]);
+is_deeply([splice @calls], [["use", "python", "please"]]);
$dispatcher->run("use php");
is_deeply([splice @calls], []);
$dispatcher->run("use php please");
-is_deeply([splice @calls], [["use", "php", "please", undef]]);
+is_deeply([splice @calls], [["use", "php", "please"]]);
$dispatcher->run("use ruby");
is_deeply([splice @calls], []);
$dispatcher->run("use ruby please");
-is_deeply([splice @calls], [["use", "ruby", "please", undef]]);
+is_deeply([splice @calls], [["use", "ruby", "please"]]);
$dispatcher->run("use c++");
is_deeply([splice @calls], []);
-----------------------------------------------------------------------
More information about the Bps-public-commit
mailing list