Line data Source code
1 : #include "testitall.h"
2 :
3 : bool show_subheader = false;
4 :
5 : /**
6 : * @brief Main testing framework function that executes and evaluates test cases
7 : *
8 : * @param func Pointer to the test function to be executed
9 : * @param function_name String containing the name of the function being tested
10 : * @param test_description String containing description of the test
11 : *
12 : * @return Return Status code indicating test results:
13 : * - SUCCESS: Test passed successfully
14 : * - FAILURE: Test failed
15 : *
16 : * @details This function provides a framework for executing test cases and capturing their output.
17 : * It measures execution time, captures stdout/stderr output, and formats the results
18 : * in a readable way with color coding.
19 : */
20 164 : Return testitall(
21 : Return (*func)(void),
22 : const char *function_name,
23 : const char *test_description)
24 : {
25 : /// The status that will be passed to return() before exiting.
26 : /// By default, the function worked without errors.
27 164 : Return status = SUCCESS;
28 :
29 : /* Start time measurement */
30 164 : long long int __start_time = cur_time_ns();
31 :
32 : /* Clear output capture buffers to ensure clean state */
33 164 : call(del(STDOUT));
34 164 : call(del(STDERR));
35 :
36 164 : if(show_subheader == true)
37 : {
38 25 : printf(BLUE " ยป %s" RESET "\n",test_description);
39 25 : show_subheader = false;
40 : }
41 :
42 : /* Execute the test function and capture its return status */
43 164 : status = func();
44 :
45 : /* Calculate execution time */
46 164 : long long int __end_time = cur_time_ns();
47 164 : long long int elapsed_time = __end_time - __start_time;
48 :
49 : /* Format and display test results with color coding */
50 164 : if(SUCCESS == status)
51 : {
52 : /* Green OK for passed tests */
53 164 : fprintf(stdout,WHITE "[ " BOLDGREEN "OK" RESET WHITE " ]" RESET );
54 164 : fprintf(stdout,WHITE " %lldns %s %s" RESET,elapsed_time,function_name,test_description);
55 :
56 : /* Display any additional info captured in EXTEND buffer */
57 164 : const char *extend_buffer = getcstring(EXTEND);
58 :
59 164 : if((EXTEND->length > 0) && (extend_buffer[0] != '\0'))
60 : {
61 162 : fprintf(stdout,WHITE " %s" RESET,extend_buffer);
62 : }
63 164 : fprintf(stdout,"\n");
64 :
65 0 : } else if(SKIPPED == status){
66 : /* Green OK for passed tests */
67 0 : fprintf(stdout,WHITE "[ " BOLDYELLOW "SKIP" RESET WHITE " ]" RESET );
68 0 : fprintf(stdout,WHITE " %lldns %s %s" RESET,elapsed_time,function_name,test_description);
69 :
70 : /* Display any additional info captured in EXTEND buffer */
71 0 : const char *extend_buffer = getcstring(EXTEND);
72 :
73 0 : if((EXTEND->length > 0) && (extend_buffer[0] != '\0'))
74 : {
75 0 : fprintf(stdout,WHITE " %s" RESET,extend_buffer);
76 : }
77 0 : fprintf(stdout,"\n");
78 0 : status = SUCCESS;
79 : } else {
80 : /* Red FAIL for failed tests */
81 0 : fprintf(stdout,WHITE "[ " BOLDRED "FAIL" RESET WHITE " ]" RESET );
82 0 : fprintf(stdout,WHITE " %lldns %s %s" RESET,elapsed_time,function_name,test_description);
83 :
84 : /* Display any additional info captured in EXTEND buffer */
85 0 : const char *extend_buffer = getcstring(EXTEND);
86 :
87 0 : if((EXTEND->length > 0) && (extend_buffer[0] != '\0'))
88 : {
89 0 : fprintf(stdout,WHITE " %s" RESET,extend_buffer);
90 : }
91 0 : fprintf(stdout,"\n");
92 : }
93 :
94 : /* Display captured stderr output in yellow */
95 164 : const char *stderr_buffer = getcstring(STDERR);
96 :
97 164 : if((STDERR->length > 0) && (stderr_buffer[0] != '\0'))
98 : {
99 0 : fprintf(stdout,RED "STDERR" RESET " " WHITE "is not empty when it should be:\n" RESET);
100 0 : fprintf(stdout,"%s",stderr_buffer);
101 : }
102 :
103 : /* Display captured stdout output */
104 164 : const char *stdout_buffer = getcstring(STDOUT);
105 :
106 164 : if((STDOUT->length > 0) && (stdout_buffer[0] != '\0'))
107 : {
108 0 : fprintf(stdout,BLUE "STDOUT" RESET " " WHITE "is not empty when it should be:\n" RESET);
109 0 : fprintf(stdout,"%s",stdout_buffer);
110 : }
111 :
112 : /* Cleanup: free dynamically allocated buffers */
113 164 : call(del(STDOUT));
114 164 : call(del(STDERR));
115 164 : call(del(EXTEND));
116 :
117 164 : return(status);
118 : }
|