r3/tests/check_slug.c

132 lines
3.7 KiB
C
Raw 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"
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;
ck_assert_str_eq( c = slug_compile(path, strlen(path) ) , "/user/([^/]+)" );
zfree(c);
2014-05-18 08:11:07 -04:00
char * path2 = "/what/{id}-foo";
2014-05-21 06:50:45 -04:00
ck_assert_str_eq( c = slug_compile(path2, strlen(path2) ) , "/what/([^/]+)-foo" );
zfree(c);
2014-05-20 09:38:59 -04:00
char * path3 = "-{id}";
2014-05-21 06:50:45 -04:00
ck_assert_str_eq( c = slug_compile(path3, strlen(path3)), "-([^/]+)" );
zfree(c);
2014-05-20 09:38:59 -04:00
char * path4 = "-{idx:\\d{3}}";
2014-05-21 06:50:45 -04:00
ck_assert_str_eq( c = slug_compile(path4, strlen(path4)), "-(\\d{3})" );
zfree(c);
2014-05-18 08:11:07 -04:00
}
END_TEST
START_TEST (test_contains_slug)
{
char * path = "/user/{id}/{name}";
ck_assert( contains_slug(path) );
}
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)
{
int slug_len = 0;
char * pattern = "/user/{name:\\s+}/to/{id}";
char * offset = strchr(pattern, '{') + 2;
2014-05-21 03:50:37 -04:00
ck_assert( (int)inside_slug(pattern, strlen(pattern), offset) );
2014-05-18 08:11:07 -04:00
ck_assert( *(inside_slug(pattern, strlen(pattern), offset)) == '{' );
ck_assert( ! inside_slug(pattern, strlen(pattern), pattern) );
}
END_TEST
2014-05-20 11:21:15 -04:00
START_TEST (test_slug_count)
2014-05-18 08:11:07 -04:00
{
int slug_len = 0;
char * pattern = "/user/{name:\\s+}/to/{id}";
2014-05-20 11:21:15 -04:00
ck_assert_int_eq( slug_count(pattern, strlen(pattern) ), 2 );
2014-05-18 08:11:07 -04:00
char * pattern2 = "/user/{name:\\d{3}}/to/{id}";
2014-05-20 11:21:15 -04:00
ck_assert_int_eq( slug_count(pattern2, strlen(pattern) ), 2 );
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-05-18 08:11:07 -04:00
ck_assert(! slug);
}
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-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;
}