[Bps-public-commit] Path-Dispatcher branch, master, updated. 2b1a4b0365732213539017e6b03f887496d55711

sartak at bestpractical.com sartak at bestpractical.com
Fri Dec 18 20:17:20 EST 2009


The branch, master has been updated
       via  2b1a4b0365732213539017e6b03f887496d55711 (commit)
      from  56a45165bbeec000f73ed2e47c16c8e89edab517 (commit)

Summary of changes:
 lib/Path/Dispatcher/Rule/Tokens.pm |   14 ++++++--
 t/303-complete-alternation.t       |   55 ++++++++++++++++++++++++++++++++++++
 2 files changed, 65 insertions(+), 4 deletions(-)
 create mode 100644 t/303-complete-alternation.t

- Log -----------------------------------------------------------------
commit 2b1a4b0365732213539017e6b03f887496d55711
Author: Shawn M Moore <sartak at bestpractical.com>
Date:   Fri Dec 18 20:17:09 2009 -0500

    Handle alternations better

diff --git a/lib/Path/Dispatcher/Rule/Tokens.pm b/lib/Path/Dispatcher/Rule/Tokens.pm
index 22367c8..dfbd05d 100644
--- a/lib/Path/Dispatcher/Rule/Tokens.pm
+++ b/lib/Path/Dispatcher/Rule/Tokens.pm
@@ -65,11 +65,17 @@ sub complete {
     return if !@$expected; # consumed all tokens
 
     my $next = shift @$expected;
-    return if ref($next); # we can only deal with strings
-
     my $part = @$got ? shift @$got : '';
-    return unless substr($next, 0, length($part)) eq $part;
-    return $self->untokenize(@$matched, $next);
+    my @completions;
+
+    for my $completion (ref($next) eq 'ARRAY' ? @$next : $next) {
+        next if ref($completion);
+
+        next unless substr($completion, 0, length($part)) eq $part;
+        push @completions, $self->untokenize(@$matched, $completion);
+    }
+
+    return @completions;
 }
 
 sub _each_token {
diff --git a/t/303-complete-alternation.t b/t/303-complete-alternation.t
new file mode 100644
index 0000000..67675e3
--- /dev/null
+++ b/t/303-complete-alternation.t
@@ -0,0 +1,55 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use Test::More tests => 16;
+
+do {
+    package MyApp::Dispatcher;
+    use Path::Dispatcher::Declarative -base;
+
+    under gate => sub {
+        on [ ['foo', 'bar', 'baz'] ] => sub { die };
+        on quux => sub { die };
+    };
+};
+
+my $dispatcher = MyApp::Dispatcher->dispatcher;
+
+sub complete_ok {
+    local $Test::Builder::Level = $Test::Builder::Level + 1;
+    my $path     = shift;
+    my @expected = @_;
+
+    my @got = $dispatcher->complete($path);
+
+    my $message = @expected == 0 ? "no completions"
+                : @expected == 1 ? "one completion"
+                :                  @expected . " completions";
+    $message .= " for path '$path'";
+
+    is_deeply(\@got, \@expected, $message);
+}
+
+complete_ok('z');
+complete_ok('gate z');
+
+complete_ok(g   => 'gate');
+complete_ok(ga  => 'gate');
+complete_ok(gat => 'gate');
+
+complete_ok(gate    => 'gate foo', 'gate bar', 'gate baz', 'gate quux');
+complete_ok('gate ' => 'gate foo', 'gate bar', 'gate baz', 'gate quux');
+
+complete_ok('gate f' => 'gate foo');
+
+complete_ok('gate b'  => 'gate bar', 'gate baz');
+complete_ok('gate ba' => 'gate bar', 'gate baz');
+
+complete_ok('gate q'   => 'gate quux');
+complete_ok('gate quu' => 'gate quux');
+
+complete_ok('gate foo');
+complete_ok('gate bar');
+complete_ok('gate baz');
+complete_ok('gate quux');
+

-----------------------------------------------------------------------



More information about the Bps-public-commit mailing list