Skip to content

Commit f91e096

Browse files
committed
Add subset of translated tests
1 parent 526697f commit f91e096

10 files changed

+536
-0
lines changed

tests/conftest.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import pytest
2+
3+
import commands2
4+
from wpilib.simulation import DriverStationSim
5+
6+
7+
@pytest.fixture(autouse=True)
8+
def scheduler():
9+
commands2.CommandScheduler.resetInstance()
10+
DriverStationSim.setEnabled(True)
11+
return commands2.CommandScheduler.getInstance()

tests/run_tests.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env python3
2+
3+
import os
4+
from os.path import abspath, dirname
5+
import sys
6+
import subprocess
7+
8+
if __name__ == "__main__":
9+
10+
root = abspath(dirname(__file__))
11+
os.chdir(root)
12+
13+
subprocess.check_call([sys.executable, "-m", "py.test"])

tests/test_button.py

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
import commands2
2+
import commands2.button
3+
4+
from util import Counter
5+
6+
7+
class MyButton(commands2.button.Button):
8+
def __init__(self):
9+
super().__init__(self.isPressed)
10+
self.pressed = False
11+
12+
def isPressed(self) -> bool:
13+
return self.pressed
14+
15+
def setPressed(self, value: bool):
16+
self.pressed = value
17+
18+
19+
class MyCommand(commands2.CommandBase):
20+
executed = 0
21+
22+
ended = 0
23+
canceled = 0
24+
25+
def execute(self) -> None:
26+
self.executed += 1
27+
28+
def end(self, interrupted: bool) -> None:
29+
self.ended += 1
30+
if interrupted:
31+
self.canceled += 1
32+
33+
34+
def test_when_pressed(scheduler: commands2.CommandScheduler):
35+
cmd1 = MyCommand()
36+
button = MyButton()
37+
button.setPressed(False)
38+
39+
button.whenPressed(cmd1)
40+
scheduler.run()
41+
42+
assert not cmd1.executed
43+
assert not scheduler.isScheduled(cmd1)
44+
45+
button.setPressed(True)
46+
scheduler.run()
47+
scheduler.run()
48+
49+
assert cmd1.executed
50+
assert scheduler.isScheduled(cmd1)
51+
52+
53+
def test_when_released(scheduler: commands2.CommandScheduler):
54+
cmd1 = MyCommand()
55+
button = MyButton()
56+
button.setPressed(True)
57+
58+
button.whenReleased(cmd1)
59+
scheduler.run()
60+
61+
assert not cmd1.executed
62+
assert not scheduler.isScheduled(cmd1)
63+
64+
button.setPressed(False)
65+
scheduler.run()
66+
scheduler.run()
67+
68+
assert cmd1.executed
69+
assert scheduler.isScheduled(cmd1)
70+
71+
72+
def test_while_held(scheduler: commands2.CommandScheduler):
73+
cmd1 = MyCommand()
74+
button = MyButton()
75+
button.setPressed(False)
76+
77+
button.whileHeld(cmd1)
78+
scheduler.run()
79+
80+
assert not cmd1.executed
81+
assert not scheduler.isScheduled(cmd1)
82+
83+
button.setPressed(True)
84+
scheduler.run()
85+
scheduler.run()
86+
87+
assert cmd1.executed == 2
88+
assert scheduler.isScheduled(cmd1)
89+
90+
button.setPressed(False)
91+
scheduler.run()
92+
93+
assert cmd1.executed == 2
94+
assert not scheduler.isScheduled(cmd1)
95+
96+
97+
def test_when_held(scheduler: commands2.CommandScheduler):
98+
cmd1 = MyCommand()
99+
button = MyButton()
100+
button.setPressed(False)
101+
102+
button.whenHeld(cmd1)
103+
scheduler.run()
104+
105+
assert not cmd1.executed
106+
assert not scheduler.isScheduled(cmd1)
107+
108+
button.setPressed(True)
109+
scheduler.run()
110+
scheduler.run()
111+
112+
assert cmd1.executed == 2
113+
assert scheduler.isScheduled(cmd1)
114+
115+
button.setPressed(False)
116+
scheduler.run()
117+
118+
assert cmd1.executed == 2
119+
assert not scheduler.isScheduled(cmd1)
120+
121+
122+
def test_toggle_when_pressed(scheduler: commands2.CommandScheduler):
123+
cmd1 = MyCommand()
124+
button = MyButton()
125+
button.setPressed(False)
126+
127+
button.toggleWhenPressed(cmd1)
128+
scheduler.run()
129+
130+
assert not cmd1.executed
131+
assert not scheduler.isScheduled(cmd1)
132+
133+
button.setPressed(True)
134+
scheduler.run()
135+
136+
assert cmd1.executed
137+
assert scheduler.isScheduled(cmd1)
138+
139+
140+
def test_cancel_when_pressed(scheduler: commands2.CommandScheduler):
141+
cmd1 = MyCommand()
142+
button = MyButton()
143+
button.setPressed(False)
144+
145+
scheduler.schedule(cmd1)
146+
147+
button.cancelWhenPressed(cmd1)
148+
scheduler.run()
149+
150+
assert cmd1.executed == 1
151+
assert cmd1.ended == 0
152+
assert cmd1.canceled == 0
153+
assert scheduler.isScheduled(cmd1)
154+
155+
button.setPressed(True)
156+
scheduler.run()
157+
scheduler.run()
158+
159+
assert cmd1.executed == 1
160+
assert cmd1.ended == 1
161+
assert cmd1.canceled == 1
162+
assert not scheduler.isScheduled(cmd1)
163+
164+
165+
def test_function_bindings(scheduler: commands2.CommandScheduler):
166+
167+
buttonWhenPressed = MyButton()
168+
buttonWhileHeld = MyButton()
169+
buttonWhenReleased = MyButton()
170+
171+
buttonWhenPressed.setPressed(False)
172+
buttonWhileHeld.setPressed(True)
173+
buttonWhenReleased.setPressed(True)
174+
175+
counter = Counter()
176+
177+
buttonWhenPressed.whenPressed(counter.increment)
178+
buttonWhileHeld.whileHeld(counter.increment)
179+
buttonWhenReleased.whenReleased(counter.increment)
180+
181+
scheduler.run()
182+
buttonWhenPressed.setPressed(True)
183+
buttonWhenReleased.setPressed(False)
184+
scheduler.run()
185+
186+
assert counter.value == 4
187+
188+
189+
def test_button_composition(scheduler: commands2.CommandScheduler):
190+
191+
button1 = MyButton()
192+
button2 = MyButton()
193+
194+
button1.setPressed(True)
195+
button2.setPressed(False)
196+
197+
# TODO: not sure if this is a great idea?
198+
assert button1
199+
assert not button2
200+
201+
assert not button1.and_(button2)
202+
assert button1.or_(button2)
203+
assert not button1.not_()
204+
assert button1.and_(button2.not_())

tests/test_command_decorators.py

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
from util import ConditionHolder, ManualSimTime
2+
import commands2
3+
4+
5+
def test_with_timeout(scheduler: commands2.CommandScheduler):
6+
with ManualSimTime() as tm:
7+
8+
cmd = commands2.WaitCommand(10)
9+
timeout = cmd.withTimeout(2)
10+
11+
scheduler.schedule(timeout)
12+
scheduler.run()
13+
14+
assert not scheduler.isScheduled(cmd)
15+
assert scheduler.isScheduled(timeout)
16+
17+
tm.step(3)
18+
19+
scheduler.run()
20+
assert not scheduler.isScheduled(timeout)
21+
22+
23+
def test_with_interrupt(scheduler: commands2.CommandScheduler):
24+
25+
cond = ConditionHolder()
26+
27+
cmd = commands2.WaitCommand(10)
28+
timeout = cmd.withInterrupt(cond.getCondition)
29+
30+
scheduler.schedule(timeout)
31+
scheduler.run()
32+
33+
assert not scheduler.isScheduled(cmd)
34+
assert scheduler.isScheduled(timeout)
35+
36+
cond.setTrue()
37+
38+
scheduler.run()
39+
assert not scheduler.isScheduled(cmd)
40+
41+
42+
def test_before_starting(scheduler: commands2.CommandScheduler):
43+
44+
cond = ConditionHolder()
45+
46+
cmd = commands2.InstantCommand().beforeStarting(cond.setTrue)
47+
48+
scheduler.schedule(cmd)
49+
50+
assert cond.getCondition()
51+
52+
scheduler.run()
53+
scheduler.run()
54+
55+
assert not scheduler.isScheduled(cmd)
56+
57+
58+
def test_and_then_fn(scheduler: commands2.CommandScheduler):
59+
cond = ConditionHolder()
60+
61+
cmd = commands2.InstantCommand().andThen(cond.setTrue)
62+
63+
scheduler.schedule(cmd)
64+
65+
assert not cond.getCondition()
66+
67+
scheduler.run()
68+
scheduler.run()
69+
70+
assert not scheduler.isScheduled(cmd)
71+
assert cond.getCondition()
72+
73+
74+
def test_and_then_commands(scheduler: commands2.CommandScheduler):
75+
cond = ConditionHolder()
76+
77+
cmd1 = commands2.InstantCommand()
78+
cmd2 = commands2.InstantCommand(cond.setTrue)
79+
80+
scheduler.schedule(cmd1.andThen(cmd2))
81+
82+
assert not cond.getCondition()
83+
84+
scheduler.run()
85+
86+
assert cond.getCondition()
87+
88+
89+
def test_deadline_with(scheduler: commands2.CommandScheduler):
90+
cond = ConditionHolder()
91+
92+
dictator = commands2.WaitUntilCommand(cond.getCondition)
93+
endsBefore = commands2.InstantCommand()
94+
endsAfter = commands2.WaitUntilCommand(lambda: False)
95+
96+
group = dictator.deadlineWith(endsBefore, endsAfter)
97+
98+
scheduler.schedule(group)
99+
scheduler.run()
100+
101+
assert scheduler.isScheduled(group)
102+
103+
cond.setTrue()
104+
scheduler.run()
105+
106+
assert not scheduler.isScheduled(group)
107+
108+
109+
def test_along_with(scheduler: commands2.CommandScheduler):
110+
cond = ConditionHolder(False)
111+
112+
cmd1 = commands2.WaitUntilCommand(cond.getCondition)
113+
cmd2 = commands2.InstantCommand()
114+
115+
group = cmd1.alongWith(cmd2)
116+
117+
scheduler.schedule(group)
118+
scheduler.run()
119+
120+
assert scheduler.isScheduled(group)
121+
122+
cond.setTrue()
123+
scheduler.run()
124+
125+
assert not scheduler.isScheduled(group)
126+
127+
128+
def test_race_with(scheduler: commands2.CommandScheduler):
129+
cmd1 = commands2.WaitUntilCommand(lambda: False)
130+
cmd2 = commands2.InstantCommand()
131+
132+
group = cmd1.raceWith(cmd2)
133+
134+
scheduler.schedule(group)
135+
scheduler.run()
136+
137+
assert not scheduler.isScheduled(group)
138+
139+
140+
def test_perpetually(scheduler: commands2.CommandScheduler):
141+
142+
cmd = commands2.InstantCommand().perpetually()
143+
144+
scheduler.schedule(cmd)
145+
146+
scheduler.run()
147+
scheduler.run()
148+
149+
assert scheduler.isScheduled(cmd)

0 commit comments

Comments
 (0)