mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-01 02:41:48 +00:00
[copybara] Sync with mostrobotpy (#8820)
GitOrigin-RevId: f03f29e57af22a74b680873090028b9c9f5c8063
This commit is contained in:
@@ -18,11 +18,7 @@ def group_name(request):
|
||||
|
||||
|
||||
def get_active_alerts(level: Alert.Level) -> T.List[str]:
|
||||
return [
|
||||
a.text
|
||||
for a in AlertSim.getAll()
|
||||
if a.level == level and a.isActive()
|
||||
]
|
||||
return [a.text for a in AlertSim.getAll() if a.level == level and a.isActive()]
|
||||
|
||||
|
||||
def is_alert_active(text: str, level: Alert.Level):
|
||||
@@ -144,6 +140,7 @@ def test_set_text_while_set(group_name):
|
||||
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,
|
||||
@@ -154,10 +151,10 @@ def test_get_active(group_name):
|
||||
a.set(True)
|
||||
b.set(True)
|
||||
c.set(False)
|
||||
|
||||
|
||||
active = AlertSim.getActive()
|
||||
allAlerts = AlertSim.getAll()
|
||||
|
||||
|
||||
assert len(active) == 2
|
||||
assert len(allAlerts) == 3
|
||||
|
||||
@@ -169,4 +166,4 @@ def test_get_active(group_name):
|
||||
allAlerts = AlertSim.getAll()
|
||||
assert len(active) == 1
|
||||
assert len(allAlerts) == 3
|
||||
assert active[0].text == "B"
|
||||
assert active[0].text == "B"
|
||||
|
||||
@@ -41,6 +41,7 @@ class MockRobot(OpModeRobot):
|
||||
super().__init__()
|
||||
self.driver_station_connected_count = 0
|
||||
self.none_periodic_count = 0
|
||||
self.periodic_count = 0
|
||||
|
||||
def driverStationConnected(self):
|
||||
self.driver_station_connected_count += 1
|
||||
@@ -48,6 +49,9 @@ class MockRobot(OpModeRobot):
|
||||
def nonePeriodic(self):
|
||||
self.none_periodic_count += 1
|
||||
|
||||
def robotPeriodic(self):
|
||||
self.periodic_count += 1
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def sim_timing_setup():
|
||||
@@ -135,8 +139,8 @@ def test_remove_op_mode():
|
||||
assert options[0].name == "OneArgOpMode"
|
||||
|
||||
|
||||
@pytest.mark.xfail(reason="wpilib bug")
|
||||
def test_none_periodic():
|
||||
@pytest.fixture
|
||||
def periodic_robot_test_fixture():
|
||||
class MyMockRobot(MockRobot):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
@@ -146,14 +150,40 @@ def test_none_periodic():
|
||||
robot = MyMockRobot()
|
||||
|
||||
robot_thread = threading.Thread(target=robot.startCompetition)
|
||||
robot_thread.daemon = True # Make thread daemon so it doesn't block test exit
|
||||
robot_thread.start()
|
||||
|
||||
yield robot
|
||||
|
||||
robot.endCompetition()
|
||||
robot_thread.join()
|
||||
|
||||
|
||||
# @pytest.mark.xfail(reason="wpilib bug")
|
||||
def test_none_periodic(periodic_robot_test_fixture):
|
||||
robot = periodic_robot_test_fixture
|
||||
|
||||
wsim.waitForProgramStart()
|
||||
|
||||
# Time step to get periodic calls on 20 ms robot loop
|
||||
wsim.stepTiming(0.110)
|
||||
|
||||
assert robot.none_periodic_count == 2
|
||||
assert robot.none_periodic_count == 5
|
||||
|
||||
robot.endCompetition()
|
||||
robot_thread.join(timeout=1.0) # Add timeout to prevent hanging
|
||||
|
||||
def test_robot_periodic(periodic_robot_test_fixture):
|
||||
kPeriod = 0.020 # 20 ms
|
||||
|
||||
robot = periodic_robot_test_fixture
|
||||
|
||||
wsim.waitForProgramStart()
|
||||
|
||||
# RobotPeriodic should be called regardless of state
|
||||
assert robot.periodic_count == 0
|
||||
|
||||
# Time step to get periodic calls on 20 ms robot loop
|
||||
wsim.stepTiming(kPeriod)
|
||||
assert robot.periodic_count == 1
|
||||
|
||||
# Additional time steps should continue calling RobotPeriodic
|
||||
wsim.stepTiming(kPeriod)
|
||||
assert robot.periodic_count == 2
|
||||
|
||||
Reference in New Issue
Block a user