r3/tests/check_slug.c

200 lines
5.2 KiB
C
Raw Permalink Normal View History

2014-05-18 08:11:07 -04:00
/*
* check_slug.c
* Copyright (C) 2014 c9s <c9s@c9smba.local>
*
* Distributed under terms of the MIT license.
*/
#include "config.h"
#include <stdio.h>
#include <check.h>
#include <stdlib.h>
#include "r3.h"
#include "r3_str.h"
2014-05-21 10:02:27 -04:00
#include "zmalloc.h"
#include "slug.h"
2014-05-18 08:11:07 -04:00
2014-05-22 23:25:25 -04:00
START_TEST (test_pattern_to_opcode)
{
2014-05-23 10:58:20 -04:00
ck_assert( r3_pattern_to_opcode("\\w+", strlen("\\w+")) == OP_EXPECT_MORE_WORDS );
ck_assert( r3_pattern_to_opcode("\\d+", strlen("\\d+")) == OP_EXPECT_MORE_DIGITS );
2014-05-23 01:49:18 -04:00
ck_assert( r3_pattern_to_opcode("[^/]+",strlen("[^/]+")) == OP_EXPECT_NOSLASH );
ck_assert( r3_pattern_to_opcode("[^-]+",strlen("[^-]+")) == OP_EXPECT_NODASH );
2014-05-22 23:25:25 -04:00
}
END_TEST
2014-05-20 11:25:55 -04:00
START_TEST (test_slug_compile)
2014-05-18 08:11:07 -04:00
{
char * path = "/user/{id}";
2014-05-21 06:50:45 -04:00
char * c = NULL;
2014-06-01 13:21:02 -04:00
ck_assert_str_eq( c = slug_compile(path, strlen(path) ) , "^/user/([^/]+)" );
2014-05-21 06:50:45 -04:00
zfree(c);
2014-05-18 08:11:07 -04:00
char * path2 = "/what/{id}-foo";
2014-06-01 13:21:02 -04:00
ck_assert_str_eq( c = slug_compile(path2, strlen(path2) ) , "^/what/([^/]+)-foo" );
2014-05-21 06:50:45 -04:00
zfree(c);
2014-05-20 09:38:59 -04:00
char * path3 = "-{id}";
2014-06-01 13:21:02 -04:00
ck_assert_str_eq( c = slug_compile(path3, strlen(path3)), "^-([^/]+)" );
2014-05-21 06:50:45 -04:00
zfree(c);
2014-05-20 09:38:59 -04:00
char * path4 = "-{idx:\\d{3}}";
2014-06-01 13:21:02 -04:00
ck_assert_str_eq( c = slug_compile(path4, strlen(path4)), "^-(\\d{3})" );
2014-05-21 06:50:45 -04:00
zfree(c);
2014-05-18 08:11:07 -04:00
}
END_TEST
START_TEST (test_contains_slug)
{
2014-06-01 18:27:35 -04:00
ck_assert( r3_path_contains_slug_char("/user/{id}/{name}") );
2014-05-18 08:11:07 -04:00
}
END_TEST
2014-05-23 01:49:18 -04:00
START_TEST (test_slug_find_pattern)
2014-05-18 08:11:07 -04:00
{
int len;
2014-05-23 01:49:18 -04:00
char * namerex = slug_find_pattern("{name:\\s+}", &len);
2014-05-18 08:11:07 -04:00
ck_assert( strncmp(namerex, "\\s+", len) == 0 );
}
END_TEST
2014-05-23 01:49:18 -04:00
START_TEST (test_slug_find_placeholder)
2014-05-18 08:11:07 -04:00
{
int slug_len = 0;
2014-05-20 09:38:59 -04:00
char * slug;
2014-05-23 01:49:18 -04:00
slug = slug_find_placeholder("/user/{name:\\s+}/to/{id}", &slug_len);
2014-05-18 08:11:07 -04:00
ck_assert( strncmp(slug, "{name:\\s+}", slug_len) == 0 );
2014-05-20 09:38:59 -04:00
2014-05-23 01:49:18 -04:00
slug = slug_find_placeholder("/user/{idx:\\d{3}}/to/{idy:\\d{3}}", &slug_len);
2014-05-20 09:38:59 -04:00
ck_assert( slug_len == strlen("{idx:\\d{3}}") );
ck_assert( strncmp(slug, "{idx:\\d{3}}", slug_len) == 0 );
2014-05-18 08:11:07 -04:00
}
END_TEST
START_TEST (test_inside_slug)
{
char * pattern = "/user/{name:\\s+}/to/{id}";
char * offset = strchr(pattern, '{') + 2;
2014-06-01 08:51:47 -04:00
ck_assert( (int)inside_slug(pattern, strlen(pattern), offset, NULL) );
ck_assert( *(inside_slug(pattern, strlen(pattern), offset, NULL)) == '{' );
ck_assert( ! inside_slug(pattern, strlen(pattern), pattern, NULL) );
2014-05-18 08:11:07 -04:00
}
END_TEST
2014-06-01 12:27:30 -04:00
START_TEST (test_incomplete_slug)
{
int cnt = 0;
char * errstr = NULL;
char * pattern = "/user/{name:\\d{3}}/to/{id";
cnt = slug_count(pattern, strlen(pattern), &errstr);
ck_assert_int_eq(cnt, -1);
2014-06-01 12:27:30 -04:00
ck_assert(errstr);
printf("%s\n",errstr);
2014-06-01 18:56:36 -04:00
free(errstr);
2014-06-01 12:27:30 -04:00
}
END_TEST
2014-06-01 13:01:29 -04:00
/*
2014-06-01 13:01:29 -04:00
START_TEST (test_slug_parse_with_pattern)
{
char * pattern = "/user/{name:\\d{3}}";
char * errstr = NULL;
r3_slug_t s;
int ret;
ret = r3_slug_parse(&s, pattern, strlen(pattern), pattern, &errstr);
ck_assert(ret);
2014-06-01 13:01:29 -04:00
char * out = r3_slug_to_str(&s);
ck_assert(out);
2014-06-01 13:01:29 -04:00
printf("%s\n",out);
free(out);
2014-06-01 13:01:29 -04:00
}
END_TEST
START_TEST (test_slug_parse_without_pattern)
{
char * pattern = "/user/{name}";
char * errstr = NULL;
r3_slug_t *s = r3_slug_new(pattern, strlen(pattern));
int ret;
ret = r3_slug_parse(s, pattern, strlen(pattern), pattern, &errstr);
ck_assert(s);
2014-06-01 13:01:29 -04:00
char * out = r3_slug_to_str(s);
ck_assert(out);
2014-06-01 13:01:29 -04:00
printf("%s\n",out);
free(out);
2014-06-01 13:01:29 -04:00
r3_slug_free(s);
}
END_TEST
*/
2014-06-01 13:01:29 -04:00
2014-05-20 11:21:15 -04:00
START_TEST (test_slug_count)
2014-05-18 08:11:07 -04:00
{
2014-06-01 12:22:12 -04:00
int cnt = 0;
2014-05-18 08:11:07 -04:00
char * pattern = "/user/{name:\\s+}/to/{id}";
2014-06-01 12:22:12 -04:00
char * errstr = NULL;
cnt = slug_count(pattern, strlen(pattern), &errstr);
ck_assert_int_eq(cnt, 2);
2014-06-01 18:56:36 -04:00
if(errstr) free(errstr);
2014-05-18 08:11:07 -04:00
char * pattern2 = "/user/{name:\\d{3}}/to/{id}";
2014-06-01 12:22:12 -04:00
cnt = slug_count(pattern2, strlen(pattern2), &errstr);
ck_assert_int_eq(cnt, 2);
2014-06-01 18:56:36 -04:00
if(errstr) free(errstr);
2014-06-01 12:27:30 -04:00
char * pattern3 = "/user/{name:\\d{3}}/to/{id}";
cnt = slug_count(pattern3, strlen(pattern3), &errstr);
ck_assert_int_eq(cnt, 2);
2014-06-01 18:56:36 -04:00
if(errstr) free(errstr);
2014-05-18 08:11:07 -04:00
}
END_TEST
2014-05-23 01:49:18 -04:00
START_TEST (test_slug_find_placeholder_with_broken_slug)
2014-05-18 08:11:07 -04:00
{
int slug_len = 0;
2014-05-23 01:49:18 -04:00
char * slug = slug_find_placeholder("/user/{name:\\s+/to/{id", &slug_len);
2014-06-01 12:22:12 -04:00
ck_assert(slug == 0);
2014-05-18 08:11:07 -04:00
}
END_TEST
Suite* r3_suite (void) {
Suite *suite = suite_create("slug test");
TCase *tcase = tcase_create("test_slug");
2014-05-22 10:06:11 -04:00
tcase_set_timeout(tcase, 30);
2014-05-18 08:11:07 -04:00
tcase_add_test(tcase, test_contains_slug);
tcase_add_test(tcase, test_inside_slug);
2014-05-23 01:49:18 -04:00
tcase_add_test(tcase, test_slug_find_pattern);
tcase_add_test(tcase, test_slug_find_placeholder);
tcase_add_test(tcase, test_slug_find_placeholder_with_broken_slug);
2014-05-20 11:21:15 -04:00
tcase_add_test(tcase, test_slug_count);
2014-05-20 11:25:55 -04:00
tcase_add_test(tcase, test_slug_compile);
2014-05-22 23:25:25 -04:00
tcase_add_test(tcase, test_pattern_to_opcode);
2014-06-01 12:27:30 -04:00
tcase_add_test(tcase, test_incomplete_slug);
// tcase_add_test(tcase, test_slug_parse_with_pattern);
// tcase_add_test(tcase, test_slug_parse_without_pattern);
2014-05-18 08:11:07 -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;
}