diff options
| author | nodist <kevin.comas.git@gmail.com> | 2026-09-16 11:03:29 -0400 |
|---|---|---|
| committer | nodist <kevin.comas.git@gmail.com> | 2026-09-16 11:03:29 -0400 |
| commit | c4dcbaa49a43ac4b536c50baafad41e3f6e4a43c (patch) | |
| tree | 140d6559ead0348cb52fa2824348e3b5b5939e73 /test | |
| parent | b6a923c66b14d3b6fd3cc5f13af04e06807da808 (diff) | |
add testing macros and all test runner
Diffstat (limited to 'test')
| -rw-r--r-- | test/README | 6 | ||||
| -rw-r--r-- | test/test.c | 89 |
2 files changed, 87 insertions, 8 deletions
diff --git a/test/README b/test/README index 43d6110..f1c86df 100644 --- a/test/README +++ b/test/README @@ -18,7 +18,7 @@ This is the only include a .c file in the test folder needs Writing Tests: -TEST(test_name) { +KPL_TEST(test_name) { // TEST CODE } @@ -28,13 +28,13 @@ Tests pass by default Asserting: -ASSERT(CONDITION, FAIL_MESSAGE); +KPL_ASSERT(CONDITION, FAIL_MESSAGE); --- Failing: -FAIL(FAIL_MESSAGE); +KPL_FAIL(FAIL_MESSAGE); --- diff --git a/test/test.c b/test/test.c index afa8e26..5a09662 100644 --- a/test/test.c +++ b/test/test.c @@ -3,17 +3,78 @@ static kpl_test *_kpl_test_head = NULL, *_kpl_test_tail = NULL, *_kpl_error_head = NULL, *_kpl_error_tail = NULL; +static int32_t _kpl_test_count = 0, _kpl_test_error_count = 0; + void kpl_test_add(kpl_test *_test) { 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) { + printf(KPL_COLOR(BOLD) "%s" KPL_COLOR(RESET) ":" KPL_COLOR(LIGHT_CYAN) "%s" KPL_COLOR(RESET), + _test->file_name, _test->test_name); +} + +static void kpl_test_report_success(kpl_test *_test) { + printf(KPL_COLOR2(BOLD, LIGHT_GREEN) "PASSED " KPL_COLOR(RESET)); + kpl_test_report(_test); + putchar('\n'); +} + +static void kpl_test_report_error(kpl_test *_test, bool failed_status) { + if (failed_status) + printf(KPL_COLOR2(BOLD, LIGHT_RED) "FAILED " KPL_COLOR(RESET)); + kpl_test_report(_test); + printf(":" KPL_COLOR(LIGHT_MAGENTA) "%d" KPL_COLOR(RESET) "::" + KPL_COLOR(LIGHT_RED) "%s\n" KPL_COLOR(RESET), _test->error_line_number, _test->error_message); +} + +static int32_t kpl_test_error_list(void) { + if (!_kpl_error_tail) { + printf(KPL_COLOR2(BOLD, LIGHT_GREEN) "ALL TESTS PASSED\n" KPL_COLOR(RESET)); + return KPL_EXIT_CODE_SUCCESS; + } + printf(KPL_COLOR2(BOLD, LIGHT_RED) "FAILED TEST COUNT:" KPL_COLOR(RESET) + KPL_COLOR(WHITE) " %d\n" KPL_COLOR(RESET), _kpl_test_error_count); + while (_kpl_error_head) { + kpl_test_report_error(_kpl_error_head, false); + _kpl_error_head = _kpl_error_head->next_test; + } + return KPL_EXIT_CODE_ERROR; +} + +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); + return; + } + _kpl_test_error_count++; + kpl_test_report_error(_test, true); + if (_kpl_error_head) + _kpl_error_tail = _kpl_error_tail->next_test = _test; + else + _kpl_error_head = _kpl_error_tail = _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_run(_test); + } + return kpl_test_error_list(); } static int32_t kpl_test_help(const char *argv0) { printf("\nUsage: " KPL_COLOR(BOLD) "%s " KPL_COLOR(RESET) KPL_COLOR(WHITE) "[OPTIONS] [TEST NAMES]\n\n" KPL_COLOR(RESET), argv0); printf("Where " KPL_COLOR(WHITE) "[OPTIONS]" KPL_COLOR(RESET) " are:\n"); + printf(KPL_COLOR(LIGHT_YELLOW) "\t-a " KPL_COLOR(RESET) "Run All Tests\n"); printf(KPL_COLOR(LIGHT_YELLOW) "\t-h " KPL_COLOR(RESET) "Show Help\n"); printf(KPL_COLOR(LIGHT_YELLOW) "\t-l " KPL_COLOR(RESET) "List Test Names\n"); putchar('\n'); @@ -21,13 +82,23 @@ static int32_t kpl_test_help(const char *argv0) { } static int32_t kpl_test_list(void) { - // TODO + printf("\nTest Count: " KPL_COLOR(WHITE) "%d\n\n" + KPL_COLOR(RESET) KPL_COLOR(LIGHT_CYAN), _kpl_test_count); + while (_kpl_test_head) { + printf("%s ", _kpl_test_head->test_name); + _kpl_test_head = _kpl_test_head->next_test; + } + printf("\n\n"); return KPL_EXIT_CODE_HELP; } -static int32_t kpl_test_run(int32_t argc, char *argv[], const int32_t named_arg_position) { +static int32_t kpl_test_run_by_name(int32_t argc, char *argv[], const int32_t named_arg_position) { + if (named_arg_position == argc) { + printf(KPL_COLOR(LIGHT_RED) "\nNo Tests Specified\n" KPL_COLOR(RESET)); + return kpl_test_help(argv[0]); + } // TODO RUN - return _kpl_error_tail ? KPL_EXIT_CODE_ERROR : KPL_EXIT_CODE_SUCCESS; + return kpl_test_error_list(); } int32_t main(int32_t argc, char *argv[]) { @@ -36,14 +107,22 @@ int32_t main(int32_t argc, char *argv[]) { if (argv[arg_position][0] != '-' || !argv[arg_position][1] || argv[arg_position][2]) break; switch (argv[arg_position][1]) { + case 'a': + return kpl_test_run_all(); case 'h': return kpl_test_help(argv[0]); case 'l': return kpl_test_list(); default: - return kpl_test_run(argc, argv, arg_position); + return kpl_test_run_by_name(argc, argv, arg_position); } arg_position++; } - return kpl_test_run(argc, argv, arg_position); + return kpl_test_run_by_name(argc, argv, arg_position); +} + +KPL_TEST(example) { + // PASS BY DEFAULT + // KPL_FAIL("Failure Message"); + KPL_ASSERT(1 == 1, "Assert Failure Message"); } |
