HelenOS sources
This source file includes following definitions.
- pcut_is_arg_with_number
- pcut_find_by_id
- run_suite
- set_setup_teardown_callbacks
- pcut_main
#include "internal.h"
#include "report/report.h"
#pragma warning(push, 0)
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#pragma warning(pop)
int pcut_run_mode = PCUT_RUN_MODE_FORKING;
static pcut_main_extra_t empty_main_extra[] = {
PCUT_MAIN_EXTRA_SET_LAST
};
#define FOR_EACH_MAIN_EXTRA(extras, it) \
for (it = extras; it->type != PCUT_MAIN_EXTRA_LAST; it++)
int pcut_is_arg_with_number(const char *arg, const char *opt, int *value) {
int opt_len = pcut_str_size(opt);
if (! pcut_str_start_equals(arg, opt, opt_len)) {
return 0;
}
*value = pcut_str_to_int(arg + opt_len);
return 1;
}
static pcut_item_t *pcut_find_by_id(pcut_item_t *first, int id) {
pcut_item_t *it = pcut_get_real(first);
while (it != NULL) {
if (it->id == id) {
return it;
}
it = pcut_get_real_next(it);
}
return NULL;
}
static int run_suite(pcut_item_t *suite, pcut_item_t **last, const char *prog_path) {
int is_first_test = 1;
int total_count = 0;
int ret_code = PCUT_OUTCOME_PASS;
int ret_code_tmp;
pcut_item_t *it = pcut_get_real_next(suite);
if ((it == NULL) || (it->kind == PCUT_KIND_TESTSUITE)) {
goto leave_no_print;
}
for (; it != NULL; it = pcut_get_real_next(it)) {
if (it->kind == PCUT_KIND_TESTSUITE) {
goto leave_ok;
}
if (it->kind != PCUT_KIND_TEST) {
continue;
}
if (is_first_test) {
pcut_report_suite_start(suite);
is_first_test = 0;
}
if (pcut_run_mode == PCUT_RUN_MODE_FORKING) {
ret_code_tmp = pcut_run_test_forking(prog_path, it);
} else {
ret_code_tmp = pcut_run_test_single(it);
}
if (ret_code_tmp != PCUT_OUTCOME_PASS) {
ret_code = PCUT_OUTCOME_FAIL;
}
total_count++;
}
leave_ok:
if (total_count > 0) {
pcut_report_suite_done(suite);
}
leave_no_print:
if (last != NULL) {
*last = it;
}
return ret_code;
}
static void set_setup_teardown_callbacks(pcut_item_t *first) {
pcut_item_t *active_suite = NULL;
pcut_item_t *it;
for (it = first; it != NULL; it = pcut_get_real_next(it)) {
if (it->kind == PCUT_KIND_TESTSUITE) {
active_suite = it;
} else if (it->kind == PCUT_KIND_SETUP) {
if (active_suite != NULL) {
active_suite->setup_func = it->setup_func;
}
it->kind = PCUT_KIND_SKIP;
} else if (it->kind == PCUT_KIND_TEARDOWN) {
if (active_suite != NULL) {
active_suite->teardown_func = it->teardown_func;
}
it->kind = PCUT_KIND_SKIP;
} else {
}
}
}
int pcut_main(pcut_item_t *last, int argc, char *argv[]) {
pcut_item_t *items = pcut_fix_list_get_real_head(last);
pcut_item_t *it;
pcut_main_extra_t *main_extras = last->main_extras;
pcut_main_extra_t *main_extras_it;
int run_only_suite = -1;
int run_only_test = -1;
int rc, rc_tmp;
if (main_extras == NULL) {
main_extras = empty_main_extra;
}
pcut_report_register_handler(&pcut_report_tap);
FOR_EACH_MAIN_EXTRA(main_extras, main_extras_it) {
if (main_extras_it->type == PCUT_MAIN_EXTRA_REPORT_XML) {
pcut_report_register_handler(&pcut_report_xml);
}
if (main_extras_it->type == PCUT_MAIN_EXTRA_PREINIT_HOOK) {
main_extras_it->preinit_hook(&argc, &argv);
}
}
if (argc > 1) {
int i;
for (i = 1; i < argc; i++) {
pcut_is_arg_with_number(argv[i], "-s", &run_only_suite);
pcut_is_arg_with_number(argv[i], "-t", &run_only_test);
if (pcut_str_equals(argv[i], "-l")) {
pcut_print_tests(items);
return PCUT_OUTCOME_PASS;
}
if (pcut_str_equals(argv[i], "-x")) {
pcut_report_register_handler(&pcut_report_xml);
}
#ifndef PCUT_NO_LONG_JUMP
if (pcut_str_equals(argv[i], "-u")) {
pcut_run_mode = PCUT_RUN_MODE_SINGLE;
}
#endif
}
}
setvbuf(stdout, NULL, _IONBF, 0);
set_setup_teardown_callbacks(items);
FOR_EACH_MAIN_EXTRA(main_extras, main_extras_it) {
if (main_extras_it->type == PCUT_MAIN_EXTRA_INIT_HOOK) {
main_extras_it->init_hook();
}
}
PCUT_DEBUG("run_only_suite = %d run_only_test = %d", run_only_suite, run_only_test);
if ((run_only_suite >= 0) && (run_only_test >= 0)) {
printf("Specify either -s or -t!\n");
return PCUT_OUTCOME_BAD_INVOCATION;
}
if (run_only_suite > 0) {
pcut_item_t *suite = pcut_find_by_id(items, run_only_suite);
if (suite == NULL) {
printf("Suite not found, aborting!\n");
return PCUT_OUTCOME_BAD_INVOCATION;
}
if (suite->kind != PCUT_KIND_TESTSUITE) {
printf("Invalid suite id!\n");
return PCUT_OUTCOME_BAD_INVOCATION;
}
run_suite(suite, NULL, argv[0]);
return PCUT_OUTCOME_PASS;
}
if (run_only_test > 0) {
pcut_item_t *test = pcut_find_by_id(items, run_only_test);
if (test == NULL) {
printf("Test not found, aborting!\n");
return PCUT_OUTCOME_BAD_INVOCATION;
}
if (test->kind != PCUT_KIND_TEST) {
printf("Invalid test id!\n");
return PCUT_OUTCOME_BAD_INVOCATION;
}
if (pcut_run_mode == PCUT_RUN_MODE_SINGLE) {
rc = pcut_run_test_single(test);
} else {
rc = pcut_run_test_forked(test);
}
return rc;
}
pcut_report_init(items);
rc = PCUT_OUTCOME_PASS;
it = items;
while (it != NULL) {
if (it->kind == PCUT_KIND_TESTSUITE) {
pcut_item_t *tmp;
rc_tmp = run_suite(it, &tmp, argv[0]);
if (rc_tmp != PCUT_OUTCOME_PASS) {
rc = rc_tmp;
}
it = tmp;
} else {
it = pcut_get_real_next(it);
}
}
pcut_report_done();
return rc;
}
HelenOS homepage, sources at GitHub