Skip to content

Commit

Permalink
Fixing regexp routes that fail if captures are unnamed. [#8 state:res…
Browse files Browse the repository at this point in the history
…olved]

Update matching test case.
  • Loading branch information
Fabrice Luraine committed Jun 9, 2009
1 parent 8f96b2b commit 166f93c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
9 changes: 7 additions & 2 deletions lib/limonade.php
Expand Up @@ -1112,13 +1112,18 @@ function route_find($method, $path)
{
array_shift($matches);
$n_matches = count($matches);
$n_names = count($route["names"]);
$names = array_values($route["names"]);
$n_names = count($names);
if( $n_matches < $n_names )
{
$a = array_fill(0, $n_names - $n_matches, null);
$matches = array_merge($matches, $a);
}
$params = array_combine(array_values($route["names"]), $matches);
else if( $n_matches > $n_names )
{
$names = range($n_names, $n_matches - 1);
}
$params = array_combine($names, $matches);
}
$route["params"] = $params;
return $route;
Expand Down
8 changes: 6 additions & 2 deletions tests/router.php
Expand Up @@ -174,9 +174,10 @@ function test_router_find_route()
route( "get", "/edit/:id", "my_edit_func" );
route( "put", "/update/:id", "my_update_func" );
route( "delete", "/delete/:id", "my_delete_func" );
route( "get", "^/list/(\d+)","my_list_func" );
$routes = route( "get", "/*.jpg/:size","my_jpeg" );

assert_length_of($routes, 7);
assert_length_of($routes, 8);

$r = route_find("GET", "/unkown");
assert_false($r);
Expand All @@ -203,7 +204,7 @@ function test_router_find_route()
route( "get", "/index/*", "my_index_func2" );
$routes = route( "delete", "/delete/:id/:confirm", "my_delete_func2" );

assert_length_of($routes, 9);
assert_length_of($routes, 10);
$r = route_find("GET", "/index");
assert_equal($r["function"], "my_index_func");

Expand All @@ -219,6 +220,9 @@ function test_router_find_route()
$r = route_find("DELETE", "/delete/120/ok");
assert_equal($r["function"], "my_delete_func2");

$r = route_find("GET", "/list/120");
assert_equal($r["function"], "my_list_func");

}

endtests();
Expand Down

0 comments on commit 166f93c

Please sign in to comment.