[Bps-public-commit] Path-Dispatcher branch, master, updated. ffc53b70cf89ee96b64764e14c1e62e1888d24be
sartak at bestpractical.com
sartak at bestpractical.com
Wed Jan 6 19:36:07 EST 2010
The branch, master has been updated
via ffc53b70cf89ee96b64764e14c1e62e1888d24be (commit)
via 3362967400a2f273355be241eafc3f6bb14b9439 (commit)
from 9f1931e648a33442256aac8da17aa4b21f22d808 (commit)
Summary of changes:
lib/Path/Dispatcher/Rule.pm | 1 +
.../Rule/{Intersection.pm => Alternation.pm} | 13 +++--
lib/Path/Dispatcher/Rule/Intersection.pm | 5 ++-
t/017-intersection.t | 15 ++++++-
t/023-alternation.t | 48 ++++++++++++++++++++
5 files changed, 75 insertions(+), 7 deletions(-)
copy lib/Path/Dispatcher/Rule/{Intersection.pm => Alternation.pm} (55%)
create mode 100644 t/023-alternation.t
- Log -----------------------------------------------------------------
commit 3362967400a2f273355be241eafc3f6bb14b9439
Author: Shawn M Moore <sartak at bestpractical.com>
Date: Wed Jan 6 19:32:59 2010 -0500
Add alternation rules
diff --git a/lib/Path/Dispatcher/Rule.pm b/lib/Path/Dispatcher/Rule.pm
index 83ccbe5..b32dfaf 100644
--- a/lib/Path/Dispatcher/Rule.pm
+++ b/lib/Path/Dispatcher/Rule.pm
@@ -139,6 +139,7 @@ __PACKAGE__->meta->make_immutable;
no Any::Moose;
# don't require others to load our subclasses explicitly
+require Path::Dispatcher::Rule::Alternation;
require Path::Dispatcher::Rule::Always;
require Path::Dispatcher::Rule::Chain;
require Path::Dispatcher::Rule::CodeRef;
diff --git a/lib/Path/Dispatcher/Rule/Alternation.pm b/lib/Path/Dispatcher/Rule/Alternation.pm
new file mode 100644
index 0000000..0b0eb0a
--- /dev/null
+++ b/lib/Path/Dispatcher/Rule/Alternation.pm
@@ -0,0 +1,38 @@
+package Path::Dispatcher::Rule::Alternation;
+use Any::Moose;
+extends 'Path::Dispatcher::Rule';
+
+with 'Path::Dispatcher::Role::Rules';
+
+sub _match {
+ my $self = shift;
+ my $path = shift;
+
+ for my $rule ($self->rules) {
+ return 1 if $rule->match($path);
+ }
+
+ return 0;
+}
+
+__PACKAGE__->meta->make_immutable;
+no Any::Moose;
+
+1;
+
+__END__
+
+=head1 NAME
+
+Path::Dispatcher::Rule::Alternation - any rule must match
+
+=head1 SYNOPSIS
+
+=head1 DESCRIPTION
+
+=head1 ATTRIBUTES
+
+=head2 rules
+
+=cut
+
diff --git a/t/023-alternation.t b/t/023-alternation.t
new file mode 100644
index 0000000..def8316
--- /dev/null
+++ b/t/023-alternation.t
@@ -0,0 +1,35 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use Test::More tests => 3;
+use Path::Dispatcher;
+
+my @calls;
+
+my $dispatcher = Path::Dispatcher->new(
+ rules => [
+ Path::Dispatcher::Rule::Alternation->new(
+ rules => [
+ Path::Dispatcher::Rule::Eq->new(
+ string => 'foo',
+ block => sub { push @calls, 'foo' },
+ ),
+ Path::Dispatcher::Rule::Eq->new(
+ string => 'bar',
+ block => sub { push @calls, 'bar' },
+ ),
+ ],
+ block => sub { push @calls, 'alternation' },
+ ),
+ ],
+);
+
+$dispatcher->run("foo");
+is_deeply([splice @calls], ['alternation'], "the alternation matched; doesn't automatically run the subrules");
+
+$dispatcher->run("bar");
+is_deeply([splice @calls], ['alternation'], "the alternation matched; doesn't automatically run the subrules");
+
+$dispatcher->run("baz");
+is_deeply([splice @calls], [], "each subrule of the intersection must match");
+
commit ffc53b70cf89ee96b64764e14c1e62e1888d24be
Author: Shawn M Moore <sartak at bestpractical.com>
Date: Wed Jan 6 19:35:29 2010 -0500
Empty intersections and alternations do not match
I could be swayed either way on this one.
diff --git a/lib/Path/Dispatcher/Rule/Alternation.pm b/lib/Path/Dispatcher/Rule/Alternation.pm
index 0b0eb0a..1135359 100644
--- a/lib/Path/Dispatcher/Rule/Alternation.pm
+++ b/lib/Path/Dispatcher/Rule/Alternation.pm
@@ -8,7 +8,10 @@ sub _match {
my $self = shift;
my $path = shift;
- for my $rule ($self->rules) {
+ my @rules = $self->rules;
+ return 0 if @rules == 0;
+
+ for my $rule (@rules) {
return 1 if $rule->match($path);
}
diff --git a/lib/Path/Dispatcher/Rule/Intersection.pm b/lib/Path/Dispatcher/Rule/Intersection.pm
index afc6ad2..a1f6a99 100644
--- a/lib/Path/Dispatcher/Rule/Intersection.pm
+++ b/lib/Path/Dispatcher/Rule/Intersection.pm
@@ -8,7 +8,10 @@ sub _match {
my $self = shift;
my $path = shift;
- for my $rule ($self->rules) {
+ my @rules = $self->rules;
+ return 0 if @rules == 0;
+
+ for my $rule (@rules) {
return 0 unless $rule->match($path);
}
diff --git a/t/017-intersection.t b/t/017-intersection.t
index 53946bf..4228727 100644
--- a/t/017-intersection.t
+++ b/t/017-intersection.t
@@ -1,7 +1,7 @@
#!/usr/bin/env perl
use strict;
use warnings;
-use Test::More tests => 3;
+use Test::More tests => 4;
use Path::Dispatcher;
my @calls;
@@ -33,3 +33,16 @@ is_deeply([splice @calls], [], "each subrule of the intersection must match");
$dispatcher->run(" foo ");
is_deeply([splice @calls], [], "each subrule of the intersection must match");
+# test empty intersection
+$dispatcher = Path::Dispatcher->new(
+ rules => [
+ Path::Dispatcher::Rule::Intersection->new(
+ rules => [ ],
+ block => sub { push @calls, 'intersection' },
+ ),
+ ],
+);
+
+$dispatcher->run("foo");
+is_deeply([splice @calls], [], "no subrules means no match");
+
diff --git a/t/023-alternation.t b/t/023-alternation.t
index def8316..d75059e 100644
--- a/t/023-alternation.t
+++ b/t/023-alternation.t
@@ -1,7 +1,7 @@
#!/usr/bin/env perl
use strict;
use warnings;
-use Test::More tests => 3;
+use Test::More tests => 4;
use Path::Dispatcher;
my @calls;
@@ -33,3 +33,16 @@ is_deeply([splice @calls], ['alternation'], "the alternation matched; doesn't au
$dispatcher->run("baz");
is_deeply([splice @calls], [], "each subrule of the intersection must match");
+# test empty alternation
+$dispatcher = Path::Dispatcher->new(
+ rules => [
+ Path::Dispatcher::Rule::Alternation->new(
+ rules => [ ],
+ block => sub { push @calls, 'alternation' },
+ ),
+ ],
+);
+
+$dispatcher->run("foo");
+is_deeply([splice @calls], [], "no subrules means no match");
+
-----------------------------------------------------------------------
More information about the Bps-public-commit
mailing list