check in files (slug tests)

This commit is contained in:
c9s 2014-05-18 20:11:07 +08:00
parent baf23fafb0
commit 4a0412d4e9
2 changed files with 119 additions and 0 deletions

15
include/r3_gvc.h Normal file
View file

@ -0,0 +1,15 @@
/*
* r3_gvc.h
* Copyright (C) 2014 c9s <c9s@c9smba.local>
*
* Distributed under terms of the MIT license.
*/
#ifndef R3_GVC_H
#define R3_GVC_H
#include <gvc.h>
void r3_tree_build_ag_nodes(Agraph_t * g, Agnode_t * ag_parent_node, node * n, int node_cnt);
#endif /* !R3_GVC_H */

104
tests/check_slug.c Normal file
View file

@ -0,0 +1,104 @@
/*
* 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"
#include "str_array.h"
START_TEST (test_compile_slug)
{
char * path = "/user/{id}";
ck_assert_str_eq( compile_slug(path, strlen(path) ) , "/user/([^/]+)" );
char * path2 = "/what/{id}-foo";
ck_assert_str_eq( compile_slug(path2, strlen(path2) ) , "/what/([^/]+)-foo" );
}
END_TEST
START_TEST (test_contains_slug)
{
char * path = "/user/{id}/{name}";
ck_assert( contains_slug(path) );
}
END_TEST
START_TEST (test_find_slug_pattern)
{
int len;
char * namerex = find_slug_pattern("{name:\\s+}", &len);
ck_assert( strncmp(namerex, "\\s+", len) == 0 );
}
END_TEST
START_TEST (test_find_slug_placeholder)
{
int slug_len = 0;
char * slug = find_slug_placeholder("/user/{name:\\s+}/to/{id}", &slug_len);
// printf("slug: '%s'\n", strndup(slug, slug_len) );
ck_assert( strncmp(slug, "{name:\\s+}", slug_len) == 0 );
}
END_TEST
START_TEST (test_inside_slug)
{
int slug_len = 0;
char * pattern = "/user/{name:\\s+}/to/{id}";
char * offset = strchr(pattern, '{') + 2;
ck_assert( inside_slug(pattern, strlen(pattern), offset) );
ck_assert( *(inside_slug(pattern, strlen(pattern), offset)) == '{' );
ck_assert( ! inside_slug(pattern, strlen(pattern), pattern) );
}
END_TEST
START_TEST (test_count_slug)
{
int slug_len = 0;
char * pattern = "/user/{name:\\s+}/to/{id}";
ck_assert_int_eq( count_slug(pattern, strlen(pattern) ), 2 );
char * pattern2 = "/user/{name:\\d{3}}/to/{id}";
ck_assert_int_eq( count_slug(pattern2, strlen(pattern) ), 2 );
}
END_TEST
START_TEST (test_find_slug_placeholder_with_broken_slug)
{
int slug_len = 0;
char * slug = find_slug_placeholder("/user/{name:\\s+/to/{id", &slug_len);
ck_assert(! slug);
}
END_TEST
Suite* r3_suite (void) {
Suite *suite = suite_create("slug test");
TCase *tcase = tcase_create("test_slug");
tcase_add_test(tcase, test_contains_slug);
tcase_add_test(tcase, test_inside_slug);
tcase_add_test(tcase, test_find_slug_pattern);
tcase_add_test(tcase, test_find_slug_placeholder);
tcase_add_test(tcase, test_find_slug_placeholder_with_broken_slug);
tcase_add_test(tcase, test_count_slug);
tcase_add_test(tcase, test_compile_slug);
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;
}