2019-06-30 22:46:51 -04:00
|
|
|
#include "config.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <check.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include "r3.h"
|
|
|
|
#include "r3_slug.h"
|
|
|
|
|
2019-07-20 19:48:11 -04:00
|
|
|
START_TEST (greedy_pattern)
|
2019-06-30 22:46:51 -04:00
|
|
|
{
|
|
|
|
R3Node * n = r3_tree_create(10);
|
|
|
|
match_entry * entry;
|
|
|
|
R3Route *matched_route;
|
|
|
|
|
|
|
|
char * uri0 = "/foo{:.*}";
|
|
|
|
r3_tree_insert_routel(n, 0, uri0, strlen(uri0), &uri0);
|
|
|
|
|
|
|
|
char * err = NULL;
|
|
|
|
r3_tree_compile(n, &err);
|
|
|
|
ck_assert(err == NULL);
|
|
|
|
|
|
|
|
entry = match_entry_create("/foo/bar");
|
|
|
|
matched_route = r3_tree_match_route(n, entry);
|
|
|
|
ck_assert(matched_route != NULL);
|
|
|
|
ck_assert(matched_route->data == &uri0);
|
2021-10-10 15:10:24 -04:00
|
|
|
match_entry_free(entry);
|
2019-06-30 22:46:51 -04:00
|
|
|
|
2019-07-19 18:51:18 -04:00
|
|
|
entry = match_entry_create("/foo");
|
|
|
|
matched_route = r3_tree_match_route(n, entry);
|
|
|
|
ck_assert(matched_route != NULL);
|
|
|
|
ck_assert(matched_route->data == &uri0);
|
2021-10-10 15:10:24 -04:00
|
|
|
match_entry_free(entry);
|
2019-06-30 22:46:51 -04:00
|
|
|
|
|
|
|
entry = match_entry_create("/foo/");
|
|
|
|
matched_route = r3_tree_match_route(n, entry);
|
|
|
|
ck_assert(matched_route != NULL);
|
|
|
|
ck_assert(matched_route->data == &uri0);
|
2021-10-10 15:10:24 -04:00
|
|
|
match_entry_free(entry);
|
2019-06-30 22:46:51 -04:00
|
|
|
|
|
|
|
entry = match_entry_create("/foo/bar/foo/mmasdfasdfasd/f/asdf/as/df");
|
|
|
|
matched_route = r3_tree_match_route(n, entry);
|
|
|
|
ck_assert(matched_route != NULL);
|
|
|
|
ck_assert(matched_route->data == &uri0);
|
2021-10-10 15:10:24 -04:00
|
|
|
match_entry_free(entry);
|
|
|
|
|
|
|
|
r3_tree_free(n);
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
|
|
|
START_TEST (common_pattern)
|
|
|
|
{
|
|
|
|
R3Node * n = r3_tree_create(10);
|
|
|
|
match_entry * entry;
|
|
|
|
R3Route *r, *matched_route;
|
|
|
|
|
|
|
|
char * uri0 = "/foo/{slug}";
|
|
|
|
r = r3_tree_insert_routel(n, 0, uri0, strlen(uri0), &uri0);
|
|
|
|
ck_assert(r != NULL);
|
|
|
|
char * uri1 = "/foo/{slug}/bar";
|
|
|
|
r = r3_tree_insert_routel(n, 0, uri1, strlen(uri1), &uri1);
|
|
|
|
ck_assert(r != NULL);
|
|
|
|
|
|
|
|
char * err = NULL;
|
|
|
|
r3_tree_compile(n, &err);
|
|
|
|
ck_assert(err == NULL);
|
|
|
|
|
|
|
|
entry = match_entry_create("/foo/bar");
|
|
|
|
matched_route = r3_tree_match_route(n, entry);
|
|
|
|
ck_assert(matched_route != NULL);
|
|
|
|
ck_assert(matched_route->data == &uri0);
|
|
|
|
match_entry_free(entry);
|
|
|
|
|
|
|
|
entry = match_entry_create("/foo/bar/bar");
|
|
|
|
matched_route = r3_tree_match_route(n, entry);
|
|
|
|
ck_assert(matched_route != NULL);
|
|
|
|
ck_assert(matched_route->data == &uri1);
|
|
|
|
match_entry_free(entry);
|
2019-06-30 22:46:51 -04:00
|
|
|
|
|
|
|
r3_tree_free(n);
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
2021-11-04 13:34:39 -04:00
|
|
|
START_TEST (incomplete_pattern)
|
|
|
|
{
|
|
|
|
R3Node * n = r3_tree_create(10);
|
|
|
|
R3Route * r;
|
|
|
|
|
|
|
|
char * uri0 = "{slug";
|
|
|
|
r = r3_tree_insert_routel(n, 0, uri0, strlen(uri0), &uri0);
|
|
|
|
ck_assert(r == NULL);
|
|
|
|
char * uri1 = "/foo/{slug";
|
|
|
|
r = r3_tree_insert_routel(n, 0, uri1, strlen(uri1), &uri1);
|
|
|
|
ck_assert(r == NULL);
|
|
|
|
|
|
|
|
r3_tree_free(n);
|
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
2019-06-30 22:46:51 -04:00
|
|
|
Suite* r3_suite (void) {
|
|
|
|
Suite *suite = suite_create("r3 routes2 tests");
|
|
|
|
TCase *tcase = tcase_create("testcase");
|
2019-07-20 19:48:11 -04:00
|
|
|
tcase_add_test(tcase, greedy_pattern);
|
2021-10-10 15:10:24 -04:00
|
|
|
tcase_add_test(tcase, common_pattern);
|
2021-11-04 13:34:39 -04:00
|
|
|
tcase_add_test(tcase, incomplete_pattern);
|
2019-06-30 22:46:51 -04:00
|
|
|
suite_add_tcase(suite, tcase);
|
|
|
|
return suite;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main (int argc, char *argv[]) {
|
|
|
|
int number_failed;
|
|
|
|
Suite *suite = r3_suite();
|
|
|
|
SRunner *runner = srunner_create(suite);
|
|
|
|
srunner_run_all(runner, CK_NORMAL);
|
|
|
|
number_failed = srunner_ntests_failed(runner);
|
|
|
|
srunner_free(runner);
|
|
|
|
return number_failed;
|
|
|
|
}
|