[Bps-public-commit] r16353 - in Path-Dispatcher/trunk: lib/Path/Dispatcher/Rule t

sartak at bestpractical.com sartak at bestpractical.com
Sun Oct 19 04:57:07 EDT 2008


Author: sartak
Date: Sun Oct 19 04:57:07 2008
New Revision: 16353

Added:
   Path-Dispatcher/trunk/t/015-regex-prefix.t
Modified:
   Path-Dispatcher/trunk/   (props changed)
   Path-Dispatcher/trunk/lib/Path/Dispatcher/Rule/Regex.pm

Log:
 r74109 at onn:  sartak | 2008-10-19 04:57:03 -0400
 Regex prefix implementation and tests


Modified: Path-Dispatcher/trunk/lib/Path/Dispatcher/Rule/Regex.pm
==============================================================================
--- Path-Dispatcher/trunk/lib/Path/Dispatcher/Rule/Regex.pm	(original)
+++ Path-Dispatcher/trunk/lib/Path/Dispatcher/Rule/Regex.pm	Sun Oct 19 04:57:07 2008
@@ -14,7 +14,16 @@
     my $path = shift;
 
     return unless $path =~ $self->regex;
-    return [ map { substr($path, $-[$_], $+[$_] - $-[$_]) } 1 .. $#- ];
+
+    my @matches = map { substr($path, $-[$_], $+[$_] - $-[$_]) } 1 .. $#-;
+
+    # 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 \@matches;
 }
 
 __PACKAGE__->meta->make_immutable;

Added: Path-Dispatcher/trunk/t/015-regex-prefix.t
==============================================================================
--- (empty file)
+++ Path-Dispatcher/trunk/t/015-regex-prefix.t	Sun Oct 19 04:57:07 2008
@@ -0,0 +1,25 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use Test::More tests => 5;
+use Path::Dispatcher;
+
+my @calls;
+
+my $rule = Path::Dispatcher::Rule::Regex->new(
+    regex  => qr/^(foo)\s*(bar)/,
+    block  => sub { push @calls, [$1, $2] },
+    prefix => 1,
+);
+
+ok(!$rule->match('foo'), "prefix means the rule matches a prefix of the path, not the other way around");
+ok($rule->match('foo bar'), "prefix matches the full path");
+ok($rule->match('foo bar baz'), "prefix matches a prefix of the path");
+
+is_deeply($rule->match('foobar baz'), ["foo", "bar"], "match returns just the results");
+is_deeply([$rule->_match('foobar:baz')], [
+    ["foo", "bar"],
+    ":baz"
+], "_match returns the results and the rest of the path");
+
+



More information about the Bps-public-commit mailing list