mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-05 03:21:42 +00:00
[wpilib] Add AlertSim function to get only active alerts (#8732)
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
|
||||
#include "wpi/simulation/AlertSim.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
@@ -35,6 +36,12 @@ std::vector<AlertSim::AlertInfo> AlertSim::GetAll() {
|
||||
return infos;
|
||||
}
|
||||
|
||||
std::vector<AlertSim::AlertInfo> AlertSim::GetActive() {
|
||||
auto infos = GetAll();
|
||||
std::erase_if(infos, [](const AlertInfo& info) { return !info.isActive(); });
|
||||
return infos;
|
||||
}
|
||||
|
||||
void AlertSim::ResetData() {
|
||||
HALSIM_ResetAlertData();
|
||||
}
|
||||
|
||||
@@ -55,12 +55,19 @@ class AlertSim final {
|
||||
static int32_t GetCount();
|
||||
|
||||
/**
|
||||
* Gets detailed information about each alert.
|
||||
* Gets detailed information about each alert (including inactive ones).
|
||||
*
|
||||
* @return Alerts
|
||||
*/
|
||||
static std::vector<AlertInfo> GetAll();
|
||||
|
||||
/**
|
||||
* Gets detailed information about all active alerts.
|
||||
*
|
||||
* @return Alerts
|
||||
*/
|
||||
static std::vector<AlertInfo> GetActive();
|
||||
|
||||
/**
|
||||
* Resets all alert simulation data.
|
||||
*/
|
||||
|
||||
@@ -3,6 +3,7 @@ classes:
|
||||
methods:
|
||||
GetCount:
|
||||
GetAll:
|
||||
GetActive:
|
||||
ResetData:
|
||||
wpi::sim::AlertSim::AlertInfo:
|
||||
attributes:
|
||||
|
||||
@@ -155,4 +155,28 @@ TEST_F(AlertSimTest, SetTextWhileSet) {
|
||||
EXPECT_TRUE(IsAlertActive("AFTER", Alert::Level::LOW));
|
||||
}
|
||||
|
||||
TEST_F(AlertSimTest, GetActive) {
|
||||
auto a = MakeAlert("A", Alert::Level::HIGH);
|
||||
auto b = MakeAlert("B", Alert::Level::HIGH);
|
||||
auto c = MakeAlert("C", Alert::Level::HIGH);
|
||||
a.Set(true);
|
||||
b.Set(true);
|
||||
c.Set(false);
|
||||
|
||||
auto active = AlertSim::GetActive();
|
||||
auto all = AlertSim::GetAll();
|
||||
EXPECT_EQ(active.size(), 2u);
|
||||
EXPECT_EQ(all.size(), 3u);
|
||||
EXPECT_TRUE((active[0].text == "A" && active[1].text == "B") ||
|
||||
(active[0].text == "B" && active[1].text == "A"));
|
||||
|
||||
a.Set(false);
|
||||
|
||||
active = AlertSim::GetActive();
|
||||
all = AlertSim::GetAll();
|
||||
EXPECT_EQ(active.size(), 1u);
|
||||
EXPECT_EQ(all.size(), 3u);
|
||||
EXPECT_EQ(active[0].text, "B");
|
||||
}
|
||||
|
||||
} // namespace wpi::sim
|
||||
|
||||
@@ -143,3 +143,30 @@ def test_set_text_while_set(group_name):
|
||||
assert alert.getText() == "AFTER"
|
||||
assert not is_alert_active("BEFORE", Alert.Level.LOW)
|
||||
assert is_alert_active("AFTER", Alert.Level.LOW)
|
||||
|
||||
def test_get_active(group_name):
|
||||
with (
|
||||
Alert(group_name, "A", Alert.Level.HIGH) as a,
|
||||
Alert(group_name, "B", Alert.Level.HIGH) as b,
|
||||
Alert(group_name, "C", Alert.Level.HIGH) as c,
|
||||
):
|
||||
|
||||
a.set(True)
|
||||
b.set(True)
|
||||
c.set(False)
|
||||
|
||||
active = AlertSim.getActive()
|
||||
allAlerts = AlertSim.getAll()
|
||||
|
||||
assert len(active) == 2
|
||||
assert len(allAlerts) == 3
|
||||
|
||||
activeTexts = [a.text for a in active]
|
||||
assert set(activeTexts) == {"A", "B"}
|
||||
|
||||
a.set(False)
|
||||
active = AlertSim.getActive()
|
||||
allAlerts = AlertSim.getAll()
|
||||
assert len(active) == 1
|
||||
assert len(allAlerts) == 3
|
||||
assert active[0].text == "B"
|
||||
@@ -71,7 +71,7 @@ public final class AlertSim {
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets detailed information about each alert.
|
||||
* Gets detailed information about each alert (including inactive ones).
|
||||
*
|
||||
* @return Alerts
|
||||
*/
|
||||
@@ -84,6 +84,18 @@ public final class AlertSim {
|
||||
return infos;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets detailed information about all active alerts.
|
||||
*
|
||||
* @return Alerts
|
||||
*/
|
||||
public static AlertInfo[] getActive() {
|
||||
AlertInfo[] alertInfos = getAll();
|
||||
return java.util.Arrays.stream(alertInfos)
|
||||
.filter(info -> info.activeStartTime != 0)
|
||||
.toArray(AlertInfo[]::new);
|
||||
}
|
||||
|
||||
/** Resets all alert simulation data. */
|
||||
public static void resetData() {
|
||||
AlertDataJNI.resetData();
|
||||
|
||||
@@ -157,4 +157,29 @@ class AlertSimTest {
|
||||
assertTrue(isAlertActive("AFTER", Level.LOW));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void getActive() {
|
||||
try (var a = makeAlert("A", Level.HIGH);
|
||||
var b = makeAlert("B", Level.HIGH);
|
||||
var c = makeAlert("C", Level.HIGH)) {
|
||||
a.set(true);
|
||||
b.set(true);
|
||||
c.set(false);
|
||||
|
||||
var active = AlertSim.getActive();
|
||||
var all = AlertSim.getAll();
|
||||
assertEquals(2, active.length);
|
||||
assertEquals(3, all.length);
|
||||
assertTrue(Arrays.stream(active).anyMatch(x -> "A".equals(x.text)));
|
||||
assertTrue(Arrays.stream(active).anyMatch(x -> "B".equals(x.text)));
|
||||
|
||||
a.set(false);
|
||||
active = AlertSim.getActive();
|
||||
all = AlertSim.getAll();
|
||||
assertEquals(1, active.length);
|
||||
assertEquals(3, all.length);
|
||||
assertEquals(active[0].text, "B");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user