Skip to content

Commit

Permalink
exact matches should also qualify
Browse files Browse the repository at this point in the history
  • Loading branch information
heapwolf committed Sep 12, 2014
1 parent f45bc2c commit bfcced9
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 21 deletions.
4 changes: 2 additions & 2 deletions Makefile
@@ -1,9 +1,9 @@
all: build test

gyp:
./deps/gyp:
git clone --depth 1 https://chromium.googlesource.com/external/gyp.git ./deps/gyp

build: gyp
build: ./deps/gyp
deps/gyp/gyp --depth=. -Goutput_dir=./out -Icommon.gypi --generator-output=./build -Dlibrary=static_library -f make

install:
Expand Down
17 changes: 9 additions & 8 deletions route.cc
Expand Up @@ -18,11 +18,11 @@ namespace route {
return "";
}

int Match::test(string tmpl) {
bool Match::test(string tmpl) {

pairs.clear();
Path path;
int pos = 0;
keys = 0;

if (route->cache.count(tmpl)) {
path = route->cache.at(tmpl);
Expand Down Expand Up @@ -53,17 +53,18 @@ namespace route {
}

smatch sm_values;
regex_match(route->url, sm_values, path.re);

if (sm_values.size() <= 1) return 0;
if (!regex_match(route->url, sm_values, path.re)) {
return false;
}

if (sm_values.size() < 1) return true;

for (auto i = 0; i < sm_values.size() - 1; i++) {
string key = path.keys[i];
pairs.insert(pair<string, string>(key, sm_values[i + 1]));
pos++;
keys++;
}

return pos;
return true;
}

} // namespace url
Expand Down
4 changes: 2 additions & 2 deletions route.h
Expand Up @@ -76,9 +76,9 @@ class Match {
map<string, string> pairs;

public:

int keys;
string get(string key);
int test(string tmpl);
bool test(string tmpl);

Match(Route &r) : route(&r) {}
~Match() {}
Expand Down
28 changes: 19 additions & 9 deletions test.cc
Expand Up @@ -21,29 +21,39 @@ int main() {
ASSERT("true is false", true == false);
ASSERT("true is true", true == true);

int i;
int r;
string s;

i = match.test("/foo/:num/:str");
ASSERT("a pattern that matches the url with two captures should contain two keys", i == 2);

match = route.set("/short");
r = match.test("/short");
ASSERT("exact match", r == true);

match = route.set("/foo/666/bazz");

r = match.test("/foo/:num/:str");
ASSERT("a pattern that matches the url with two captures should contain two keys", match.keys == 2);

s = match.get("num");
ASSERT("the first key matches the value provided in the url", s == "666");

s = match.get("str");
ASSERT("the second key matches the value provided in the url", s == "bazz");

i = match.test("/foo/:num");
ASSERT("a shorter pattern that does match the url should contain 0 keys", i == 0);
r = match.test("/foo/:num");
ASSERT("url match", r == false);
ASSERT("a shorter pattern that does match the url should contain 0 keys", match.keys == 0);

s = match.get("num");
ASSERT("the first key from the previous match should not exist after a new call to test()", s == "");

i = match.test("/foo/bar/:num/:str");
ASSERT("a longer pattern that doesn't match the url should contain 0 keys", i == 0);
r = match.test("/foo/bar/:num/:str");
ASSERT("url match", r == false);
ASSERT("a longer pattern that doesn't match the url should contain 0 keys", match.keys == 0);

match = route.set("/foo/a666/bazz");
i = match.test("/foo/a:num/:str");
ASSERT("a pattern that has a prefixed capture should contain 2 keys", i == 2);
r = match.test("/foo/a:num/:str");
ASSERT("url match", r == true);
ASSERT("a pattern that has a prefixed capture should contain 2 keys", match.keys == 2);
}

0 comments on commit bfcced9

Please sign in to comment.