Skip to content

Commit

Permalink
Add C++ tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rzblue committed Oct 25, 2024
1 parent 024fbed commit c9c509d
Showing 1 changed file with 89 additions and 18 deletions.
107 changes: 89 additions & 18 deletions wpilibc/src/test/native/cpp/AlertsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// the WPILib BSD license file in the root directory of this project.

#include <frc/Alert.h>
#include <frc/smartdashboard/SmartDashboard.h>

#include <algorithm>
#include <chrono>
Expand All @@ -20,9 +21,11 @@ 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);
// test all destructors
Update();
EXPECT_EQ(GetSubscriberForType(kError).Get().size(), 0ul);
EXPECT_EQ(GetSubscriberForType(kWarning).Get().size(), 0ul);
EXPECT_EQ(GetSubscriberForType(kInfo).Get().size(), 0ul);
}

template <typename... Args>
Expand All @@ -37,12 +40,18 @@ class AlertsTest : public ::testing::Test {
testInfo->test_case_name());
}

// todo: this
void ExpectAlertsState() {}

bool IsAlertActive(std::string_view text, Alert::AlertType type) {
std::vector<std::string> activeAlerts = GetSubscriberForType(type).Get();
Update();
auto activeAlerts = GetSubscriberForType(type).Get();
return std::find(activeAlerts.begin(), activeAlerts.end(), text) !=
activeAlerts.end();
}

void Update() { frc::SmartDashboard::UpdateValues(); }

private:
std::string GetSubtableName(Alert::AlertType type) {
switch (type) {
Expand Down Expand Up @@ -74,20 +83,82 @@ class AlertsTest : public ::testing::Test {
}
};

TEST_F(AlertsTest, Constructors) {
// Make and destroy
Alert{GetGroupName(), "One", kError};
Alert a{GetGroupName(), "Two", kInfo};
a.Set(true);
a.Set(false);
// Move assign
a = Alert(GetGroupName(), "Three", kWarning);
// Move construct
Alert b{std::move(a)};
TEST_F(AlertsTest, SetUnset) {
auto one = MakeAlert("one", kError);
auto two = MakeAlert("two", kInfo);
EXPECT_FALSE(IsAlertActive("one", kError));
EXPECT_FALSE(IsAlertActive("two", kInfo));
one.Set(true);
EXPECT_TRUE(IsAlertActive("one", kError));
EXPECT_FALSE(IsAlertActive("two", kInfo));
one.Set(true);
two.Set(true);
EXPECT_TRUE(IsAlertActive("one", kError));
EXPECT_TRUE(IsAlertActive("two", kInfo));
one.Set(false);
EXPECT_FALSE(IsAlertActive("one", kError));
EXPECT_TRUE(IsAlertActive("two", kInfo));
}

TEST_F(AlertsTest, DestructorUnsetsAlert) {
{
// Move assign with dynamic string
std::string text = fmt::format(
"{}", std::chrono::steady_clock::now().time_since_epoch().count());
b = Alert(text, kError);
auto alert = MakeAlert("alert", kWarning);
alert.Set(true);
EXPECT_TRUE(IsAlertActive("alert", kWarning));
}
EXPECT_FALSE(IsAlertActive("alert", kWarning));
}

TEST_F(AlertsTest, SetTextWhileUnset) {
auto alert = MakeAlert("BEFORE", kInfo);
EXPECT_EQ("BEFORE", alert.GetText());
alert.Set(true);
EXPECT_TRUE(IsAlertActive("BEFORE", kInfo));
alert.Set(false);
EXPECT_FALSE(IsAlertActive("BEFORE", kInfo));
alert.SetText("AFTER");
EXPECT_EQ("AFTER", alert.GetText());
alert.Set(true);
EXPECT_FALSE(IsAlertActive("BEFORE", kInfo));
EXPECT_TRUE(IsAlertActive("AFTER", kInfo));
}

TEST_F(AlertsTest, SetTextWhileSet) {
auto alert = MakeAlert("BEFORE", kInfo);
EXPECT_EQ("BEFORE", alert.GetText());
alert.Set(true);
EXPECT_TRUE(IsAlertActive("BEFORE", kInfo));
alert.SetText("AFTER");
EXPECT_EQ("AFTER", alert.GetText());
EXPECT_FALSE(IsAlertActive("BEFORE", kInfo));
EXPECT_TRUE(IsAlertActive("AFTER", kInfo));
}

TEST_F(AlertsTest, MoveAssign) {
auto outer = MakeAlert("outer", kInfo);
outer.Set(true);
EXPECT_TRUE(IsAlertActive("outer", kInfo));

{
auto inner = MakeAlert("inner", kWarning);
inner.Set(true);
EXPECT_TRUE(IsAlertActive("inner", kWarning));
outer = std::move(inner);
// Assignment target should be unset and invalidated as part of move, before
// destruction
EXPECT_FALSE(IsAlertActive("outer", kInfo));
}
EXPECT_TRUE(IsAlertActive("inner", kWarning));
}

TEST_F(AlertsTest, MoveConstruct) {
auto a = MakeAlert("A", kInfo);
a.Set(true);
EXPECT_TRUE(IsAlertActive("A", kInfo));
Alert b{std::move(a)};
EXPECT_TRUE(IsAlertActive("A", kInfo));
b.Set(false);
EXPECT_FALSE(IsAlertActive("A", kInfo));
b.Set(true);
EXPECT_TRUE(IsAlertActive("A", kInfo));
}

0 comments on commit c9c509d

Please sign in to comment.