Skip to content

Commit ec4b0ea

Browse files
update logic to ensure no test go without an assert
1 parent 8c3623d commit ec4b0ea

File tree

1 file changed

+85
-67
lines changed

1 file changed

+85
-67
lines changed

code/logic/testing.c

Lines changed: 85 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ const char *timeout_messages[] = {
280280
#endif
281281

282282
jmp_buf test_jump_buffer; // This will hold the jump buffer for longjmp
283+
static int _ASSERT_COUNT = 0; // Counter for the number of assertions
283284

284285

285286
// Custom implementation of strdup to avoid warnings on some platforms
@@ -457,7 +458,7 @@ void fossil_test_register_suite(fossil_test_env_t *env, test_suite_t *suite) {
457458
suite->next = env->test_suites;
458459
env->test_suites = suite;
459460
if (env->options.show_info) {
460-
printf(COLOR_INFO "Registered test suite: %s\n" COLOR_RESET, suite->name);
461+
printf(FOSSIL_TEST_COLOR_BLUE "Registered test suite: %s\n" FOSSIL_TEST_COLOR_RESET, suite->name);
461462
}
462463
}
463464

@@ -505,6 +506,58 @@ void fossil_test_case_teardown(test_case_t *test_case) {
505506
}
506507
}
507508

509+
// Run all test cases in a test suite
510+
void fossil_test_run_suite(test_suite_t *suite, fossil_test_env_t *env) {
511+
if (!suite) return;
512+
513+
if (env->options.show_info) {
514+
printf(FOSSIL_TEST_COLOR_BLUE "Running suite: %s\n" FOSSIL_TEST_COLOR_RESET, suite->name);
515+
}
516+
517+
if (env->options.shuffle_enabled){
518+
shuffle_test_cases(&suite->tests);
519+
}
520+
521+
if (env->options.reverse) {
522+
reverse_test_cases(&suite->tests);
523+
}
524+
525+
if (suite->suite_setup_func) {
526+
suite->suite_setup_func();
527+
}
528+
529+
double total_execution_time = 0.0;
530+
test_case_t *current_test = suite->tests;
531+
while (current_test) {
532+
fossil_test_run_case(current_test, env);
533+
total_execution_time += current_test->execution_time;
534+
current_test = current_test->next;
535+
}
536+
537+
if (suite->suite_teardown_func) {
538+
suite->suite_teardown_func();
539+
}
540+
541+
if (env->options.show_info) {
542+
printf(FOSSIL_TEST_COLOR_CYAN "Total execution time for suite %s: %.3f seconds\n" FOSSIL_TEST_COLOR_RESET, suite->name, total_execution_time);
543+
}
544+
}
545+
546+
// Internal function to handle assertions
547+
void fossil_test_assert_internal(bool condition, const char *message, const char *file, int line, const char *func) {
548+
_ASSERT_COUNT++; // Increment the assertion count
549+
550+
if (!condition) {
551+
printf(FOSSIL_TEST_COLOR_RED "Assertion failed: %s (%s:%d in %s)\n" FOSSIL_TEST_COLOR_RESET, message, file, line, func);
552+
longjmp(test_jump_buffer, 1); // Jump back to test case failure handler
553+
}
554+
}
555+
556+
// Function to check if a test case is empty (i.e., contains no assertions)
557+
bool is_test_case_empty(test_case_t *test_case) {
558+
return _ASSERT_COUNT == 0;
559+
}
560+
508561
// Run an individual test case
509562
void fossil_test_run_case(test_case_t *test_case, fossil_test_env_t *env) {
510563
if (!test_case) return;
@@ -517,86 +570,50 @@ void fossil_test_run_case(test_case_t *test_case, fossil_test_env_t *env) {
517570
clock_t test_start_time = clock();
518571
clock_t timeout_limit = test_start_time + 3 * 60 * CLOCKS_PER_SEC; // 3 minutes timeout
519572

573+
_ASSERT_COUNT = 0; // Reset assertion count before running the test
574+
520575
if (setjmp(env->env) == 0) {
521576
for (int i = 0; i < env->options.repeat_count; i++) {
522577
test_case->test_func();
523578
if (clock() > timeout_limit) {
524579
test_case->status = TEST_STATUS_TTIMEOUT;
525-
printf(COLOR_FAIL "TIMEOUT: " COLOR_INFO " %s\n" COLOR_RESET, test_case->name);
580+
printf(FOSSIL_TEST_COLOR_ORANGE "TIMEOUT: " FOSSIL_TEST_COLOR_BLUE " %s\n" FOSSIL_TEST_COLOR_RESET, test_case->name);
526581
break;
527582
}
528583
}
529584
} else {
530585
test_case->status = TEST_STATUS_FAIL;
531-
printf(COLOR_FAIL "FAIL: " COLOR_INFO " %s\n", test_case->name);
532-
printf("Failure Message: %s\n" COLOR_RESET, test_case->failure_message);
586+
printf(FOSSIL_TEST_COLOR_RED "FAIL: " FOSSIL_TEST_COLOR_BLUE " %s\n", test_case->name);
587+
printf("Failure Message: %s\n" FOSSIL_TEST_COLOR_RESET, test_case->failure_message);
533588
}
534589
test_case->execution_time = (double)(clock() - test_start_time) / CLOCKS_PER_SEC;
535590

591+
// Check if the test case is empty
592+
if (is_test_case_empty(test_case)) {
593+
printf(FOSSIL_TEST_COLOR_YELLOW "WARNING: " FOSSIL_TEST_COLOR_BLUE " %s contains no assertions\n" FOSSIL_TEST_COLOR_RESET, test_case->name);
594+
}
595+
536596
// Run teardown
537597
fossil_test_case_teardown(test_case);
538598

539599
// Log result
540600
if (test_case->status == TEST_STATUS_PASS) {
541601
if (env->options.show_info) {
542-
printf(COLOR_PASS "PASS: " COLOR_INFO " %s (%.3f seconds)\n" COLOR_RESET, test_case->name, test_case->execution_time);
602+
printf(FOSSIL_TEST_COLOR_GREEN "PASS: " FOSSIL_TEST_COLOR_BLUE " %s (%.3f seconds)\n" FOSSIL_TEST_COLOR_RESET, test_case->name, test_case->execution_time);
543603
}
544604
} else if (test_case->status == TEST_STATUS_FAIL) {
545605
env->fail_count++;
546606
} else if (test_case->status == TEST_STATUS_SKIP) {
547607
env->skip_count++;
548608
} else if (test_case->status == TEST_STATUS_TTIMEOUT) {
549609
env->timeout_count++;
610+
} else if (test_case->status == TEST_STATUS_EMPTY) {
611+
env->empty_count++;
550612
} else {
551613
env->unexpected_count++;
552614
}
553615
}
554616

555-
// Run all test cases in a test suite
556-
void fossil_test_run_suite(test_suite_t *suite, fossil_test_env_t *env) {
557-
if (!suite) return;
558-
559-
if (env->options.show_info) {
560-
printf(COLOR_INFO "Running suite: %s\n" COLOR_RESET, suite->name);
561-
}
562-
563-
if (env->options.shuffle_enabled){
564-
shuffle_test_cases(&suite->tests);
565-
}
566-
567-
if (env->options.reverse) {
568-
reverse_test_cases(&suite->tests);
569-
}
570-
571-
if (suite->suite_setup_func) {
572-
suite->suite_setup_func();
573-
}
574-
575-
double total_execution_time = 0.0;
576-
test_case_t *current_test = suite->tests;
577-
while (current_test) {
578-
fossil_test_run_case(current_test, env);
579-
total_execution_time += current_test->execution_time;
580-
current_test = current_test->next;
581-
}
582-
583-
if (suite->suite_teardown_func) {
584-
suite->suite_teardown_func();
585-
}
586-
587-
if (env->options.show_info) {
588-
printf(COLOR_CYAN "Total execution time for suite %s: %.3f seconds\n" COLOR_RESET, suite->name, total_execution_time);
589-
}
590-
}
591-
592-
// Internal function to handle assertions
593-
void fossil_test_assert_internal(bool condition, const char *message, const char *file, int line, const char *func) {
594-
if (!condition) {
595-
printf("Assertion failed: %s (%s:%d in %s)\n", message, file, line, func);
596-
longjmp(test_jump_buffer, 1); // Jump back to test case failure handler
597-
}
598-
}
599-
600617
void fossil_test_run_all(fossil_test_env_t *env) {
601618
test_suite_t *current_suite = env->test_suites;
602619

@@ -619,6 +636,7 @@ void fossil_test_init(fossil_test_env_t *env, int argc, char **argv) {
619636
env->pass_count = 0;
620637
env->fail_count = 0;
621638
env->skip_count = 0;
639+
env->empty_count = 0;
622640
env->total_tests = 0;
623641
env->timeout_count = 0;
624642
env->start_execution_time = clock();
@@ -632,16 +650,16 @@ void fossil_test_message(fossil_test_env_t *env) {
632650
// Seed random number generator
633651
srand(time(NULL));
634652

635-
if (env->pass_count == 0 && env->fail_count == 0 && env->skip_count == 0 && env->timeout_count == 0) {
636-
printf(COLOR_INFO "%s\n" COLOR_RESET, sarcastic_messages[rand() % 30]);
653+
if (env->pass_count == 0 && env->fail_count == 0 && env->skip_count == 0 && env->timeout_count == 0 && env->empty_count > 0) {
654+
printf(FOSSIL_TEST_COLOR_YELLOW FOSSIL_TEST_ATTR_ITATIC "%s\n" FOSSIL_TEST_COLOR_RESET, sarcastic_messages[rand() % 30]);
637655
} else if (env->fail_count > 0) {
638-
printf(COLOR_FAIL "%s\n" COLOR_RESET, humorous_messages[rand() % 30]);
656+
printf(FOSSIL_TEST_COLOR_RED FOSSIL_TEST_ATTR_ITATIC "%s\n" FOSSIL_TEST_COLOR_RESET, humorous_messages[rand() % 30]);
639657
} else if (env->pass_count > 0) {
640-
printf(COLOR_PASS "%s\n" COLOR_RESET, great_news_messages[rand() % 30]);
658+
printf(FOSSIL_TEST_COLOR_GREEN FOSSIL_TEST_ATTR_ITATIC "%s\n" FOSSIL_TEST_COLOR_RESET, great_news_messages[rand() % 30]);
641659
} else if (env->timeout_count > 0) {
642-
printf(COLOR_FAIL "%s\n" COLOR_RESET, timeout_messages[rand() % 30]);
660+
printf(FOSSIL_TEST_COLOR_ORANGE FOSSIL_TEST_ATTR_ITATIC "%s\n" FOSSIL_TEST_COLOR_RESET, timeout_messages[rand() % 30]);
643661
} else {
644-
printf(COLOR_INFO "Test results are in. Keep pushing, you're getting there! 💪\n" COLOR_RESET);
662+
printf(FOSSIL_TEST_COLOR_BLUE FOSSIL_TEST_ATTR_ITATIC "Test results are in. Keep pushing, you're getting there! 💪\n" FOSSIL_TEST_COLOR_RESET);
645663
}
646664
}
647665

@@ -672,25 +690,25 @@ void fossil_test_summary(fossil_test_env_t *env) {
672690
}
673691
env->end_execution_time = clock();
674692

675-
printf(COLOR_INFO "===================================================================" COLOR_RESET);
676-
printf(COLOR_INFO "\nFossil Test Summary:\n" COLOR_RESET);
677-
printf(COLOR_INFO "===================================================================\n" COLOR_RESET);
693+
printf(FOSSIL_TEST_COLOR_BLUE FOSSIL_TEST_ATTR_BOLD "===================================================================" FOSSIL_TEST_COLOR_RESET);
694+
printf(FOSSIL_TEST_COLOR_CYAN FOSSIL_TEST_ATTR_BOLD FOSSIL_TEST_ATTR_ITATIC "\nFossil Test Summary:\n" FOSSIL_TEST_COLOR_RESET);
695+
printf(FOSSIL_TEST_COLOR_BLUE FOSSIL_TEST_ATTR_BOLD "===================================================================\n" FOSSIL_TEST_COLOR_RESET);
678696

679-
printf(COLOR_INFO "Passed: " COLOR_PASS " %d\n" COLOR_RESET, env->pass_count);
680-
printf(COLOR_INFO "Failed: " COLOR_FAIL " %d\n" COLOR_RESET, env->fail_count);
681-
printf(COLOR_INFO "Skipped: " COLOR_SKIP " %d\n" COLOR_RESET, env->skip_count);
682-
printf(COLOR_INFO "Timeout: " COLOR_SKIP " %d\n" COLOR_RESET, env->timeout_count);
683-
printf(COLOR_INFO "Total: %d tests\n" COLOR_RESET, env->pass_count + env->fail_count + env->skip_count);
697+
printf(FOSSIL_TEST_COLOR_CYAN FOSSIL_TEST_ATTR_ITATIC "Passed: " FOSSIL_TEST_COLOR_ORANGE " %d\n" FOSSIL_TEST_COLOR_RESET, env->pass_count);
698+
printf(FOSSIL_TEST_COLOR_CYAN FOSSIL_TEST_ATTR_ITATIC "Failed: " FOSSIL_TEST_COLOR_ORANGE " %d\n" FOSSIL_TEST_COLOR_RESET, env->fail_count);
699+
printf(FOSSIL_TEST_COLOR_CYAN FOSSIL_TEST_ATTR_ITATIC "Skipped:" FOSSIL_TEST_COLOR_ORANGE " %d\n" FOSSIL_TEST_COLOR_RESET, env->skip_count);
700+
printf(FOSSIL_TEST_COLOR_CYAN FOSSIL_TEST_ATTR_ITATIC "Timeout:" FOSSIL_TEST_COLOR_ORANGE " %d\n" FOSSIL_TEST_COLOR_RESET, env->timeout_count);
701+
printf(FOSSIL_TEST_COLOR_CYAN FOSSIL_TEST_ATTR_ITATIC "Total: %d tests\n" FOSSIL_TEST_COLOR_RESET, env->pass_count + env->fail_count + env->skip_count);
684702

685703
// Optionally, you could add the total execution time summary here
686704
double total_execution_time = (double)(env->end_execution_time - env->start_execution_time) / CLOCKS_PER_SEC;
687705
int seconds = (int)total_execution_time;
688706
int milliseconds = (int)((total_execution_time - seconds) * 1000);
689707
int microseconds = (int)((total_execution_time - seconds - milliseconds / 1000.0) * 1000000);
690708

691-
printf(COLOR_INFO "===================================================================\n" COLOR_RESET);
692-
printf(COLOR_INFO "Execution time: (%.2d) seconds, (%.2d) milliseconds, (%.3d) microseconds\n" COLOR_RESET, seconds, milliseconds, microseconds);
693-
printf(COLOR_INFO "===================================================================\n" COLOR_RESET);
709+
printf(FOSSIL_TEST_COLOR_BLUE FOSSIL_TEST_ATTR_BOLD "===================================================================\n" FOSSIL_TEST_COLOR_RESET);
710+
printf(FOSSIL_TEST_COLOR_CYAN FOSSIL_TEST_ATTR_ITATIC "Execution time: (%.2d) seconds, (%.2d) milliseconds, (%.3d) microseconds\n" FOSSIL_TEST_COLOR_RESET, seconds, milliseconds, microseconds);
711+
printf(FOSSIL_TEST_COLOR_BLUE FOSSIL_TEST_ATTR_BOLD "===================================================================\n" FOSSIL_TEST_COLOR_RESET);
694712

695713
fossil_test_message(env);
696714
}

0 commit comments

Comments
 (0)