Permalink
Browse files

some tests

  • Loading branch information...
1 parent d8a827e commit 6db6cb42f538ed997f19f430cbd1cb6ccac3d947 @ruoso committed May 12, 2009
Showing with 53 additions and 19 deletions.
  1. +41 −12 example.pl
  2. +3 −3 lib/Faz/Application.pm
  3. +9 −4 lib/Faz/Dispatcher.pm
View
@@ -11,36 +11,65 @@
my $app = Faz::Application.new(:dispatcher(Faz::Dispatcher.new()));
my $ctrl = Faz::Controller.new();
-my $root = Faz::Action::Root.new( :parent(0),
- :controller($ctrl),
- :private-name('(root)'),
- :base('/'),
- :begin-closure({ say "root - begin" }),
- :execute-closure({ say "root - execute " }),
- :finish-closure({ say "root - finish" }) );
+my $root = Faz::Action::Chained.new( :parent(0),
+ :controller($ctrl),
+ :private-name('(root)'),
+ :regex(/^ /),
+ :begin-closure({ say "root - begin" }),
+ :execute-closure({ say "root - execute " }),
+ :finish-closure({ say "root - finish" }) );
$app.register-action($root);
my $blog = Faz::Action::Chained.new( :parent($root),
:controller($ctrl),
:private-name('(root)/blog/*'),
- :regex(/ blog\/(\w+)\/ /),
+ :regex(/ \/blog\/(\w+) /),
:begin-closure(-> $name { say "blog $name - begin " }),
:execute-closure(-> $name { say "blog $name - execute " }),
:finish-closure(-> $name { say "blog $name - finish" }) );
$app.register-action($blog);
+my $index = Faz::Action::Public.new( :parent($root),
+ :controller($ctrl),
+ :private-name('(root)/blog/*'),
+ :regex(/ \/ $/),
+ :begin-closure({ say "index - begin " }),
+ :execute-closure({ say "index - execute " }),
+ :finish-closure({ say "index - finish" }) );
+$app.register-action($index);
+my $viewblog1 = Faz::Action::Public.new( :parent($blog),
+ :controller($ctrl),
+ :private-name('(root)/blog/*/'),
+ :regex(/ \/? $/),
+ :begin-closure({ say "viewblog1 - begin" }),
+ :execute-closure({ say "viewblog1 - execute" }),
+ :finish-closure({ say "viewblog1 - finish" }) );
+$app.register-action($viewblog1);
my $viewblog = Faz::Action::Public.new( :parent($blog),
:controller($ctrl),
:private-name('(root)/blog/*/'),
- :regex(/ (\w+) \/? /),
- :begin-closure( -> $name { say "viewblog $name - begin" }),
+ :regex(/ \/ (\w+) \/? $/),
+ :begin-closure(-> $name { say "viewblog $name - begin" }),
:execute-closure( -> $name { say "viewblog $name " ~ $*request.params<test> ~ " - execute" }),
:finish-closure(-> $name { say "viewblog $name - finish" }) );
$app.register-action($viewblog);
-my $uri = Faz::URI.new(:path('/blog/faz/bla'));
-my $request = Faz::Request.new(:uri($uri), :params({ :test<"ok!"> }) );
+
+
+my $uri;
+my $request;
my $response = 1;
+
+$uri = Faz::URI.new(:path('/blog/faz'));
+$request = Faz::Request.new(:uri($uri), :params({ :test<"ok!"> }) );
+$app.handle($request,$response);
+
+$uri = Faz::URI.new(:path('/blog/faz/bla'));
+$request = Faz::Request.new(:uri($uri), :params({ :test<"ok!"> }) );
+$app.handle($request,$response);
+
+$uri = Faz::URI.new(:path(''));
+$request = Faz::Request.new(:uri($uri), :params({ :test<"ok!"> }) );
$app.handle($request,$response);
1;
View
@@ -16,9 +16,9 @@ role Faz::Application {
$*request = $request;
$*response = $response;
%*stash = ();
- self.prepare;
- self.dispatch;
- self.finalize;
+ self.*prepare;
+ self.*dispatch;
+ self.*finalize;
};
}
View
@@ -40,30 +40,35 @@ role Faz::Dispatcher {
my &subrx = sub ($/) {
for @subregexes -> &eachrx {
- my $result = eachrx($/);
+ my $match = Match.new($/);
+ my $result = eachrx($match);
if $result {
return $result;
};
};
return Match.new($/);
};
+ warn 'Compiling regexes.';
$!regex = token { <subrx> };
# I get a null pmc in isa_pmc() if without this line...
1;
}
method dispatch() {
- unless $!regex { self.compile; }
+ unless defined $!regex {
+ warn 'Compiling regexes at dispatch time.';
+ self.compile;
+ }
if $*request.uri.path ~~ $!regex {
my %named = %($<subrx><action_capture>);
my @pos = @($<subrx><action_capture>);
%named<parent_action_capture> = $<subrx><parent_action_capture>;
self.run-action($<subrx>.ast, |@pos, |%named );
} else {
- say 'failed';
- fail 'No action matched';
+ warn "No action matched '{$*request.uri.path}'";
+ 1;
}
}

0 comments on commit 6db6cb4

Please sign in to comment.