1919 3. This notice may not be removed or altered from any source distribution.
2020*/
2121#include <SDL3/SDL_test.h>
22+ #include "SDL_test_internal.h"
2223
2324#include <stdlib.h> /* Needed for exit() */
2425
25- /* Enable to have color in logs */
26- #if 1
27- #define COLOR_RED "\033[0;31m"
28- #define COLOR_GREEN "\033[0;32m"
29- #define COLOR_YELLOW "\033[0;93m"
30- #define COLOR_BLUE "\033[0;94m"
31- #define COLOR_END "\033[0m"
32- #else
33- #define COLOR_RED ""
34- #define COLOR_GREEN ""
35- #define COLOR_BLUE ""
36- #define COLOR_YELLOW ""
37- #define COLOR_END ""
38- #endif
39-
4026/* Invalid test name/description message format */
4127#define SDLTEST_INVALID_NAME_FORMAT "(Invalid)"
4228
43- /* Log summary message format */
44- #define SDLTEST_LOG_SUMMARY_FORMAT "%s Summary: Total=%d " COLOR_GREEN "Passed=%d" COLOR_END " " COLOR_RED "Failed=%d" COLOR_END " " COLOR_BLUE "Skipped=%d" COLOR_END
45- #define SDLTEST_LOG_SUMMARY_FORMAT_OK "%s Summary: Total=%d " COLOR_GREEN "Passed=%d" COLOR_END " " COLOR_GREEN "Failed=%d" COLOR_END " " COLOR_BLUE "Skipped=%d" COLOR_END
29+ static void SDLTest_LogSummary (bool success , const char * name , int total , int passed , int failed , int skipped )
30+ {
31+ SDLTest_LogMessage (success ? SDL_LOG_PRIORITY_INFO : SDL_LOG_PRIORITY_ERROR ,
32+ "%s Summary: Total=%d " "%s" "Passed=%d" "%s" " " "%s" "Failed=%d" "%s" " " "%s" "Skipped=%d" "%s" ,
33+ name , total , COLOR_GREEN , passed , COLOR_END , success ? COLOR_GREEN : COLOR_RED , failed , COLOR_END , COLOR_BLUE , skipped , COLOR_END );
34+ }
4635
47- /* Final result message format */
48- #define SDLTEST_FINAL_RESULT_FORMAT COLOR_YELLOW ">>> %s '%s':" COLOR_END " %s\n"
36+ static void SDLTest_LogFinalResult (bool success , const char * stage , const char * name , const char * color_message , const char * message )
37+ {
38+ SDL_LogPriority priority = success ? SDL_LOG_PRIORITY_INFO : SDL_LOG_PRIORITY_ERROR ;
39+ SDLTest_LogMessage (priority , "%s>>> %s '%s':" "%s" " " "%s" "%s" "%s" , COLOR_YELLOW , stage , name , COLOR_END , color_message ? color_message : "" , message , color_message ? COLOR_END : "" );
40+ }
4941
5042struct SDLTest_TestSuiteRunner {
5143 struct
@@ -242,7 +234,7 @@ static int SDLTest_RunTest(SDLTest_TestSuiteReference *testSuite, const SDLTest_
242234 }
243235
244236 if (!testCase -> enabled && forceTestRun == false) {
245- SDLTest_Log ( SDLTEST_FINAL_RESULT_FORMAT , "Test" , testCase -> name , "Skipped (Disabled)" );
237+ SDLTest_LogFinalResult (true , "Test" , testCase -> name , NULL , "Skipped (Disabled)" );
246238 return TEST_RESULT_SKIPPED ;
247239 }
248240
@@ -259,7 +251,7 @@ static int SDLTest_RunTest(SDLTest_TestSuiteReference *testSuite, const SDLTest_
259251 if (testSuite -> testSetUp ) {
260252 testSuite -> testSetUp (& data );
261253 if (SDLTest_AssertSummaryToTestResult () == TEST_RESULT_FAILED ) {
262- SDLTest_LogError ( SDLTEST_FINAL_RESULT_FORMAT , "Suite Setup" , testSuite -> name , COLOR_RED "Failed" COLOR_END );
254+ SDLTest_LogFinalResult (false , "Suite Setup" , testSuite -> name , COLOR_RED , "Failed" );
263255 return TEST_RESULT_SETUP_FAILURE ;
264256 }
265257 }
@@ -301,13 +293,13 @@ static int SDLTest_RunTest(SDLTest_TestSuiteReference *testSuite, const SDLTest_
301293 /* Final log based on test execution result */
302294 if (testCaseResult == TEST_SKIPPED ) {
303295 /* Test was programmatically skipped */
304- SDLTest_Log ( SDLTEST_FINAL_RESULT_FORMAT , "Test" , testCase -> name , COLOR_BLUE "Skipped (Programmatically)" COLOR_END );
296+ SDLTest_LogFinalResult (true , "Test" , testCase -> name , COLOR_BLUE , "Skipped (Programmatically)" );
305297 } else if (testCaseResult == TEST_STARTED ) {
306298 /* Test did not return a TEST_COMPLETED value; assume it failed */
307- SDLTest_LogError ( SDLTEST_FINAL_RESULT_FORMAT , "Test" , testCase -> name , COLOR_RED "Failed (test started, but did not return TEST_COMPLETED)" COLOR_END );
299+ SDLTest_LogFinalResult (false , "Test" , testCase -> name , COLOR_RED , "Skipped (test started, but did not return TEST_COMPLETED)" );
308300 } else if (testCaseResult == TEST_ABORTED ) {
309301 /* Test was aborted early; assume it failed */
310- SDLTest_LogError ( SDLTEST_FINAL_RESULT_FORMAT , "Test" , testCase -> name , COLOR_RED "Failed (Aborted)" COLOR_END );
302+ SDLTest_LogFinalResult (false , "Test" , testCase -> name , COLOR_RED , "Failed (Aborted)" );
311303 } else {
312304 SDLTest_LogAssertSummary ();
313305 }
@@ -572,9 +564,11 @@ int SDLTest_ExecuteTestSuiteRunner(SDLTest_TestSuiteRunner *runner)
572564 if (suiteFilter == 1 && suiteFilterName && testSuite -> name &&
573565 SDL_strcasecmp (suiteFilterName , testSuite -> name ) != 0 ) {
574566 /* Skip suite */
575- SDLTest_Log ("===== Test Suite %i: '%s' " COLOR_BLUE "skipped" COLOR_END "\n" ,
567+ SDLTest_Log ("===== Test Suite %i: '%s' " "%s" "skipped" "%s" "\n" ,
576568 suiteCounter ,
577- currentSuiteName );
569+ currentSuiteName ,
570+ COLOR_BLUE ,
571+ COLOR_END );
578572 } else {
579573
580574 int nbTestCases = 0 ;
@@ -634,10 +628,12 @@ int SDLTest_ExecuteTestSuiteRunner(SDLTest_TestSuiteRunner *runner)
634628 if (testFilter == 1 && testFilterName && testCase -> name &&
635629 SDL_strcasecmp (testFilterName , testCase -> name ) != 0 ) {
636630 /* Skip test */
637- SDLTest_Log ("===== Test Case %i.%i: '%s' " COLOR_BLUE "skipped" COLOR_END "\n" ,
631+ SDLTest_Log ("===== Test Case %i.%i: '%s' " "%s" "skipped" "%s" "\n" ,
638632 suiteCounter ,
639633 testCounter ,
640- currentTestName );
634+ currentTestName ,
635+ COLOR_BLUE ,
636+ COLOR_END );
641637 } else {
642638 /* Override 'disabled' flag if we specified a test filter (i.e. force run for debugging) */
643639 if (testFilter == 1 && !testCase -> enabled ) {
@@ -649,10 +645,12 @@ int SDLTest_ExecuteTestSuiteRunner(SDLTest_TestSuiteRunner *runner)
649645 testStartSeconds = GetClock ();
650646
651647 /* Log test started */
652- SDLTest_Log (COLOR_YELLOW "----- Test Case %i.%i: '%s' started" COLOR_END ,
648+ SDLTest_Log ("%s" "----- Test Case %i.%i: '%s' started" "%s" ,
649+ COLOR_YELLOW ,
653650 suiteCounter ,
654651 testCounter ,
655- currentTestName );
652+ currentTestName ,
653+ COLOR_END );
656654 if (testCase -> description && testCase -> description [0 ] != '\0' ) {
657655 SDLTest_Log ("Test Description: '%s'" ,
658656 (testCase -> description ) ? testCase -> description : SDLTEST_INVALID_NAME_FORMAT );
@@ -703,13 +701,13 @@ int SDLTest_ExecuteTestSuiteRunner(SDLTest_TestSuiteRunner *runner)
703701 /* Log final test result */
704702 switch (testResult ) {
705703 case TEST_RESULT_PASSED :
706- SDLTest_Log ( SDLTEST_FINAL_RESULT_FORMAT , "Test" , currentTestName , COLOR_GREEN "Passed" COLOR_END );
704+ SDLTest_LogFinalResult (true , "Test" , currentTestName , COLOR_GREEN , "Passed" );
707705 break ;
708706 case TEST_RESULT_FAILED :
709- SDLTest_LogError ( SDLTEST_FINAL_RESULT_FORMAT , "Test" , currentTestName , COLOR_RED "Failed" COLOR_END );
707+ SDLTest_LogFinalResult (false , "Test" , currentTestName , COLOR_RED , "Failed" );
710708 break ;
711709 case TEST_RESULT_NO_ASSERT :
712- SDLTest_LogError ( SDLTEST_FINAL_RESULT_FORMAT , "Test" , currentTestName , COLOR_BLUE "No Asserts" COLOR_END );
710+ SDLTest_LogFinalResult (false , "Test" , currentTestName , COLOR_BLUE , "No Asserts" );
713711 break ;
714712 }
715713
@@ -734,11 +732,11 @@ int SDLTest_ExecuteTestSuiteRunner(SDLTest_TestSuiteRunner *runner)
734732 /* Log summary and final Suite result */
735733 countSum = testPassedCount + testFailedCount + testSkippedCount ;
736734 if (testFailedCount == 0 ) {
737- SDLTest_Log ( SDLTEST_LOG_SUMMARY_FORMAT_OK , "Suite" , countSum , testPassedCount , testFailedCount , testSkippedCount );
738- SDLTest_Log ( SDLTEST_FINAL_RESULT_FORMAT , "Suite" , currentSuiteName , COLOR_GREEN "Passed" COLOR_END );
735+ SDLTest_LogSummary (true , "Suite" , countSum , testPassedCount , testFailedCount , testSkippedCount );
736+ SDLTest_LogFinalResult (true , "Suite" , currentSuiteName , COLOR_GREEN , "Passed" );
739737 } else {
740- SDLTest_LogError ( SDLTEST_LOG_SUMMARY_FORMAT , "Suite" , countSum , testPassedCount , testFailedCount , testSkippedCount );
741- SDLTest_LogError ( SDLTEST_FINAL_RESULT_FORMAT , "Suite" , currentSuiteName , COLOR_RED "Failed" COLOR_END );
738+ SDLTest_LogSummary (false , "Suite" , countSum , testPassedCount , testFailedCount , testSkippedCount );
739+ SDLTest_LogFinalResult (false , "Suite" , currentSuiteName , COLOR_RED , "Failed" );
742740 }
743741
744742 SDL_free (arrayTestCases );
@@ -761,19 +759,19 @@ int SDLTest_ExecuteTestSuiteRunner(SDLTest_TestSuiteRunner *runner)
761759 countSum = totalTestPassedCount + totalTestFailedCount + totalTestSkippedCount ;
762760 if (totalTestFailedCount == 0 ) {
763761 runResult = 0 ;
764- SDLTest_Log ( SDLTEST_LOG_SUMMARY_FORMAT_OK , "Run" , countSum , totalTestPassedCount , totalTestFailedCount , totalTestSkippedCount );
765- SDLTest_Log ( SDLTEST_FINAL_RESULT_FORMAT , "Run /w seed" , runSeed , COLOR_GREEN "Passed" COLOR_END );
762+ SDLTest_LogSummary (true , "Run" , countSum , totalTestPassedCount , totalTestFailedCount , totalTestSkippedCount );
763+ SDLTest_LogFinalResult (true , "Run /w seed" , runSeed , COLOR_GREEN , "Passed" );
766764 } else {
767765 runResult = 1 ;
768- SDLTest_LogError ( SDLTEST_LOG_SUMMARY_FORMAT , "Run" , countSum , totalTestPassedCount , totalTestFailedCount , totalTestSkippedCount );
769- SDLTest_LogError ( SDLTEST_FINAL_RESULT_FORMAT , "Run /w seed" , runSeed , COLOR_RED "Failed" COLOR_END );
766+ SDLTest_LogSummary (false , "Run" , countSum , totalTestPassedCount , totalTestFailedCount , totalTestSkippedCount );
767+ SDLTest_LogFinalResult (false , "Run /w seed" , runSeed , COLOR_RED , "Failed" );
770768 }
771769
772770 /* Print repro steps for failed tests */
773771 if (failedNumberOfTests > 0 ) {
774772 SDLTest_Log ("Harness input to repro failures:" );
775773 for (testCounter = 0 ; testCounter < failedNumberOfTests ; testCounter ++ ) {
776- SDLTest_Log (COLOR_RED " --seed %s --filter %s" COLOR_END , runSeed , failedTests [testCounter ]-> name );
774+ SDLTest_Log ("%s" " --seed %s --filter %s" "%s" , COLOR_RED , runSeed , failedTests [testCounter ]-> name , COLOR_END );
777775 }
778776 }
779777 SDL_free ((void * )failedTests );
0 commit comments