Skip to content

Commit 368f538

Browse files
committed
SDL_test: allow disabling colorized output
1 parent 7b74fbb commit 368f538

File tree

9 files changed

+178
-92
lines changed

9 files changed

+178
-92
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4036,7 +4036,7 @@ sdl_compile_definitions(
40364036
##### Tests #####
40374037

40384038
if(SDL_TEST_LIBRARY)
4039-
file(GLOB TEST_SOURCES "${SDL3_SOURCE_DIR}/src/test/*.c")
4039+
file(GLOB TEST_SOURCES "${SDL3_SOURCE_DIR}/src/test/*.c" "${SDL3_SOURCE_DIR}/src/test/*.h")
40404040
target_sources(SDL3_test PRIVATE ${TEST_SOURCES})
40414041
if(APPLE)
40424042
set_target_properties(SDL3_test PROPERTIES

VisualC-GDK/SDL_test/SDL_test.vcxproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@
195195
<TreatWarningAsError>$(TreatWarningsAsError)</TreatWarningAsError>
196196
</ClCompile>
197197
</ItemDefinitionGroup>
198+
<ItemGroup>
199+
<ClCompile Include="..\..\src\test\SDL_test_internal.h" />
200+
</ItemGroup>
198201
<ItemGroup>
199202
<ClCompile Include="..\..\src\test\SDL_test_assert.c" />
200203
<ClCompile Include="..\..\src\test\SDL_test_common.c" />

VisualC/SDL_test/SDL_test.vcxproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@
161161
<TreatWarningAsError>$(TreatWarningsAsError)</TreatWarningAsError>
162162
</ClCompile>
163163
</ItemDefinitionGroup>
164+
<ItemGroup>
165+
<ClCompile Include="..\..\src\test\SDL_test_internal.h" />
166+
</ItemGroup>
164167
<ItemGroup>
165168
<ClCompile Include="..\..\src\test\SDL_test_assert.c" />
166169
<ClCompile Include="..\..\src\test\SDL_test_common.c" />

include/SDL3/SDL_test_log.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@
4242
extern "C" {
4343
#endif
4444

45+
/**
46+
* Prints given message with a timestamp in the TEST category and given priority.
47+
*
48+
* \param priority Priority of the message
49+
* \param fmt Message to be logged
50+
*/
51+
void SDLCALL SDLTest_LogMessage(SDL_LogPriority priority, SDL_PRINTF_FORMAT_STRING const char *fmt, ...);
52+
4553
/**
4654
* Prints given message with a timestamp in the TEST category and INFO priority.
4755
*

src/test/SDL_test_assert.c

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,35 +25,32 @@
2525
2626
*/
2727
#include <SDL3/SDL_test.h>
28-
29-
/* Enable to have color in logs */
30-
#if 1
31-
#define COLOR_RED "\033[0;31m"
32-
#define COLOR_GREEN "\033[0;32m"
33-
#define COLOR_YELLOW "\033[0;93m"
34-
#define COLOR_BLUE "\033[0;94m"
35-
#define COLOR_END "\033[0m"
36-
#else
37-
#define COLOR_RED ""
38-
#define COLOR_GREEN ""
39-
#define COLOR_BLUE ""
40-
#define COLOR_YELLOW ""
41-
#define COLOR_END ""
42-
#endif
43-
44-
/* Assert check message format */
45-
#define SDLTEST_ASSERT_CHECK_FORMAT "Assert '%s': %s"
46-
47-
/* Assert summary message format */
48-
#define SDLTEST_ASSERT_SUMMARY_FORMAT "Assert Summary: Total=%d " COLOR_GREEN "Passed=%d" COLOR_END " " COLOR_RED "Failed=%d" COLOR_END
49-
#define SDLTEST_ASSERT_SUMMARY_FORMAT_OK "Assert Summary: Total=%d " COLOR_GREEN "Passed=%d" COLOR_END " " COLOR_GREEN "Failed=%d" COLOR_END
28+
#include "SDL_test_internal.h"
5029

5130
/* ! counts the failed asserts */
5231
static int SDLTest_AssertsFailed = 0;
5332

5433
/* ! counts the passed asserts */
5534
static int SDLTest_AssertsPassed = 0;
5635

36+
static void SDLTest_LogAssertMessage(bool success, const char *assertion)
37+
{
38+
SDL_LogPriority priority;
39+
const char *color;
40+
const char *message;
41+
42+
if (success) {
43+
priority = SDL_LOG_PRIORITY_INFO;
44+
color = COLOR_GREEN;
45+
message = "Passed";
46+
} else {
47+
priority = SDL_LOG_PRIORITY_ERROR;
48+
color = COLOR_RED;
49+
message = "Failed";
50+
}
51+
SDLTest_LogMessage(priority, "Assert '%s': %s%s%s", assertion, color, message, COLOR_END);
52+
}
53+
5754
/*
5855
* Assert that logs and break execution flow on failures (i.e. for harness errors).
5956
*/
@@ -89,10 +86,10 @@ int SDLTest_AssertCheck(int assertCondition, SDL_PRINTF_FORMAT_STRING const char
8986
/* Log pass or fail message */
9087
if (assertCondition == ASSERT_FAIL) {
9188
SDLTest_AssertsFailed++;
92-
SDLTest_LogError(SDLTEST_ASSERT_CHECK_FORMAT, logMessage, COLOR_RED "Failed" COLOR_END);
89+
SDLTest_LogAssertMessage(false, logMessage);
9390
} else {
9491
SDLTest_AssertsPassed++;
95-
SDLTest_Log(SDLTEST_ASSERT_CHECK_FORMAT, logMessage, COLOR_GREEN "Passed" COLOR_END);
92+
SDLTest_LogAssertMessage(true, logMessage);
9693
}
9794

9895
return assertCondition;
@@ -114,7 +111,7 @@ void SDLTest_AssertPass(SDL_PRINTF_FORMAT_STRING const char *assertDescription,
114111

115112
/* Log pass message */
116113
SDLTest_AssertsPassed++;
117-
SDLTest_Log(SDLTEST_ASSERT_CHECK_FORMAT, logMessage, COLOR_GREEN "Passed" COLOR_END);
114+
SDLTest_LogAssertMessage(true, logMessage);
118115
}
119116

120117
/*
@@ -133,11 +130,12 @@ void SDLTest_ResetAssertSummary(void)
133130
void SDLTest_LogAssertSummary(void)
134131
{
135132
int totalAsserts = SDLTest_AssertsPassed + SDLTest_AssertsFailed;
136-
if (SDLTest_AssertsFailed == 0) {
137-
SDLTest_Log(SDLTEST_ASSERT_SUMMARY_FORMAT_OK, totalAsserts, SDLTest_AssertsPassed, SDLTest_AssertsFailed);
138-
} else {
139-
SDLTest_LogError(SDLTEST_ASSERT_SUMMARY_FORMAT, totalAsserts, SDLTest_AssertsPassed, SDLTest_AssertsFailed);
140-
}
133+
bool success = SDLTest_AssertsFailed == 0;
134+
135+
SDLTest_LogMessage(success ? SDL_LOG_PRIORITY_INFO : SDL_LOG_PRIORITY_ERROR,
136+
"Assert Summary: Total=%d " "%s" "Passed=%d" "%s" " " "%s" "Failed=%d" "%s",
137+
totalAsserts, COLOR_GREEN, SDLTest_AssertsPassed, COLOR_END,
138+
success ? COLOR_GREEN : COLOR_RED, SDLTest_AssertsFailed, COLOR_END);
141139
}
142140

143141
/*

src/test/SDL_test_common.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,28 @@
2121

2222
/* Ported from original test/common.c file. */
2323
#include <SDL3/SDL_test.h>
24+
#include "SDL_test_internal.h"
2425

2526
#define SDL_MAIN_NOIMPL
2627
#define SDL_MAIN_USE_CALLBACKS
2728
#include <SDL3/SDL_main.h>
2829

30+
bool SDLTest_Color = true;
31+
32+
static bool get_environment_bool_variable(const char *name)
33+
{
34+
const char *var_string = SDL_GetEnvironmentVariable(SDL_GetEnvironment(), name);
35+
if (!var_string || var_string[0] == '\0') {
36+
return false;
37+
}
38+
return true;
39+
}
40+
2941
static const char *common_usage[] = {
3042
"[-h | --help]",
3143
"[--trackmem]",
3244
"[--randmem]",
45+
"[--no-color]",
3346
"[--info all|video|modes|render|event|event_motion]",
3447
"[--log all|error|system|audio|video|render|input]",
3548
NULL
@@ -142,6 +155,10 @@ static int SDLCALL SDLTest_CommonStateParseCommonArguments(void *data, char **ar
142155
/* Already handled in SDLTest_CommonCreateState() */
143156
return 1;
144157
}
158+
if (SDL_strcasecmp(argv[index], "--no-color") == 0) {
159+
SDLTest_Color = false;
160+
return 1;
161+
}
145162
if (SDL_strcasecmp(argv[index], "--randmem") == 0) {
146163
/* Already handled in SDLTest_CommonCreateState() */
147164
return 1;
@@ -686,6 +703,8 @@ SDLTest_CommonState *SDLTest_CommonCreateState(char **argv, SDL_InitFlags flags)
686703
int i;
687704
SDLTest_CommonState *state;
688705

706+
SDLTest_Color = !get_environment_bool_variable("NO_COLOR");
707+
689708
/* Do this first so we catch all allocations */
690709
for (i = 1; argv[i]; ++i) {
691710
if (SDL_strcasecmp(argv[i], "--trackmem") == 0) {

src/test/SDL_test_harness.c

Lines changed: 41 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -19,33 +19,25 @@
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

5042
struct 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

Comments
 (0)