From 63cc458fc3d0df10c91ca5c859a656845d27a8ad Mon Sep 17 00:00:00 2001 From: nodist Date: Wed, 16 Sep 2026 14:52:09 -0400 Subject: run test by matching string --- test/test.c | 46 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 12 deletions(-) (limited to 'test/test.c') diff --git a/test/test.c b/test/test.c index 5a09662..b232e4c 100644 --- a/test/test.c +++ b/test/test.c @@ -6,11 +6,11 @@ static kpl_test *_kpl_test_head = NULL, *_kpl_test_tail = NULL, *_kpl_error_head static int32_t _kpl_test_count = 0, _kpl_test_error_count = 0; void kpl_test_add(kpl_test *_test) { + _kpl_test_count++; if (_kpl_test_head) _kpl_test_tail = _kpl_test_tail->next_test = _test; else _kpl_test_head = _kpl_test_tail = _test; - _kpl_test_count++; } static void kpl_test_report(kpl_test *_test) { @@ -33,7 +33,7 @@ static void kpl_test_report_error(kpl_test *_test, bool failed_status) { } static int32_t kpl_test_error_list(void) { - if (!_kpl_error_tail) { + if (!_kpl_test_error_count) { printf(KPL_COLOR2(BOLD, LIGHT_GREEN) "ALL TESTS PASSED\n" KPL_COLOR(RESET)); return KPL_EXIT_CODE_SUCCESS; } @@ -47,7 +47,6 @@ static int32_t kpl_test_error_list(void) { } static void kpl_test_run(kpl_test *_test) { - _test->next_test = NULL; _test->test_function(_test); if (_test->error_line_number == -1) { kpl_test_report_success(_test); @@ -61,12 +60,19 @@ static void kpl_test_run(kpl_test *_test) { _kpl_error_head = _kpl_error_tail = _test; } +static kpl_test *kpl_test_next(void) { + if (!_kpl_test_head) + return NULL; + kpl_test *_test = _kpl_test_head; + _kpl_test_head = _kpl_test_head->next_test; + _test->next_test = NULL; + return _test; +} + static int32_t kpl_test_run_all(void) { - while (_kpl_test_head) { - kpl_test *_test = _kpl_test_head; - _kpl_test_head = _kpl_test_head->next_test; + kpl_test *_test = NULL; + while ((_test = kpl_test_next())) kpl_test_run(_test); - } return kpl_test_error_list(); } @@ -88,7 +94,7 @@ static int32_t kpl_test_list(void) { printf("%s ", _kpl_test_head->test_name); _kpl_test_head = _kpl_test_head->next_test; } - printf("\n\n"); + printf(KPL_COLOR(RESET) "\n\n"); return KPL_EXIT_CODE_HELP; } @@ -97,7 +103,19 @@ static int32_t kpl_test_run_by_name(int32_t argc, char *argv[], const int32_t na printf(KPL_COLOR(LIGHT_RED) "\nNo Tests Specified\n" KPL_COLOR(RESET)); return kpl_test_help(argv[0]); } - // TODO RUN + int32_t tests_ran = 0; + kpl_test *_test = NULL; + while ((_test = kpl_test_next())) + for (int32_t test_name_position = named_arg_position; test_name_position < argc; test_name_position++) + if (strcmp(argv[test_name_position], _test->test_name) == 0) { + kpl_test_run(_test); + tests_ran++; + } + if (!tests_ran) { + printf(KPL_COLOR(LIGHT_RED) "\nNo Matching Tests Found, for help run: " KPL_COLOR(RESET) + KPL_COLOR(BOLD) "%s -h\n\n" KPL_COLOR(RESET), argv[0]); + return KPL_EXIT_CODE_ERROR; + } return kpl_test_error_list(); } @@ -121,8 +139,12 @@ int32_t main(int32_t argc, char *argv[]) { return kpl_test_run_by_name(argc, argv, arg_position); } -KPL_TEST(example) { - // PASS BY DEFAULT +KPL_TEST(test_pass_by_default) { + // Pass by default + // Uncomment below to fail // KPL_FAIL("Failure Message"); - KPL_ASSERT(1 == 1, "Assert Failure Message"); +} + +KPL_TEST(test_counting) { + KPL_ASSERT(_kpl_test_count > 0, "Test counter not incrementing"); } -- cgit v1.2.3