mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-23 01:21:42 +00:00
[wpilib] Fixup alerts (#8663)
Makes Java `Alert.Level.ERROR`, `Alert.Level.WARNING`, and `Alert.Level.INFO` proper aliases (instead of separate enum constants with the same value). Cleans up Python tests. Makes the Alert tests more consistent between languages.
This commit is contained in:
@@ -17,48 +17,56 @@ def group_name(request):
|
||||
AlertSim.resetData()
|
||||
|
||||
|
||||
def get_active_alerts(group_name: str, level: Alert.Level) -> T.List[str]:
|
||||
def get_active_alerts(level: Alert.Level) -> T.List[str]:
|
||||
return [
|
||||
a.text
|
||||
for a in AlertSim.getAll()
|
||||
if a.group == group_name and a.level == level and a.isActive()
|
||||
if a.level == level and a.isActive()
|
||||
]
|
||||
|
||||
|
||||
def is_alert_active(group_name: str, text: str, level: Alert.Level):
|
||||
def is_alert_active(text: str, level: Alert.Level):
|
||||
matches = [
|
||||
a
|
||||
for a in AlertSim.getAll()
|
||||
if a.group == group_name
|
||||
and a.level == level
|
||||
and a.text == text
|
||||
and a.isActive()
|
||||
if a.level == level and a.text == text and a.isActive()
|
||||
]
|
||||
return len(matches) > 0
|
||||
|
||||
|
||||
def assert_state(
|
||||
group_name: str,
|
||||
level: Alert.Level,
|
||||
expected_state: T.List[str],
|
||||
):
|
||||
assert expected_state == get_active_alerts(group_name, level)
|
||||
assert expected_state == get_active_alerts(level)
|
||||
|
||||
|
||||
def test_no_alerts_initially(group_name):
|
||||
assert AlertSim.getCount() == 0
|
||||
assert not AlertSim.getAll()
|
||||
|
||||
|
||||
def test_no_alerts_after_reset(group_name):
|
||||
with Alert(group_name, "alert", Alert.Level.HIGH) as alert:
|
||||
|
||||
alert.set(True)
|
||||
assert is_alert_active("alert", Alert.Level.HIGH)
|
||||
|
||||
AlertSim.resetData()
|
||||
assert AlertSim.getCount() == 0
|
||||
assert not AlertSim.getAll()
|
||||
|
||||
|
||||
def test_set_unset_single(group_name):
|
||||
with Alert(group_name, "one", Alert.Level.HIGH) as one:
|
||||
|
||||
assert not is_alert_active(group_name, "one", Alert.Level.HIGH)
|
||||
assert not is_alert_active(group_name, "two", Alert.Level.LOW)
|
||||
assert not is_alert_active("one", Alert.Level.HIGH)
|
||||
|
||||
one.set(True)
|
||||
assert is_alert_active(group_name, "one", Alert.Level.HIGH)
|
||||
|
||||
one.set(True)
|
||||
assert is_alert_active(group_name, "one", Alert.Level.HIGH)
|
||||
assert is_alert_active("one", Alert.Level.HIGH)
|
||||
|
||||
one.set(False)
|
||||
assert not is_alert_active(group_name, "one", Alert.Level.HIGH)
|
||||
assert not is_alert_active("one", Alert.Level.HIGH)
|
||||
|
||||
|
||||
def test_set_unset_multiple(group_name):
|
||||
@@ -67,25 +75,24 @@ def test_set_unset_multiple(group_name):
|
||||
Alert(group_name, "two", Alert.Level.LOW) as two,
|
||||
):
|
||||
|
||||
assert not is_alert_active(group_name, "one", Alert.Level.HIGH)
|
||||
assert not is_alert_active(group_name, "two", Alert.Level.LOW)
|
||||
assert not is_alert_active("one", Alert.Level.HIGH)
|
||||
assert not is_alert_active("two", Alert.Level.LOW)
|
||||
|
||||
one.set(True)
|
||||
assert is_alert_active(group_name, "one", Alert.Level.HIGH)
|
||||
assert not is_alert_active(group_name, "two", Alert.Level.LOW)
|
||||
assert is_alert_active("one", Alert.Level.HIGH)
|
||||
assert not is_alert_active("two", Alert.Level.LOW)
|
||||
|
||||
one.set(True)
|
||||
two.set(True)
|
||||
assert is_alert_active(group_name, "one", Alert.Level.HIGH)
|
||||
assert is_alert_active(group_name, "two", Alert.Level.LOW)
|
||||
assert is_alert_active("one", Alert.Level.HIGH)
|
||||
assert is_alert_active("two", Alert.Level.LOW)
|
||||
|
||||
one.set(False)
|
||||
assert not is_alert_active(group_name, "one", Alert.Level.HIGH)
|
||||
assert is_alert_active(group_name, "two", Alert.Level.LOW)
|
||||
assert not is_alert_active("one", Alert.Level.HIGH)
|
||||
assert is_alert_active("two", Alert.Level.LOW)
|
||||
|
||||
|
||||
def test_set_is_idempotent(group_name):
|
||||
group_name = group_name
|
||||
with (
|
||||
Alert(group_name, "A", Alert.Level.LOW) as a,
|
||||
Alert(group_name, "B", Alert.Level.LOW) as b,
|
||||
@@ -96,45 +103,43 @@ def test_set_is_idempotent(group_name):
|
||||
b.set(True)
|
||||
c.set(True)
|
||||
|
||||
start_state = get_active_alerts(group_name, Alert.Level.LOW)
|
||||
start_state = get_active_alerts(Alert.Level.LOW)
|
||||
assert set(start_state) == {"A", "B", "C"}
|
||||
|
||||
b.set(True)
|
||||
assert_state(group_name, Alert.Level.LOW, start_state)
|
||||
assert_state(Alert.Level.LOW, start_state)
|
||||
|
||||
a.set(True)
|
||||
assert_state(group_name, Alert.Level.LOW, start_state)
|
||||
assert_state(Alert.Level.LOW, start_state)
|
||||
|
||||
|
||||
def test_close_unsets_alert(group_name):
|
||||
group_name = group_name
|
||||
with Alert(group_name, "alert", Alert.Level.MEDIUM) as alert:
|
||||
alert.set(True)
|
||||
assert is_alert_active(group_name, "alert", Alert.Level.MEDIUM)
|
||||
assert not is_alert_active(group_name, "alert", Alert.Level.MEDIUM)
|
||||
assert is_alert_active("alert", Alert.Level.MEDIUM)
|
||||
assert not is_alert_active("alert", Alert.Level.MEDIUM)
|
||||
|
||||
|
||||
def test_set_text_while_unset(group_name):
|
||||
group_name = group_name
|
||||
with Alert(group_name, "BEFORE", Alert.Level.LOW) as alert:
|
||||
assert alert.getText() == "BEFORE"
|
||||
alert.set(True)
|
||||
assert is_alert_active(group_name, "BEFORE", Alert.Level.LOW)
|
||||
assert is_alert_active("BEFORE", Alert.Level.LOW)
|
||||
alert.set(False)
|
||||
assert not is_alert_active(group_name, "BEFORE", Alert.Level.LOW)
|
||||
assert not is_alert_active("BEFORE", Alert.Level.LOW)
|
||||
alert.setText("AFTER")
|
||||
assert alert.getText() == "AFTER"
|
||||
alert.set(True)
|
||||
assert not is_alert_active(group_name, "BEFORE", Alert.Level.LOW)
|
||||
assert is_alert_active(group_name, "AFTER", Alert.Level.LOW)
|
||||
assert not is_alert_active("BEFORE", Alert.Level.LOW)
|
||||
assert is_alert_active("AFTER", Alert.Level.LOW)
|
||||
|
||||
|
||||
def test_set_text_while_set(group_name):
|
||||
with Alert(group_name, "BEFORE", Alert.Level.LOW) as alert:
|
||||
assert alert.getText() == "BEFORE"
|
||||
alert.set(True)
|
||||
assert is_alert_active(group_name, "BEFORE", Alert.Level.LOW)
|
||||
assert is_alert_active("BEFORE", Alert.Level.LOW)
|
||||
alert.setText("AFTER")
|
||||
assert alert.getText() == "AFTER"
|
||||
assert not is_alert_active(group_name, "BEFORE", Alert.Level.LOW)
|
||||
assert is_alert_active(group_name, "AFTER", Alert.Level.LOW)
|
||||
assert not is_alert_active("BEFORE", Alert.Level.LOW)
|
||||
assert is_alert_active("AFTER", Alert.Level.LOW)
|
||||
|
||||
Reference in New Issue
Block a user