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

sartak at bestpractical.com sartak at bestpractical.com
Mon Aug 25 19:34:33 EDT 2008


Author: sartak
Date: Mon Aug 25 19:34:33 2008
New Revision: 15451

Added:
   Path-Dispatcher/trunk/t/011-next-rule.t
Modified:
   Path-Dispatcher/trunk/   (props changed)
   Path-Dispatcher/trunk/lib/Path/Dispatcher/Dispatch.pm

Log:
 r70331 at onn:  sartak | 2008-08-25 19:34:26 -0400
 Support for continuing on to the next rule as if the current rule hadn't been matched (next_rule)


Modified: Path-Dispatcher/trunk/lib/Path/Dispatcher/Dispatch.pm
==============================================================================
--- Path-Dispatcher/trunk/lib/Path/Dispatcher/Dispatch.pm	(original)
+++ Path-Dispatcher/trunk/lib/Path/Dispatcher/Dispatch.pm	Mon Aug 25 19:34:33 2008
@@ -54,8 +54,14 @@
     eval {
         local $SIG{__DIE__} = 'DEFAULT';
         for my $match ($self->matches) {
-            $match->run(@args);
-            last if $match->ends_dispatch($self);
+            eval {
+                local $SIG{__DIE__} = 'DEFAULT';
+                $match->run(@args);
+
+                no warnings 'exiting';
+                last if $match->ends_dispatch($self);
+            };
+            die $@ if $@ && $@ !~ /^Patch::Dispatcher next rule\n/;
         }
     };
 

Added: Path-Dispatcher/trunk/t/011-next-rule.t
==============================================================================
--- (empty file)
+++ Path-Dispatcher/trunk/t/011-next-rule.t	Mon Aug 25 19:34:33 2008
@@ -0,0 +1,58 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use Test::More tests => 6;
+use Test::Exception;
+use Path::Dispatcher;
+
+my @calls;
+
+my $dispatcher = Path::Dispatcher->new;
+$dispatcher->stage('on')->add_rule(
+    Path::Dispatcher::Rule::Regex->new(
+        regex => qr/foo/,
+        block => sub {
+            push @calls, "on";
+            die "Patch::Dispatcher next rule\n";
+            push @calls, "on post-die?!";
+        },
+    ),
+);
+
+$dispatcher->stage('on')->add_rule(
+    Path::Dispatcher::Rule::Regex->new(
+        regex => qr/foo/,
+        block => sub {
+            push @calls, "last";
+        },
+    ),
+);
+
+my $dispatch;
+lives_ok {
+    $dispatch = $dispatcher->dispatch('foo');
+};
+is_deeply([splice @calls], [], "no blocks called yet of course");
+
+lives_ok {
+    $dispatch->run;
+};
+is_deeply([splice @calls], ['on', 'last'], "correctly continued to the next rule");
+
+$dispatcher->stage('on')->add_rule(
+    Path::Dispatcher::Rule::Regex->new(
+        regex => qr/bar/,
+        block => sub {
+            push @calls, "bar: before";
+            my $x = {}->();
+            push @calls, "bar: last";
+        },
+    ),
+);
+
+throws_ok {
+    $dispatcher->run('bar');
+} qr/Not a CODE reference/;
+
+is_deeply([splice @calls], ['bar: before'], "regular dies pass through");
+



More information about the Bps-public-commit mailing list