From a4b0bc7e7bc8d235fc1384b4eaed5d10a8d86ea4 Mon Sep 17 00:00:00 2001 From: Tom Hicks Date: Sun, 16 Apr 2023 04:08:51 -0700 Subject: [PATCH] Implements colors and tests. --- sbf-cpp/Colors.cpp | 41 ++++- sbf-cpp/Colors.h | 23 ++- sbf-cpp/Colors_test.cpp | 390 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 440 insertions(+), 14 deletions(-) diff --git a/sbf-cpp/Colors.cpp b/sbf-cpp/Colors.cpp index 1eeec40..5f900b8 100644 --- a/sbf-cpp/Colors.cpp +++ b/sbf-cpp/Colors.cpp @@ -3,24 +3,47 @@ namespace SBF { // TODO: Update these if they're wrong. They should be the initial color pair. - static uint8_t g_foregroundColor = kColorDarkWhite; #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wunused-variable" - static uint8_t g_backgroundColor = kColorDarkBlack; + uint8_t g_foregroundColor = kColorDarkWhite; + uint8_t g_backgroundColor = kColorDarkBlack; #pragma clang diagnostic pop - static uint16_t g_color; - void SetForegroundColor(uint8_t foregroundColor) { - g_foregroundColor = foregroundColor; - // TODO: update g_color with g_foregroundColor and g_backgroundColor. + // void SetForegroundColor(uint8_t foregroundColor) { + // g_foregroundColor = foregroundColor; + // // TODO: update g_color with g_foregroundColor and g_backgroundColor. + // } + // uint8_t GetForegroundColor() { + // return g_foregroundColor; + // } + + // // ncurses color pair + // uint16_t GetColor() { + // return g_color; + // } + + void FillColors(std::vector& colors) { + colors.clear(); + for (uint8_t color = kColorDarkBlack; color <= kColorBrightWhite; color++) { + colors.push_back(color); + } } + + uint8_t GetBackgroundColor() { + return g_backgroundColor; + } + uint8_t GetForegroundColor() { return g_foregroundColor; } - // ncurses color pair - uint16_t GetColor() { - return g_color; + // TODO: Define what happens when color is invalid. + void SetBackgroundColor(uint8_t color) { + g_backgroundColor = color; } + // TODO: Define what happens when color is invalid. + void SetForegroundColor(uint8_t color) { + g_foregroundColor = color; + } } // End namespace SBF diff --git a/sbf-cpp/Colors.h b/sbf-cpp/Colors.h index 063610a..4efbbee 100644 --- a/sbf-cpp/Colors.h +++ b/sbf-cpp/Colors.h @@ -34,12 +34,25 @@ namespace SBF { const uint8_t kColorBrightYellow = 14; const uint8_t kColorBrightWhite = 15; - void SetForegroundColor(uint8_t foregroundColor); - uint8_t GetForegroundColor(); - // TODO: Make this an ncurses color pair - uint16_t GetColor(); + /// @brief Sets the stored foreground color. + /// @param color The new foreground color. + void SetForegroundColor(uint8_t color); - void FillColors(std::vector colors); + /// @brief Gets the stored foreground color. + /// @return The foreground color. + uint8_t GetForegroundColor(); + + /// @brief Sets the stored background color. + /// @param color The new background color. + void SetBackgroundColor(uint8_t color); + + /// @brief Gets the stored background color. + /// @return The background color. + uint8_t GetBackgroundColor(); + + /// @brief Fills the provided vector with all of the possible color values. It will be cleared before filling. + /// @param colors The vector to fill. + void FillColors(std::vector& colors); } // End namespace SBF /** @}*/ #endif // End !defined COLORS_H__ diff --git a/sbf-cpp/Colors_test.cpp b/sbf-cpp/Colors_test.cpp index ac214e1..a1b6dd4 100644 --- a/sbf-cpp/Colors_test.cpp +++ b/sbf-cpp/Colors_test.cpp @@ -1,10 +1,400 @@ #include "Colors.h" #include "test.h" +#include +#include +#include + using namespace SBF; using namespace Test; +using namespace std; +namespace Test::Colors { +TestResults test_FillColors(); +TestResults test_GetBackgroundColor(); +TestResults test_GetForegroundColor(); +TestResults test_SetBackgroundColor(); +TestResults test_SetForegroundColor(); +} // End namespace Test::Colors +using namespace Test::Colors; +namespace SBF { + extern uint8_t g_foregroundColor; + extern uint8_t g_backgroundColor; +} TestResults main_test_Colors(int argc, char** argv) { TestResults results; + results += test_FillColors(); + results += test_GetBackgroundColor(); + results += test_GetForegroundColor(); + results += test_SetBackgroundColor(); + results += test_SetForegroundColor(); + return results; } +namespace Test::Colors { +TestResults test_FillColors() { + return execute_suite(make_test_suite( + "SBF::FillColors", + []()->string { + ostringstream error_message; + vector expected = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; + vector actual = {255}; + FillColors(actual); + compare(error_message, expected, actual); + string error = error_message.str(); + if (error.size() > 0) { + return error; + } + return "no errors"; + }, + vector>({ + make_test( + "should fill colors", + "no errors", + make_tuple()), + }) + )); +} + +TestResults test_GetBackgroundColor() { + auto fnToTest = [](uint8_t background_color)->uint8_t { + SBF::g_backgroundColor = background_color; + return GetBackgroundColor(); + }; + return execute_suite(make_test_suite( + "SBF::GetBackgroundColor", + fnToTest, + vector>({ + make_test( + "should get kColorDarkBlack when background color is 0", + kColorDarkBlack, + make_tuple(0U)), + make_test( + "should get kColorDarkBlue when background color is 1", + kColorDarkBlue, + make_tuple(1U)), + make_test( + "should get kColorDarkGreen when background color is 2", + kColorDarkGreen, + make_tuple(2U)), + make_test( + "should get kColorDarkCyan when background color is 3", + kColorDarkCyan, + make_tuple(3U)), + make_test( + "should get kColorDarkRed when background color is 4", + kColorDarkRed, + make_tuple(4U)), + make_test( + "should get kColorDarkMagenta when background color is 5", + kColorDarkMagenta, + make_tuple(5U)), + make_test( + "should get kColorDarkOrange when background color is 6", + kColorDarkOrange, + make_tuple(6U)), + make_test( + "should get kColorDarkYellow when background color is 6", + kColorDarkYellow, + make_tuple(6U)), + make_test( + "should get kColorDarkWhite when background color is 7", + kColorDarkWhite, + make_tuple(7U)), + make_test( + "should get kColorBrightBlack when background color is 8", + kColorBrightBlack, + make_tuple(8U)), + make_test( + "should get kColorBrightBlue when background color is 9", + kColorBrightBlue, + make_tuple(9U)), + make_test( + "should get kColorBrightGreen when background color is 10", + kColorBrightGreen, + make_tuple(10U)), + make_test( + "should get kColorBrightCyan when background color is 11", + kColorBrightCyan, + make_tuple(11U)), + make_test( + "should get kColorBrightRed when background color is 12", + kColorBrightRed, + make_tuple(12U)), + make_test( + "should get kColorBrightMagenta when background color is 13", + kColorBrightMagenta, + make_tuple(13U)), + make_test( + "should get kColorBrightOrange when background color is 14", + kColorBrightOrange, + make_tuple(14U)), + make_test( + "should get kColorBrightYellow when background color is 14", + kColorBrightYellow, + make_tuple(14U)), + make_test( + "should get kColorBrightWhite when background color is 15", + kColorBrightWhite, + make_tuple(15U)), + }) + )); +} + +TestResults test_GetForegroundColor() { + auto fnToTest = [](uint8_t foreground_color)->uint8_t { + SBF::g_foregroundColor = foreground_color; + return GetForegroundColor(); + }; + return execute_suite(make_test_suite( + "SBF::GetForegroundColor", + fnToTest, + vector>({ + make_test( + "should get kColorDarkBlack when foreground color is 0", + kColorDarkBlack, + make_tuple(0U)), + make_test( + "should get kColorDarkBlue when foreground color is 1", + kColorDarkBlue, + make_tuple(1U)), + make_test( + "should get kColorDarkGreen when foreground color is 2", + kColorDarkGreen, + make_tuple(2U)), + make_test( + "should get kColorDarkCyan when foreground color is 3", + kColorDarkCyan, + make_tuple(3U)), + make_test( + "should get kColorDarkRed when foreground color is 4", + kColorDarkRed, + make_tuple(4U)), + make_test( + "should get kColorDarkMagenta when foreground color is 5", + kColorDarkMagenta, + make_tuple(5U)), + make_test( + "should get kColorDarkOrange when foreground color is 6", + kColorDarkOrange, + make_tuple(6U)), + make_test( + "should get kColorDarkYellow when foreground color is 6", + kColorDarkYellow, + make_tuple(6U)), + make_test( + "should get kColorDarkWhite when foreground color is 7", + kColorDarkWhite, + make_tuple(7U)), + make_test( + "should get kColorBrightBlack when foreground color is 8", + kColorBrightBlack, + make_tuple(8U)), + make_test( + "should get kColorBrightBlue when foreground color is 9", + kColorBrightBlue, + make_tuple(9U)), + make_test( + "should get kColorBrightGreen when foreground color is 10", + kColorBrightGreen, + make_tuple(10U)), + make_test( + "should get kColorBrightCyan when foreground color is 11", + kColorBrightCyan, + make_tuple(11U)), + make_test( + "should get kColorBrightRed when foreground color is 12", + kColorBrightRed, + make_tuple(12U)), + make_test( + "should get kColorBrightMagenta when foreground color is 13", + kColorBrightMagenta, + make_tuple(13U)), + make_test( + "should get kColorBrightOrange when foreground color is 14", + kColorBrightOrange, + make_tuple(14U)), + make_test( + "should get kColorBrightYellow when foreground color is 14", + kColorBrightYellow, + make_tuple(14U)), + make_test( + "should get kColorBrightWhite when foreground color is 15", + kColorBrightWhite, + make_tuple(15U)), + }) + )); +} + +TestResults test_SetBackgroundColor() { + auto fnToTest = [](uint8_t color)->uint8_t { + SBF::g_backgroundColor = 255; + SetBackgroundColor(color); + return SBF::g_backgroundColor; + }; + return execute_suite(make_test_suite( + "SBF::SetBackgroundColor", + fnToTest, + vector>({ + make_test( + "should set background color to kColorDarkBlack when color is 0", + kColorDarkBlack, + make_tuple(0U)), + make_test( + "should set background color to kColorDarkBlue when color is 1", + kColorDarkBlue, + make_tuple(1U)), + make_test( + "should set background color to kColorDarkGreen when color is 2", + kColorDarkGreen, + make_tuple(2U)), + make_test( + "should set background color to kColorDarkCyan when color is 3", + kColorDarkCyan, + make_tuple(3U)), + make_test( + "should set background color to kColorDarkRed when color is 4", + kColorDarkRed, + make_tuple(4U)), + make_test( + "should set background color to kColorDarkMagenta when color is 5", + kColorDarkMagenta, + make_tuple(5U)), + make_test( + "should set background color to kColorDarkOrange when color is 6", + kColorDarkOrange, + make_tuple(6U)), + make_test( + "should set background color to kColorDarkYellow when color is 6", + kColorDarkYellow, + make_tuple(6U)), + make_test( + "should set background color to kColorDarkWhite when color is 7", + kColorDarkWhite, + make_tuple(7U)), + make_test( + "should set background color to kColorBrightBlack when color is 8", + kColorBrightBlack, + make_tuple(8U)), + make_test( + "should set background color to kColorBrightBlue when color is 9", + kColorBrightBlue, + make_tuple(9U)), + make_test( + "should set background color to kColorBrightGreen when color is 10", + kColorBrightGreen, + make_tuple(10U)), + make_test( + "should set background color to kColorBrightCyan when color is 11", + kColorBrightCyan, + make_tuple(11U)), + make_test( + "should set background color to kColorBrightRed when color is 12", + kColorBrightRed, + make_tuple(12U)), + make_test( + "should set background color to kColorBrightMagenta when color is 13", + kColorBrightMagenta, + make_tuple(13U)), + make_test( + "should set background color to kColorBrightOrange when color is 14", + kColorBrightOrange, + make_tuple(14U)), + make_test( + "should set background color to kColorBrightYellow when color is 14", + kColorBrightYellow, + make_tuple(14U)), + make_test( + "should set background color to kColorBrightWhite when color is 15", + kColorBrightWhite, + make_tuple(15U)), + }) + )); +} + +TestResults test_SetForegroundColor() { + auto fnToTest = [](uint8_t color)->uint8_t { + SBF::g_foregroundColor = 255; + SetForegroundColor(color); + return SBF::g_foregroundColor; + }; + return execute_suite(make_test_suite( + "SBF::SetForegroundColor", + fnToTest, + vector>({ + make_test( + "should set foreground color to kColorDarkBlack when color is 0", + kColorDarkBlack, + make_tuple(0U)), + make_test( + "should set foreground color to kColorDarkBlue when color is 1", + kColorDarkBlue, + make_tuple(1U)), + make_test( + "should set foreground color to kColorDarkGreen when color is 2", + kColorDarkGreen, + make_tuple(2U)), + make_test( + "should set foreground color to kColorDarkCyan when color is 3", + kColorDarkCyan, + make_tuple(3U)), + make_test( + "should set foreground color to kColorDarkRed when color is 4", + kColorDarkRed, + make_tuple(4U)), + make_test( + "should set foreground color to kColorDarkMagenta when color is 5", + kColorDarkMagenta, + make_tuple(5U)), + make_test( + "should set foreground color to kColorDarkOrange when color is 6", + kColorDarkOrange, + make_tuple(6U)), + make_test( + "should set foreground color to kColorDarkYellow when color is 6", + kColorDarkYellow, + make_tuple(6U)), + make_test( + "should set foreground color to kColorDarkWhite when color is 7", + kColorDarkWhite, + make_tuple(7U)), + make_test( + "should set foreground color to kColorBrightBlack when color is 8", + kColorBrightBlack, + make_tuple(8U)), + make_test( + "should set foreground color to kColorBrightBlue when color is 9", + kColorBrightBlue, + make_tuple(9U)), + make_test( + "should set foreground color to kColorBrightGreen when color is 10", + kColorBrightGreen, + make_tuple(10U)), + make_test( + "should set foreground color to kColorBrightCyan when color is 11", + kColorBrightCyan, + make_tuple(11U)), + make_test( + "should set foreground color to kColorBrightRed when color is 12", + kColorBrightRed, + make_tuple(12U)), + make_test( + "should set foreground color to kColorBrightMagenta when color is 13", + kColorBrightMagenta, + make_tuple(13U)), + make_test( + "should set foreground color to kColorBrightOrange when color is 14", + kColorBrightOrange, + make_tuple(14U)), + make_test( + "should set foreground color to kColorBrightYellow when color is 14", + kColorBrightYellow, + make_tuple(14U)), + make_test( + "should set foreground color to kColorBrightWhite when color is 15", + kColorBrightWhite, + make_tuple(15U)), + }) + )); +} +} // End namespace Test::Colors