Implements colors and tests.

This commit is contained in:
2023-04-16 04:08:51 -07:00
parent 5501de76e2
commit a4b0bc7e7b
3 changed files with 440 additions and 14 deletions

View File

@@ -3,24 +3,47 @@
namespace SBF { namespace SBF {
// TODO: Update these if they're wrong. They should be the initial color pair. // 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 push
#pragma clang diagnostic ignored "-Wunused-variable" #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 #pragma clang diagnostic pop
static uint16_t g_color;
void SetForegroundColor(uint8_t foregroundColor) { // void SetForegroundColor(uint8_t foregroundColor) {
g_foregroundColor = foregroundColor; // g_foregroundColor = foregroundColor;
// TODO: update g_color with g_foregroundColor and g_backgroundColor. // // 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<uint8_t>& colors) {
colors.clear();
for (uint8_t color = kColorDarkBlack; color <= kColorBrightWhite; color++) {
colors.push_back(color);
}
} }
uint8_t GetBackgroundColor() {
return g_backgroundColor;
}
uint8_t GetForegroundColor() { uint8_t GetForegroundColor() {
return g_foregroundColor; return g_foregroundColor;
} }
// ncurses color pair // TODO: Define what happens when color is invalid.
uint16_t GetColor() { void SetBackgroundColor(uint8_t color) {
return g_color; g_backgroundColor = color;
} }
// TODO: Define what happens when color is invalid.
void SetForegroundColor(uint8_t color) {
g_foregroundColor = color;
}
} // End namespace SBF } // End namespace SBF

View File

@@ -34,12 +34,25 @@ namespace SBF {
const uint8_t kColorBrightYellow = 14; const uint8_t kColorBrightYellow = 14;
const uint8_t kColorBrightWhite = 15; const uint8_t kColorBrightWhite = 15;
void SetForegroundColor(uint8_t foregroundColor); /// @brief Sets the stored foreground color.
uint8_t GetForegroundColor(); /// @param color The new foreground color.
// TODO: Make this an ncurses color pair void SetForegroundColor(uint8_t color);
uint16_t GetColor();
void FillColors(std::vector<uint8_t> 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<uint8_t>& colors);
} // End namespace SBF } // End namespace SBF
/** @}*/ /** @}*/
#endif // End !defined COLORS_H__ #endif // End !defined COLORS_H__

View File

@@ -1,10 +1,400 @@
#include "Colors.h" #include "Colors.h"
#include "test.h" #include "test.h"
#include <string>
#include <tuple>
#include <vector>
using namespace SBF; using namespace SBF;
using namespace Test; 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 main_test_Colors(int argc, char** argv) {
TestResults results; TestResults results;
results += test_FillColors();
results += test_GetBackgroundColor();
results += test_GetForegroundColor();
results += test_SetBackgroundColor();
results += test_SetForegroundColor();
return results; return results;
} }
namespace Test::Colors {
TestResults test_FillColors() {
return execute_suite<string>(make_test_suite(
"SBF::FillColors",
[]()->string {
ostringstream error_message;
vector<uint8_t> expected = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
vector<uint8_t> actual = {255};
FillColors(actual);
compare(error_message, expected, actual);
string error = error_message.str();
if (error.size() > 0) {
return error;
}
return "no errors";
},
vector<TestTuple<string>>({
make_test<string>(
"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<uint32_t, uint32_t>(make_test_suite(
"SBF::GetBackgroundColor",
fnToTest,
vector<TestTuple<uint32_t, uint32_t>>({
make_test<uint32_t, uint32_t>(
"should get kColorDarkBlack when background color is 0",
kColorDarkBlack,
make_tuple(0U)),
make_test<uint32_t, uint32_t>(
"should get kColorDarkBlue when background color is 1",
kColorDarkBlue,
make_tuple(1U)),
make_test<uint32_t, uint32_t>(
"should get kColorDarkGreen when background color is 2",
kColorDarkGreen,
make_tuple(2U)),
make_test<uint32_t, uint32_t>(
"should get kColorDarkCyan when background color is 3",
kColorDarkCyan,
make_tuple(3U)),
make_test<uint32_t, uint32_t>(
"should get kColorDarkRed when background color is 4",
kColorDarkRed,
make_tuple(4U)),
make_test<uint32_t, uint32_t>(
"should get kColorDarkMagenta when background color is 5",
kColorDarkMagenta,
make_tuple(5U)),
make_test<uint32_t, uint32_t>(
"should get kColorDarkOrange when background color is 6",
kColorDarkOrange,
make_tuple(6U)),
make_test<uint32_t, uint32_t>(
"should get kColorDarkYellow when background color is 6",
kColorDarkYellow,
make_tuple(6U)),
make_test<uint32_t, uint32_t>(
"should get kColorDarkWhite when background color is 7",
kColorDarkWhite,
make_tuple(7U)),
make_test<uint32_t, uint32_t>(
"should get kColorBrightBlack when background color is 8",
kColorBrightBlack,
make_tuple(8U)),
make_test<uint32_t, uint32_t>(
"should get kColorBrightBlue when background color is 9",
kColorBrightBlue,
make_tuple(9U)),
make_test<uint32_t, uint32_t>(
"should get kColorBrightGreen when background color is 10",
kColorBrightGreen,
make_tuple(10U)),
make_test<uint32_t, uint32_t>(
"should get kColorBrightCyan when background color is 11",
kColorBrightCyan,
make_tuple(11U)),
make_test<uint32_t, uint32_t>(
"should get kColorBrightRed when background color is 12",
kColorBrightRed,
make_tuple(12U)),
make_test<uint32_t, uint32_t>(
"should get kColorBrightMagenta when background color is 13",
kColorBrightMagenta,
make_tuple(13U)),
make_test<uint32_t, uint32_t>(
"should get kColorBrightOrange when background color is 14",
kColorBrightOrange,
make_tuple(14U)),
make_test<uint32_t, uint32_t>(
"should get kColorBrightYellow when background color is 14",
kColorBrightYellow,
make_tuple(14U)),
make_test<uint32_t, uint32_t>(
"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<uint32_t, uint32_t>(make_test_suite(
"SBF::GetForegroundColor",
fnToTest,
vector<TestTuple<uint32_t, uint32_t>>({
make_test<uint32_t, uint32_t>(
"should get kColorDarkBlack when foreground color is 0",
kColorDarkBlack,
make_tuple(0U)),
make_test<uint32_t, uint32_t>(
"should get kColorDarkBlue when foreground color is 1",
kColorDarkBlue,
make_tuple(1U)),
make_test<uint32_t, uint32_t>(
"should get kColorDarkGreen when foreground color is 2",
kColorDarkGreen,
make_tuple(2U)),
make_test<uint32_t, uint32_t>(
"should get kColorDarkCyan when foreground color is 3",
kColorDarkCyan,
make_tuple(3U)),
make_test<uint32_t, uint32_t>(
"should get kColorDarkRed when foreground color is 4",
kColorDarkRed,
make_tuple(4U)),
make_test<uint32_t, uint32_t>(
"should get kColorDarkMagenta when foreground color is 5",
kColorDarkMagenta,
make_tuple(5U)),
make_test<uint32_t, uint32_t>(
"should get kColorDarkOrange when foreground color is 6",
kColorDarkOrange,
make_tuple(6U)),
make_test<uint32_t, uint32_t>(
"should get kColorDarkYellow when foreground color is 6",
kColorDarkYellow,
make_tuple(6U)),
make_test<uint32_t, uint32_t>(
"should get kColorDarkWhite when foreground color is 7",
kColorDarkWhite,
make_tuple(7U)),
make_test<uint32_t, uint32_t>(
"should get kColorBrightBlack when foreground color is 8",
kColorBrightBlack,
make_tuple(8U)),
make_test<uint32_t, uint32_t>(
"should get kColorBrightBlue when foreground color is 9",
kColorBrightBlue,
make_tuple(9U)),
make_test<uint32_t, uint32_t>(
"should get kColorBrightGreen when foreground color is 10",
kColorBrightGreen,
make_tuple(10U)),
make_test<uint32_t, uint32_t>(
"should get kColorBrightCyan when foreground color is 11",
kColorBrightCyan,
make_tuple(11U)),
make_test<uint32_t, uint32_t>(
"should get kColorBrightRed when foreground color is 12",
kColorBrightRed,
make_tuple(12U)),
make_test<uint32_t, uint32_t>(
"should get kColorBrightMagenta when foreground color is 13",
kColorBrightMagenta,
make_tuple(13U)),
make_test<uint32_t, uint32_t>(
"should get kColorBrightOrange when foreground color is 14",
kColorBrightOrange,
make_tuple(14U)),
make_test<uint32_t, uint32_t>(
"should get kColorBrightYellow when foreground color is 14",
kColorBrightYellow,
make_tuple(14U)),
make_test<uint32_t, uint32_t>(
"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<uint32_t, uint32_t>(make_test_suite(
"SBF::SetBackgroundColor",
fnToTest,
vector<TestTuple<uint32_t, uint32_t>>({
make_test<uint32_t, uint32_t>(
"should set background color to kColorDarkBlack when color is 0",
kColorDarkBlack,
make_tuple(0U)),
make_test<uint32_t, uint32_t>(
"should set background color to kColorDarkBlue when color is 1",
kColorDarkBlue,
make_tuple(1U)),
make_test<uint32_t, uint32_t>(
"should set background color to kColorDarkGreen when color is 2",
kColorDarkGreen,
make_tuple(2U)),
make_test<uint32_t, uint32_t>(
"should set background color to kColorDarkCyan when color is 3",
kColorDarkCyan,
make_tuple(3U)),
make_test<uint32_t, uint32_t>(
"should set background color to kColorDarkRed when color is 4",
kColorDarkRed,
make_tuple(4U)),
make_test<uint32_t, uint32_t>(
"should set background color to kColorDarkMagenta when color is 5",
kColorDarkMagenta,
make_tuple(5U)),
make_test<uint32_t, uint32_t>(
"should set background color to kColorDarkOrange when color is 6",
kColorDarkOrange,
make_tuple(6U)),
make_test<uint32_t, uint32_t>(
"should set background color to kColorDarkYellow when color is 6",
kColorDarkYellow,
make_tuple(6U)),
make_test<uint32_t, uint32_t>(
"should set background color to kColorDarkWhite when color is 7",
kColorDarkWhite,
make_tuple(7U)),
make_test<uint32_t, uint32_t>(
"should set background color to kColorBrightBlack when color is 8",
kColorBrightBlack,
make_tuple(8U)),
make_test<uint32_t, uint32_t>(
"should set background color to kColorBrightBlue when color is 9",
kColorBrightBlue,
make_tuple(9U)),
make_test<uint32_t, uint32_t>(
"should set background color to kColorBrightGreen when color is 10",
kColorBrightGreen,
make_tuple(10U)),
make_test<uint32_t, uint32_t>(
"should set background color to kColorBrightCyan when color is 11",
kColorBrightCyan,
make_tuple(11U)),
make_test<uint32_t, uint32_t>(
"should set background color to kColorBrightRed when color is 12",
kColorBrightRed,
make_tuple(12U)),
make_test<uint32_t, uint32_t>(
"should set background color to kColorBrightMagenta when color is 13",
kColorBrightMagenta,
make_tuple(13U)),
make_test<uint32_t, uint32_t>(
"should set background color to kColorBrightOrange when color is 14",
kColorBrightOrange,
make_tuple(14U)),
make_test<uint32_t, uint32_t>(
"should set background color to kColorBrightYellow when color is 14",
kColorBrightYellow,
make_tuple(14U)),
make_test<uint32_t, uint32_t>(
"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<uint32_t, uint32_t>(make_test_suite(
"SBF::SetForegroundColor",
fnToTest,
vector<TestTuple<uint32_t, uint32_t>>({
make_test<uint32_t, uint32_t>(
"should set foreground color to kColorDarkBlack when color is 0",
kColorDarkBlack,
make_tuple(0U)),
make_test<uint32_t, uint32_t>(
"should set foreground color to kColorDarkBlue when color is 1",
kColorDarkBlue,
make_tuple(1U)),
make_test<uint32_t, uint32_t>(
"should set foreground color to kColorDarkGreen when color is 2",
kColorDarkGreen,
make_tuple(2U)),
make_test<uint32_t, uint32_t>(
"should set foreground color to kColorDarkCyan when color is 3",
kColorDarkCyan,
make_tuple(3U)),
make_test<uint32_t, uint32_t>(
"should set foreground color to kColorDarkRed when color is 4",
kColorDarkRed,
make_tuple(4U)),
make_test<uint32_t, uint32_t>(
"should set foreground color to kColorDarkMagenta when color is 5",
kColorDarkMagenta,
make_tuple(5U)),
make_test<uint32_t, uint32_t>(
"should set foreground color to kColorDarkOrange when color is 6",
kColorDarkOrange,
make_tuple(6U)),
make_test<uint32_t, uint32_t>(
"should set foreground color to kColorDarkYellow when color is 6",
kColorDarkYellow,
make_tuple(6U)),
make_test<uint32_t, uint32_t>(
"should set foreground color to kColorDarkWhite when color is 7",
kColorDarkWhite,
make_tuple(7U)),
make_test<uint32_t, uint32_t>(
"should set foreground color to kColorBrightBlack when color is 8",
kColorBrightBlack,
make_tuple(8U)),
make_test<uint32_t, uint32_t>(
"should set foreground color to kColorBrightBlue when color is 9",
kColorBrightBlue,
make_tuple(9U)),
make_test<uint32_t, uint32_t>(
"should set foreground color to kColorBrightGreen when color is 10",
kColorBrightGreen,
make_tuple(10U)),
make_test<uint32_t, uint32_t>(
"should set foreground color to kColorBrightCyan when color is 11",
kColorBrightCyan,
make_tuple(11U)),
make_test<uint32_t, uint32_t>(
"should set foreground color to kColorBrightRed when color is 12",
kColorBrightRed,
make_tuple(12U)),
make_test<uint32_t, uint32_t>(
"should set foreground color to kColorBrightMagenta when color is 13",
kColorBrightMagenta,
make_tuple(13U)),
make_test<uint32_t, uint32_t>(
"should set foreground color to kColorBrightOrange when color is 14",
kColorBrightOrange,
make_tuple(14U)),
make_test<uint32_t, uint32_t>(
"should set foreground color to kColorBrightYellow when color is 14",
kColorBrightYellow,
make_tuple(14U)),
make_test<uint32_t, uint32_t>(
"should set foreground color to kColorBrightWhite when color is 15",
kColorBrightWhite,
make_tuple(15U)),
})
));
}
} // End namespace Test::Colors