Commit f21cca03 authored by Gautier Pelloux-Prayer's avatar Gautier Pelloux-Prayer
Browse files

tester: update common

parent 31228812
......@@ -80,8 +80,9 @@ static unsigned char curses = 0;
char* xml_file = "CUnitAutomated-Results.xml";
int xml_enabled = 0;
char * suite_name;
char * test_name;
char * suite_name = NULL;
char * test_name = NULL;
char * tag_name = NULL;
static long max_vm_kb = 0;
void (*tester_printf_va)(int level, const char *format, va_list args);
......@@ -93,14 +94,38 @@ void bc_tester_printf(int level, const char *format, ...) {
va_end (args);
}
int bc_tester_run_suite(test_suite_t *suite) {
int bc_tester_run_suite(test_suite_t *suite, const char *tag_name) {
int i;
CU_pSuite pSuite = CU_add_suite(suite->name, suite->before_all, suite->after_all);
for (i = 0; i < suite->nb_tests; i++) {
if (NULL == CU_add_test(pSuite, suite->tests[i].name, suite->tests[i].func)) {
return CU_get_error();
CU_pSuite pSuite;
if (tag_name != NULL) {
int j;
int nb_tests_for_tag = 0;
for (i = 0; i < suite->nb_tests; i++) {
for (j = 0; j < (sizeof(suite->tests[i].tags) / sizeof(suite->tests[i].tags[0])); j++) {
if ((suite->tests[i].tags[j] != NULL) && (strcasecmp(tag_name, suite->tests[i].tags[j]) == 0)) {
nb_tests_for_tag++;
}
}
}
if (nb_tests_for_tag > 0) {
pSuite = CU_add_suite(suite->name, suite->before_all, suite->after_all);
for (i = 0; i < suite->nb_tests; i++) {
for (j = 0; j < (sizeof(suite->tests[i].tags) / sizeof(suite->tests[i].tags[0])); j++) {
if ((suite->tests[i].tags[j] != NULL) && (strcasecmp(tag_name, suite->tests[i].tags[j]) == 0)) {
if (NULL == CU_add_test(pSuite, suite->tests[i].name, suite->tests[i].func)) {
return CU_get_error();
}
}
}
}
}
} else {
pSuite = CU_add_suite(suite->name, suite->before_all, suite->after_all);
for (i = 0; i < suite->nb_tests; i++) {
if (NULL == CU_add_test(pSuite, suite->tests[i].name, suite->tests[i].func)) {
return CU_get_error();
}
}
}
......@@ -124,6 +149,19 @@ int bc_tester_suite_index(const char *suite_name) {
return -1;
}
int bc_tester_test_index(test_suite_t *suite, const char *test_name) {
int i;
for (i = 0; i < suite->nb_tests; i++) {
if (strcmp(test_name, suite->tests[i].name) == 0) {
return i;
}
}
return -1;
}
int bc_tester_nb_suites(void) {
return nb_test_suites;
}
......@@ -222,7 +260,11 @@ static void test_complete_message_handler(const CU_pTest pTest, const CU_pSuite
free(result);
if (test_suite[suite_index]->after_each) {
test_suite[suite_index]->after_each();
int err = test_suite[suite_index]->after_each();
//if test passed but not after_each, count it as failure
if (err && !pFailure) {
CU_get_run_summary()->nTestsFailed++;
}
}
//insert empty line
bc_tester_printf(bc_printf_verbosity_info,"");
......@@ -251,7 +293,7 @@ static void test_complete_message_handler(const CU_pTest pTest, const CU_pSuite
}
#endif
int bc_tester_run_tests(const char *suite_name, const char *test_name) {
int bc_tester_run_tests(const char *suite_name, const char *test_name, const char *tag_name) {
int i;
/* initialize the CUnit test registry */
......@@ -259,7 +301,7 @@ int bc_tester_run_tests(const char *suite_name, const char *test_name) {
return CU_get_error();
for (i = 0; i < nb_test_suites; i++) {
bc_tester_run_suite(test_suite[i]);
bc_tester_run_suite(test_suite[i], tag_name);
}
#ifdef HAVE_CU_GET_SUITE
CU_set_suite_start_handler(suite_start_message_handler);
......@@ -284,13 +326,21 @@ int bc_tester_run_tests(const char *suite_name, const char *test_name) {
CU_pSuite suite;
suite=CU_get_suite(suite_name);
if (!suite) {
bc_tester_printf(bc_printf_verbosity_error, "Could not find suite '%s'. Available suites are:", suite_name);
if (tag_name != NULL) {
bc_tester_printf(bc_printf_verbosity_error, "Could not find suite '%s' or this suite has no tests with tag '%s'. Available suites are:", suite_name, tag_name);
} else {
bc_tester_printf(bc_printf_verbosity_error, "Could not find suite '%s'. Available suites are:", suite_name);
}
bc_tester_list_suites();
return -1;
} else if (test_name) {
CU_pTest test=CU_get_test_by_name(test_name, suite);
if (!test) {
bc_tester_printf(bc_printf_verbosity_error, "Could not find test '%s' in suite '%s'. Available tests are:", test_name, suite_name);
if (tag_name != NULL) {
bc_tester_printf(bc_printf_verbosity_error, "Could not find test '%s' in suite '%s' or this test is not tagged '%s'. Available tests are:", test_name, suite_name, tag_name);
} else {
bc_tester_printf(bc_printf_verbosity_error, "Could not find test '%s' in suite '%s'. Available tests are:", test_name, suite_name);
}
// do not use suite_name here, since this method is case sensitive
bc_tester_list_tests(suite->pName);
return -2;
......@@ -464,9 +514,12 @@ int bc_tester_parse_args(int argc, char **argv, int argid)
} else if (strcmp(argv[i],"--test")==0){
CHECK_ARG("--test", ++i, argc);
test_name=argv[i];
}else if (strcmp(argv[i],"--suite")==0){
} else if (strcmp(argv[i],"--suite")==0){
CHECK_ARG("--suite", ++i, argc);
suite_name=argv[i];
} else if (strcmp(argv[i], "--tag") == 0) {
CHECK_ARG("--tag", ++i, argc);
tag_name = argv[i];
} else if (strcmp(argv[i],"--list-suites")==0){
bc_tester_list_suites();
return 0;
......@@ -518,7 +571,7 @@ int bc_tester_start(const char* prog_name) {
free(xml_tmp_file);
}
ret = bc_tester_run_tests(suite_name, test_name);
ret = bc_tester_run_tests(suite_name, test_name, tag_name);
return ret;
}
......@@ -656,6 +709,10 @@ char* bc_sprintf(const char* format, ...) {
return res;
}
void bc_free(void *ptr) {
free(ptr);
}
const char * bc_tester_current_suite_name(void) {
return bc_current_suite_name;
}
......@@ -663,3 +720,12 @@ const char * bc_tester_current_suite_name(void) {
const char * bc_tester_current_test_name(void) {
return bc_current_test_name;
}
const char ** bc_tester_current_test_tags(void) {
if (bc_current_suite_name && bc_current_test_name) {
int suite_index = bc_tester_suite_index(bc_current_suite_name);
int test_index = bc_tester_test_index(test_suite[suite_index], bc_current_test_name);
return test_suite[suite_index]->tests[test_index].tags;
}
return NULL;
}
......@@ -27,6 +27,7 @@
#ifdef _WIN32
#ifndef snprintf
#define snprintf _snprintf
#define strcasecmp _stricmp
#endif
#ifndef strdup
#define strdup _strdup
......@@ -46,15 +47,23 @@ typedef int (*pre_post_function_t)(void);
typedef struct {
const char *name;
test_function_t func;
const char *tags[2];
} test_t;
#define TEST_NO_TAG(name, func) \
{ name, func, { NULL, NULL } }
#define TEST_ONE_TAG(name, func, tag) \
{ name, func, { tag, NULL } }
#define TEST_TWO_TAGS(name, func, tag1, tag2) \
{ name, func, { tag1, tag2 } }
typedef struct {
const char *name; /*suite name*/
pre_post_function_t
before_all; /*function invoked before running the suite. If not returning 0, suite is not launched. */
pre_post_function_t after_all; /*function invoked at the end of the suite, even if some tests failed. */
test_function_t before_each; /*function invoked before each test within this suite. */
test_function_t after_each; /*function invoked after each test within this suite, even if it failed. */
pre_post_function_t after_each; /*function invoked after each test within this suite, even if it failed. */
int nb_tests; /* number of tests */
test_t *tests; /* tests within this suite */
} test_suite_t;
......@@ -89,14 +98,16 @@ void bc_tester_list_suites(void);
void bc_tester_list_tests(const char *suite_name);
const char * bc_tester_suite_name(int suite_index);
const char * bc_tester_test_name(const char *suite_name, int test_index);
int bc_tester_run_suite(test_suite_t *suite);
int bc_tester_run_tests(const char *suite_name, const char *test_name);
int bc_tester_run_suite(test_suite_t *suite, const char *tag_name);
int bc_tester_run_tests(const char *suite_name, const char *test_name, const char *tag_name);
int bc_tester_suite_index(const char *suite_name);
const char * bc_tester_current_suite_name(void);
const char * bc_tester_current_test_name(void);
const char ** bc_tester_current_test_tags(void);
char* bc_sprintfva(const char* format, va_list args);
char* bc_sprintf(const char* format, ...);
void bc_free(void *ptr);
/**
* Get full path to the given resource
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment