diff --git a/utest.h b/utest.h index af41909..8f4ce9b 100644 --- a/utest.h +++ b/utest.h @@ -319,20 +319,55 @@ // extern to the global state utest needs to execute UTEST_EXTERN struct utest_state_s utest_state; +UTEST_WEAK int utest_should_filter_test(const char *filter, + const char *testcase); +UTEST_WEAK int utest_should_filter_test(const char *filter, + const char *testcase) { + if (filter) { + return strcmp(filter, testcase); + } else { + return 0; + } +} + UTEST_WEAK int utest_main(int argc, const char *const argv[]); UTEST_WEAK int utest_main(int argc, const char *const argv[]) { size_t failed = 0; size_t index = 0; size_t *failed_testcases = 0; size_t failed_testcases_length = 0; + const char *filter = 0; + size_t ran_tests = 0; - (void)argc; - (void)argv; + // loop through all arguments looking for our options + for (index = 1; index < UTEST_CAST(size_t, argc); index++) { + const char filter_str[] = "--filter="; + + if (0 < strcmp(argv[index], filter_str)) { + // user wants to filter what test cases run! + filter = argv[index] + strlen(filter_str); + } + } + + for (index = 0; index < utest_state.testcases_length; index++) { + if (utest_should_filter_test(filter, utest_state.testcase_names[index])) { + continue; + } + + ran_tests++; + } + printf("\033[32m[==========]\033[0m Running %" UTEST_PRIu64 " test cases.\n", - UTEST_CAST(uint64_t, utest_state.testcases_length)); + UTEST_CAST(uint64_t, ran_tests)); + for (index = 0; index < utest_state.testcases_length; index++) { int result = 0; int64_t ns = 0; + + if (utest_should_filter_test(filter, utest_state.testcase_names[index])) { + continue; + } + printf("\033[32m[ RUN ]\033[0m %s\n", utest_state.testcase_names[index]); ns = utest_ns(); @@ -353,9 +388,9 @@ } } printf("\033[32m[==========]\033[0m %" UTEST_PRIu64 " test cases ran.\n", - UTEST_CAST(uint64_t, utest_state.testcases_length)); + UTEST_CAST(uint64_t, ran_tests)); printf("\033[32m[ PASSED ]\033[0m %" UTEST_PRIu64 " tests.\n", - UTEST_CAST(uint64_t, utest_state.testcases_length - failed)); + UTEST_CAST(uint64_t, ran_tests - failed)); if (0 != failed) { printf("\033[31m[ FAILED ]\033[0m %" UTEST_PRIu64 " tests, listed below:\n",