From c40a06d780edaef00dbe96ca7291f2a01d6b534d Mon Sep 17 00:00:00 2001 From: Ryan Blue Date: Thu, 24 Oct 2024 15:23:52 -0400 Subject: [PATCH] Add initial c++ testing infrastructure --- wpilibc/src/test/native/cpp/AlertsTest.cpp | 75 ++++++++++++++++++++-- 1 file changed, 69 insertions(+), 6 deletions(-) diff --git a/wpilibc/src/test/native/cpp/AlertsTest.cpp b/wpilibc/src/test/native/cpp/AlertsTest.cpp index dba0be1162d..947e7423d00 100644 --- a/wpilibc/src/test/native/cpp/AlertsTest.cpp +++ b/wpilibc/src/test/native/cpp/AlertsTest.cpp @@ -4,27 +4,90 @@ #include +#include #include +#include #include #include #include #include +#include +#include -TEST(AlertsTest, Smoke) { +using namespace frc; +using enum Alert::AlertType; +class AlertsTest : public ::testing::Test { + public: + ~AlertsTest() { + EXPECT_EQ(GetSubscriberForType(kError).Get().size(), 0); + EXPECT_EQ(GetSubscriberForType(kWarning).Get().size(), 0); + EXPECT_EQ(GetSubscriberForType(kInfo).Get().size(), 0); + } + + template + Alert MakeAlert(Args&&... args) { + return Alert(GetGroupName(), std::forward(args)...); + } + + std::string GetGroupName() { + const ::testing::TestInfo* testInfo = + ::testing::UnitTest::GetInstance()->current_test_info(); + return fmt::format("{}_{}", testInfo->test_suite_name(), + testInfo->test_case_name()); + } + + bool IsAlertActive(std::string_view text, Alert::AlertType type) { + std::vector activeAlerts = GetSubscriberForType(type).Get(); + return std::find(activeAlerts.begin(), activeAlerts.end(), text) != + activeAlerts.end(); + } + + private: + std::string GetSubtableName(Alert::AlertType type) { + switch (type) { + case kError: + return "errors"; + case kWarning: + return "warnings"; + case kInfo: + return "infos"; + default: + return "unknown"; + } + } + + std::map m_subs; + const nt::StringArraySubscriber& GetSubscriberForType(Alert::AlertType type) { + if (m_subs.contains(type)) { + return m_subs[type]; + } else { + return m_subs + .emplace(std::make_pair( + type, nt::NetworkTableInstance::GetDefault() + .GetStringArrayTopic( + fmt::format("/SmartDashboard/{}/{}", GetGroupName(), + GetSubtableName(type))) + .Subscribe({}))) + .first->second; + } + } +}; + +TEST_F(AlertsTest, Constructors) { // Make and destroy - frc::Alert("One", frc::Alert::AlertType::kError); - frc::Alert a{"Two", frc::Alert::AlertType::kInfo}; + Alert{GetGroupName(), "One", kError}; + Alert a{GetGroupName(), "Two", kInfo}; a.Set(true); a.Set(false); // Move assign - a = frc::Alert("Three", frc::Alert::AlertType::kWarning); + a = Alert(GetGroupName(), "Three", kWarning); // Move construct - frc::Alert b{std::move(a)}; + Alert b{std::move(a)}; { // Move assign with dynamic string std::string text = fmt::format( "{}", std::chrono::steady_clock::now().time_since_epoch().count()); - b = frc::Alert(text, frc::Alert::AlertType::kError); + b = Alert(text, kError); } }