From 1be2dd996c04f59e8a8d5a3d80e0d016e7e6e2a6 Mon Sep 17 00:00:00 2001 From: Marijn van der Werf Date: Wed, 19 Oct 2016 01:55:18 +0200 Subject: [PATCH] Make testpaint a lot more quiet --- scripts/linux/build.sh | 2 +- test/testpaint/TestPaint.hpp | 5 ++ test/testpaint/TestTrack.cpp | 4 +- test/testpaint/TestTrack.hpp | 2 +- test/testpaint/main.cpp | 116 +++++++++++++++++++++-------------- 5 files changed, 79 insertions(+), 50 deletions(-) diff --git a/scripts/linux/build.sh b/scripts/linux/build.sh index 019366c749..c07b28687e 100755 --- a/scripts/linux/build.sh +++ b/scripts/linux/build.sh @@ -75,7 +75,7 @@ pushd build # NOT the same variable as above # this target also includes building & running of testpaint make $OPENRCT2_MAKE_OPTS testpaint - ./testpaint --silent || if [[ $? -eq 1 ]] ; then echo Allowing failed tests to pass ; else false; fi + ./testpaint --quiet || if [[ $? -eq 1 ]] ; then echo Allowing failed tests to pass ; else false; fi else cmake $OPENRCT2_CMAKE_OPTS .. # NOT the same variable as above diff --git a/test/testpaint/TestPaint.hpp b/test/testpaint/TestPaint.hpp index b0ff7d78de..f5e4f3de89 100644 --- a/test/testpaint/TestPaint.hpp +++ b/test/testpaint/TestPaint.hpp @@ -48,6 +48,11 @@ namespace TestPaint void ResetSupportHeights(); } +enum Verbosity { + QUIET, + NORMAL, +}; + extern "C" { int generatePaintCode(uint8 rideType); diff --git a/test/testpaint/TestTrack.cpp b/test/testpaint/TestTrack.cpp index cec12af3c7..c067779e43 100644 --- a/test/testpaint/TestTrack.cpp +++ b/test/testpaint/TestTrack.cpp @@ -215,7 +215,7 @@ static uint8 TestTrackElementSideTunnels(uint8 rideType, uint8 trackType, uint8 static uint8 TestTrackElementVerticalTunnels(uint8 rideType, uint8 trackType, uint8 trackSequence, std::string *error); -uint8 TestTrack::TestPaintTrackElement(uint8 rideType, uint8 trackType) { +uint8 TestTrack::TestPaintTrackElement(uint8 rideType, uint8 trackType, std::string *out) { if (!Utils::rideSupportsTrackType(rideType, trackType)) { return TEST_FAILED; } @@ -246,7 +246,7 @@ uint8 TestTrack::TestPaintTrackElement(uint8 rideType, uint8 trackType) { retVal = function(rideType, trackType, trackSequence, &error); if (retVal != TEST_SUCCESS) { - printf("%s\n", error.c_str()); + *out += error + "\n"; return retVal; } } diff --git a/test/testpaint/TestTrack.hpp b/test/testpaint/TestTrack.hpp index 7eb3ff1d07..811cb2cfe3 100644 --- a/test/testpaint/TestTrack.hpp +++ b/test/testpaint/TestTrack.hpp @@ -22,5 +22,5 @@ class TestTrack { public: - static uint8 TestPaintTrackElement(uint8 rideType, uint8 trackType); + static uint8 TestPaintTrackElement(uint8 rideType, uint8 trackType, std::string *out); }; diff --git a/test/testpaint/main.cpp b/test/testpaint/main.cpp index ad1363762b..d99a3e73bd 100644 --- a/test/testpaint/main.cpp +++ b/test/testpaint/main.cpp @@ -49,6 +49,7 @@ enum CLIColour { }; bool gTestColor = true; +Verbosity _verbosity = NORMAL; static bool CStringEquals(const char *lhs, const char *rhs) { if (lhs == NULL) return rhs == NULL; @@ -126,10 +127,9 @@ static WORD GetWindowsConsoleAttribute(CLIColour color, WORD defaultAttr) #endif -static void ColouredPrintF(CLIColour colour, const char* fmt, ...) +static void Write_VA(Verbosity verbosity, CLIColour colour, const char *fmt, va_list args) { - va_list args; - va_start(args, fmt); + if (_verbosity < verbosity) return; COLOUR_METHOD colourMethod = GetColourMethod(); @@ -148,6 +148,37 @@ static void ColouredPrintF(CLIColour colour, const char* fmt, ...) SetConsoleTextAttribute(hStdOut, defaultAttr); #endif } +} + +static void Write(Verbosity verbosity, CLIColour colour, const char *fmt, ...) +{ + va_list args; + va_start(args, fmt); + Write_VA(verbosity, colour, fmt, args); + va_end(args); +} + +static void Write(Verbosity verbosity, const char * fmt, ...) +{ + va_list args; + va_start(args, fmt); + Write_VA(verbosity, DEFAULT, fmt, args); + va_end(args); +} + +static void Write(CLIColour colour, const char * fmt, ...) +{ + va_list args; + va_start(args, fmt); + Write_VA(NORMAL, colour, fmt, args); + va_end(args); +} + +static void Write(const char * fmt, ...) +{ + va_list args; + va_start(args, fmt); + Write_VA(NORMAL, DEFAULT, fmt, args); va_end(args); } @@ -354,7 +385,7 @@ static void PrintRideTypes() colour = CLIColour::GREEN; } - ColouredPrintF(colour, "%2d: %-30s%s\n", rideType, rideName, status); + Write(colour, "%2d: %-30s%s\n", rideType, rideName, status); } } @@ -398,7 +429,6 @@ int main(int argc, char *argv[]) { std::vector testCases; - bool silent = false; bool generate = false; uint8 specificRideType = 0xFF; for (int i = 0; i < argc; ++i) { @@ -406,8 +436,8 @@ int main(int argc, char *argv[]) { if (strcmp(arg, "--gtest_color=no") == 0) { gTestColor = false; } - else if (strcmp(arg, "--silent") == 0) { - silent = true; + else if (strcmp(arg, "--quiet") == 0) { + _verbosity = Verbosity::QUIET; } else if (strcmp(arg, "--ride-type") == 0) { if (i + 1 < argc) { @@ -466,11 +496,11 @@ int main(int argc, char *argv[]) { testCount += tc.trackTypes.size(); } - ColouredPrintF(CLIColour::GREEN, "[==========] "); - printf("Running %d tests from %d test cases.\n", testCount, testCaseCount); + Write(CLIColour::GREEN, "[==========] "); + Write("Running %d tests from %d test cases.\n", testCount, testCaseCount); - ColouredPrintF(CLIColour::GREEN, "[----------] "); - printf("Global test environment set-up.\n"); + Write(CLIColour::GREEN, "[----------] "); + Write("Global test environment set-up.\n"); openrct2_setup_rct2_segment(); PaintIntercept::InitHooks(); @@ -478,8 +508,8 @@ int main(int argc, char *argv[]) { std::vector failures; for (auto &&tc : testCases) { const utf8string rideTypeName = RideNames[tc.rideType]; - ColouredPrintF(CLIColour::GREEN, "[----------] "); - printf("%d tests from %s\n", (int)tc.trackTypes.size(), rideTypeName); + Write(CLIColour::GREEN, "[----------] "); + Write("%d tests from %s\n", (int)tc.trackTypes.size(), rideTypeName); for (auto &&trackType : tc.trackTypes) { utf8string trackTypeName; @@ -489,71 +519,65 @@ int main(int argc, char *argv[]) { trackTypeName = TrackNames[trackType]; } + Write(CLIColour::GREEN, "[ RUN ] "); + Write("%s.%s\n", rideTypeName, trackTypeName); - if (!silent) { - ColouredPrintF(CLIColour::GREEN, "[ RUN ] "); - printf("%s.%s\n", rideTypeName, trackTypeName); - } - int retVal = TestTrack::TestPaintTrackElement(tc.rideType, trackType); + std::string out; + int retVal = TestTrack::TestPaintTrackElement(tc.rideType, trackType, &out); + Write("%s", out.c_str()); switch (retVal) { case TEST_SUCCESS: - if (!silent) { - ColouredPrintF(CLIColour::GREEN, "[ OK ] "); - printf("%s.%s (0 ms)\n", rideTypeName, trackTypeName); - } + Write(CLIColour::GREEN, "[ OK ] "); + Write("%s.%s (0 ms)\n", rideTypeName, trackTypeName); successCount++; break; case TEST_SKIPPED: - printf("Skipped\n"); + Write("Skipped\n"); // Outputting this as OK because CLion only allows FAILED or OK - ColouredPrintF(CLIColour::YELLOW, "[ OK ] "); - printf("%s.%s (0 ms)\n", rideTypeName, trackTypeName); + Write(CLIColour::YELLOW, "[ OK ] "); + Write("%s.%s (0 ms)\n", rideTypeName, trackTypeName); successCount++; break; case TEST_FAILED: - if (silent) { - ColouredPrintF(CLIColour::GREEN, "[ RUN ] "); - printf("%s.%s\n", rideTypeName, trackTypeName); - } utf8string testCaseName = new utf8[64]; snprintf(testCaseName, 64, "%s.%s", rideTypeName, trackTypeName); - ColouredPrintF(CLIColour::RED, "[ FAILED ] "); - printf("%s (0 ms)\n", testCaseName); + Write(CLIColour::RED, "[ FAILED ] "); + Write("%s (0 ms)\n", testCaseName); failures.push_back(testCaseName); break; } } - ColouredPrintF(CLIColour::GREEN, "[----------] "); - printf("%d tests from %s (0 ms total)\n", (int)tc.trackTypes.size(), rideTypeName); + Write(CLIColour::GREEN, "[----------] "); + Write("%d tests from %s (0 ms total)\n", (int)tc.trackTypes.size(), rideTypeName); } - printf("\n"); + Write("\n"); - ColouredPrintF(CLIColour::GREEN, "[----------] "); - printf("Global test environment tear-down\n"); + Write(CLIColour::GREEN, "[----------] "); + Write("Global test environment tear-down\n"); - ColouredPrintF(CLIColour::GREEN, "[==========] "); - printf("%d tests from %d test cases ran. (0 ms total).\n", testCount, testCaseCount); + Write(CLIColour::GREEN, "[==========] "); + Write("%d tests from %d test cases ran. (0 ms total).\n", testCount, testCaseCount); - ColouredPrintF(CLIColour::GREEN, "[ PASSED ] "); - printf("%d tests.\n", successCount); + Write(Verbosity::QUIET, CLIColour::GREEN, "[ PASSED ] "); + Write(Verbosity::QUIET, "%d tests.\n", successCount); if (failures.size() > 0) { - ColouredPrintF(CLIColour::RED, "[ FAILED ] "); - printf("%d tests, listed below:\n", (int)failures.size()); + Write(Verbosity::QUIET, CLIColour::RED, "[ FAILED ] "); + Write(Verbosity::QUIET, "%d tests, listed below:\n", (int)failures.size()); for (auto &&failure : failures) { - ColouredPrintF(CLIColour::RED, "[ FAILED ] "); - printf("%s\n", failure); + Write(Verbosity::QUIET, CLIColour::RED, "[ FAILED ] "); + Write(Verbosity::QUIET, "%s\n", failure); delete [] failure; } - printf("\n"); + Write(Verbosity::QUIET, "\n"); - printf("%d FAILED TESTS\n", (int)failures.size()); + Write(Verbosity::QUIET, "%d FAILED TESTS\n", (int)failures.size()); return 1; }