Example #1
0
int main(int argc, char * const *argv)
{
    int ret = 0;
#ifdef HAVE_GETOPT_LONG
    int c;
    struct option long_options[] = {
        {"help", 0, 0, 'h'},
        {"list", 0, 0, 'l'},
        {0, 0, 0, 0}
    };

    while ((c = getopt_long(argc, argv, "hl", long_options, NULL)) != -1) {
        switch (c) {
            case 'h':
                fprintf(stderr, "Usage: %s [test name]\n", argv[0]);
                fputs("\nOptions:\n", stderr);
                fputs("  -h, --help           Show this help message and exit\n", stderr);
                fputs("  -l, --list           List available tests and exit\n", stderr);
                exit(EXIT_SUCCESS);
            case 'l':
                fputs("Available test suites:\n", stdout);;
                printSuites(CU_get_registry()->pSuite, stdout);
                exit(EXIT_SUCCESS);
        }
    }

    if (optind < argc) {
        CU_pTestRegistry old_registry = CU_get_registry();
        CU_set_registry(CU_create_new_registry());

        while (optind < argc) {
            char *suite_name = strdup(argv[optind++]);
            char *test_name = strstr(suite_name, "::");

            if (test_name) {
                *test_name = '\0';
                test_name += 2;
            }

            if (copySuite(old_registry, suite_name, test_name) == 0) {
                fprintf(stderr, "Can't find test suite \"%s", suite_name);
                if (test_name)
                    fprintf(stderr, "::%s\"\n", test_name);
                else
                    fputs("\"\n", stderr);
                exit(EXIT_FAILURE);
            }

            free(suite_name);
        }
        CU_destroy_existing_registry(&old_registry);
    }
#endif

    CU_basic_set_mode(CU_BRM_VERBOSE);
    CU_set_output_filename(XML_TEST_REPORT);
    CU_basic_run_tests();
    ret = (CU_get_number_of_tests_failed() == 0) ? 0 : 1;
    CU_cleanup_registry();

    return ret;
}
Example #2
0
File: Util.c Project: buhlaw/cunit
CU_BOOL CU_check_for_single_runs(void)
{
    CU_pTestRegistry old_reg;
    CU_pSuite current_suite, active_suite;
    CU_pTest current_test;
    unsigned int i, j;
    CU_BOOL singleRunsFound = CU_TRUE;

    /* create a new registry and save our old one */
    old_reg = CU_set_registry(CU_create_new_registry());

    /* go through all suites and tests in the old registry looking for the marker */
    current_suite = old_reg->pSuite;
    for (i = 0; i < old_reg->uiNumberOfSuites; i++)
    {
        active_suite = NULL;
        /* see if the current suite has the marker */
        if (strstr(current_suite->pName, "[x]") == NULL)
        {
            /* marker was not found so move through the tests */
            current_test = current_suite->pTest;
            for (j = 0; j < current_suite->uiNumberOfTests; j++)
            {
                /* see if the current test has the marker */
                if (strstr(current_test->pName, "[x]") != NULL)
                {
                    /* add the current suite if required */
                    if (active_suite == NULL)
                    {
                        active_suite = CU_add_suite(current_suite->pName, current_suite->pInitializeFunc, current_suite->pCleanupFunc);
                    }
                    /* now add the current test to the active suite */
                    CU_add_test(active_suite, current_test->pName, current_test->pTestFunc);
                }
                /* go to the next test */
                current_test = current_test->pNext;
            }
        }
        else
        {
            /* marker was found so add the entire current suite */
            active_suite = CU_add_suite(current_suite->pName, current_suite->pInitializeFunc, current_suite->pCleanupFunc);
            current_test = current_suite->pTest;
            for (j = 0; j < current_suite->uiNumberOfTests; j++)
            {
                /* add the current test to the active suite */
                CU_add_test(active_suite, current_test->pName, current_test->pTestFunc);
                /* go to the next test */
                current_test = current_test->pNext;
            }
        }
        /* go to the next suite */
        current_suite = current_suite->pNext;
    }

    /* if no single runs were found then restore the old registery */
    if (CU_get_registry()->uiNumberOfSuites == 0)
    {
        old_reg = CU_set_registry(old_reg);
        singleRunsFound = CU_FALSE;
    }
    /* destroy the old registry */
    CU_destroy_existing_registry(&old_reg);

    return singleRunsFound;
}