Skip to content

Commit 4df3f31

Browse files
TheTripleVvirtuald
authored andcommitted
port latest test changes
1 parent 8167ac0 commit 4df3f31

11 files changed

+597
-36
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
description="WPILib command framework v2",
1212
url="https://github.com/robotpy/robotpy-commands-v2",
1313
packages=["commands2"],
14-
install_requires=["wpilib<2025,>=2024.0.0b2", "typing_extensions>=4.1.0,<5"],
1514
license="BSD-3-Clause",
15+
install_requires=["wpilib<2025,>=2024.0.0b2", "typing_extensions>=4.1.0,<5"],
1616
python_requires=">=3.8",
1717
include_package_data=True,
1818
)

tests/test_command_decorators.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,14 @@ def test_raceWith(scheduler: commands2.CommandScheduler):
126126
assert not group.isScheduled()
127127

128128

129-
def test_perpetually(scheduler: commands2.CommandScheduler):
130-
command = commands2.InstantCommand()
131-
perpetual = command.perpetually()
132-
scheduler.schedule(perpetual)
133-
scheduler.run()
134-
scheduler.run()
135-
scheduler.run()
136-
assert perpetual.isScheduled()
129+
# def test_perpetually(scheduler: commands2.CommandScheduler):
130+
# command = commands2.InstantCommand()
131+
# perpetual = command.perpetually()
132+
# scheduler.schedule(perpetual)
133+
# scheduler.run()
134+
# scheduler.run()
135+
# scheduler.run()
136+
# assert perpetual.isScheduled()
137137

138138

139139
@pytest.mark.skipif(IS_OLD_COMMANDS, reason="not in old commands")

tests/test_command_schedule.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
if TYPE_CHECKING:
77
from .util import *
88

9-
import pytest
9+
from ntcore import NetworkTableInstance
10+
from wpilib import SmartDashboard
1011

12+
import pytest
1113

1214
def test_instantSchedule(scheduler: commands2.CommandScheduler):
1315
command = commands2.Command()
@@ -88,3 +90,21 @@ def test_notScheduledCancel(scheduler: commands2.CommandScheduler):
8890
command = commands2.Command()
8991

9092
scheduler.cancel(command)
93+
94+
95+
def test_smartDashboardCancelTest(scheduler: commands2.CommandScheduler, nt_instance: NetworkTableInstance):
96+
SmartDashboard.putData("Scheduler", scheduler)
97+
SmartDashboard.updateValues()
98+
99+
command = commands2.Command()
100+
scheduler.schedule(command)
101+
scheduler.run()
102+
SmartDashboard.updateValues()
103+
assert scheduler.isScheduled(command)
104+
105+
table = nt_instance.getTable("SmartDashboard")
106+
table.getEntry("Scheduler/Cancel").setIntegerArray([id(command)])
107+
SmartDashboard.updateValues()
108+
scheduler.run()
109+
assert not scheduler.isScheduled(command)
110+

tests/test_command_sendable_button.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
from typing import TYPE_CHECKING
2+
3+
import commands2
4+
from util import * # type: ignore
5+
6+
if TYPE_CHECKING:
7+
from .util import *
8+
9+
from ntcore import NetworkTableInstance
10+
from wpilib import SmartDashboard
11+
12+
import pytest
13+
14+
def test_trueAndNotScheduledSchedules(scheduler: commands2.CommandScheduler, nt_instance: NetworkTableInstance):
15+
schedule = OOInteger(0)
16+
cancel = OOInteger(0)
17+
18+
command = commands2.cmd.startEnd(schedule.incrementAndGet, cancel.incrementAndGet)
19+
publish = nt_instance.getBooleanTopic("/SmartDashboard/command/running").publish()
20+
SmartDashboard.putData("command", command)
21+
SmartDashboard.updateValues()
22+
#
23+
commands2.CommandScheduler.getInstance().run()
24+
SmartDashboard.updateValues()
25+
assert not command.isScheduled()
26+
assert schedule == 0
27+
assert cancel == 0
28+
29+
publish.set(True)
30+
SmartDashboard.updateValues()
31+
commands2.CommandScheduler.getInstance().run()
32+
assert command.isScheduled()
33+
assert schedule == 1
34+
assert cancel == 0
35+
36+
def test_trueAndScheduleNoOp(scheduler: commands2.CommandScheduler, nt_instance: NetworkTableInstance):
37+
schedule = OOInteger(0)
38+
cancel = OOInteger(0)
39+
40+
command = commands2.cmd.startEnd(schedule.incrementAndGet, cancel.incrementAndGet)
41+
publish = nt_instance.getBooleanTopic("/SmartDashboard/command/running").publish()
42+
SmartDashboard.putData("command", command)
43+
SmartDashboard.updateValues()
44+
#
45+
command.schedule()
46+
commands2.CommandScheduler.getInstance().run()
47+
SmartDashboard.updateValues()
48+
assert command.isScheduled()
49+
assert schedule == 1
50+
assert cancel == 0
51+
52+
publish.set(True)
53+
SmartDashboard.updateValues()
54+
commands2.CommandScheduler.getInstance().run()
55+
assert command.isScheduled()
56+
assert schedule == 1
57+
assert cancel == 0
58+
59+
def test_falseAndNotScheduledNoOp(scheduler: commands2.CommandScheduler, nt_instance: NetworkTableInstance):
60+
schedule = OOInteger(0)
61+
cancel = OOInteger(0)
62+
63+
command = commands2.cmd.startEnd(schedule.incrementAndGet, cancel.incrementAndGet)
64+
publish = nt_instance.getBooleanTopic("/SmartDashboard/command/running").publish()
65+
SmartDashboard.putData("command", command)
66+
SmartDashboard.updateValues()
67+
#
68+
commands2.CommandScheduler.getInstance().run()
69+
SmartDashboard.updateValues()
70+
assert not command.isScheduled()
71+
assert schedule == 0
72+
assert cancel == 0
73+
74+
publish.set(False)
75+
SmartDashboard.updateValues()
76+
commands2.CommandScheduler.getInstance().run()
77+
assert not command.isScheduled()
78+
assert schedule == 0
79+
assert cancel == 0
80+
81+
def test_falseAndScheduleCancel(scheduler: commands2.CommandScheduler, nt_instance: NetworkTableInstance):
82+
schedule = OOInteger(0)
83+
cancel = OOInteger(0)
84+
85+
command = commands2.cmd.startEnd(schedule.incrementAndGet, cancel.incrementAndGet)
86+
publish = nt_instance.getBooleanTopic("/SmartDashboard/command/running").publish()
87+
SmartDashboard.putData("command", command)
88+
SmartDashboard.updateValues()
89+
#
90+
command.schedule()
91+
commands2.CommandScheduler.getInstance().run()
92+
SmartDashboard.updateValues()
93+
assert command.isScheduled()
94+
assert schedule == 1
95+
assert cancel == 0
96+
97+
publish.set(False)
98+
SmartDashboard.updateValues()
99+
commands2.CommandScheduler.getInstance().run()
100+
assert not command.isScheduled()
101+
assert schedule == 1
102+
assert cancel == 1

tests/test_conditional_command.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,107 @@ def test_conditionalCommandRequirement(scheduler: commands2.CommandScheduler):
5353

5454
assert command1.end.called_with(True)
5555
assert not command2.end.called_with(True)
56+
57+
58+
@pytest.mark.parametrize(
59+
"name,expected,command1,command2,selector",
60+
[
61+
(
62+
"AllCancelSelf",
63+
commands2.InterruptionBehavior.kCancelSelf,
64+
commands2.WaitUntilCommand(lambda: False).withInterruptBehavior(
65+
commands2.InterruptionBehavior.kCancelSelf
66+
),
67+
commands2.WaitUntilCommand(lambda: False).withInterruptBehavior(
68+
commands2.InterruptionBehavior.kCancelSelf
69+
),
70+
lambda: True,
71+
),
72+
(
73+
"AllCancelIncoming",
74+
commands2.InterruptionBehavior.kCancelIncoming,
75+
commands2.WaitUntilCommand(lambda: False).withInterruptBehavior(
76+
commands2.InterruptionBehavior.kCancelIncoming
77+
),
78+
commands2.WaitUntilCommand(lambda: False).withInterruptBehavior(
79+
commands2.InterruptionBehavior.kCancelIncoming
80+
),
81+
lambda: True,
82+
),
83+
(
84+
"OneCancelSelfOneIncoming",
85+
commands2.InterruptionBehavior.kCancelSelf,
86+
commands2.WaitUntilCommand(lambda: False).withInterruptBehavior(
87+
commands2.InterruptionBehavior.kCancelSelf
88+
),
89+
commands2.WaitUntilCommand(lambda: False).withInterruptBehavior(
90+
commands2.InterruptionBehavior.kCancelIncoming
91+
),
92+
lambda: True,
93+
),
94+
(
95+
"OneCancelIncomingOneSelf",
96+
commands2.InterruptionBehavior.kCancelSelf,
97+
commands2.WaitUntilCommand(lambda: False).withInterruptBehavior(
98+
commands2.InterruptionBehavior.kCancelIncoming
99+
),
100+
commands2.WaitUntilCommand(lambda: False).withInterruptBehavior(
101+
commands2.InterruptionBehavior.kCancelSelf
102+
),
103+
lambda: True,
104+
),
105+
],
106+
)
107+
def test_interruptible(
108+
name,
109+
expected,
110+
command1,
111+
command2,
112+
selector,
113+
):
114+
command = commands2.ConditionalCommand(command1, command2, selector)
115+
assert command.getInterruptionBehavior() == expected
116+
117+
118+
@pytest.mark.parametrize(
119+
"name,expected,command1,command2,selector",
120+
[
121+
(
122+
"AllFalse",
123+
False,
124+
commands2.WaitUntilCommand(lambda: False).ignoringDisable(False),
125+
commands2.WaitUntilCommand(lambda: False).ignoringDisable(False),
126+
lambda: True,
127+
),
128+
(
129+
"AllTrue",
130+
True,
131+
commands2.WaitUntilCommand(lambda: False).ignoringDisable(True),
132+
commands2.WaitUntilCommand(lambda: False).ignoringDisable(True),
133+
lambda: True,
134+
),
135+
(
136+
"OneTrueOneFalse",
137+
False,
138+
commands2.WaitUntilCommand(lambda: False).ignoringDisable(True),
139+
commands2.WaitUntilCommand(lambda: False).ignoringDisable(False),
140+
lambda: True,
141+
),
142+
(
143+
"OneFalseOneTrue",
144+
False,
145+
commands2.WaitUntilCommand(lambda: False).ignoringDisable(False),
146+
commands2.WaitUntilCommand(lambda: False).ignoringDisable(True),
147+
lambda: True,
148+
),
149+
],
150+
)
151+
def test_runsWhenDisabled(
152+
name,
153+
expected,
154+
command1,
155+
command2,
156+
selector,
157+
):
158+
command = commands2.ConditionalCommand(command1, command2, selector)
159+
assert command.runsWhenDisabled() == expected

tests/test_deferred_command.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
from typing import TYPE_CHECKING
2+
3+
import commands2
4+
from util import * # type: ignore
5+
6+
if TYPE_CHECKING:
7+
from .util import *
8+
9+
import pytest
10+
11+
#parameterized on true and false
12+
@pytest.mark.parametrize(
13+
"interrupted",
14+
[
15+
True,
16+
False,
17+
],
18+
)
19+
def test_deferredFunctions(interrupted):
20+
inner_command = commands2.Command()
21+
start_spying_on(inner_command)
22+
command = commands2.DeferredCommand(lambda: inner_command)
23+
24+
command.initialize()
25+
verify(inner_command).initialize()
26+
27+
command.execute()
28+
verify(inner_command).execute()
29+
30+
assert not command.isFinished()
31+
verify(inner_command).isFinished()
32+
33+
inner_command.isFinished = lambda: True
34+
assert command.isFinished()
35+
verify(inner_command, times(2)).isFinished()
36+
37+
command.end(interrupted)
38+
verify(inner_command).end(interrupted)
39+
40+
def test_deferredSupplierOnlyCalledDuringInit(scheduler: commands2.CommandScheduler):
41+
class Supplier:
42+
def get(self):
43+
return commands2.cmd.none()
44+
supplier = Supplier()
45+
start_spying_on(supplier)
46+
47+
command = commands2.DeferredCommand(supplier)
48+
verify(supplier, never()).get()
49+
50+
scheduler.schedule(command)
51+
verify(supplier, times(1)).get()
52+
scheduler.run()
53+
54+
scheduler.schedule(command)
55+
verify(supplier, times(2)).get()
56+
57+
def test_deferredRequirements():
58+
subsystem = commands2.Subsystem()
59+
command = commands2.DeferredCommand(commands2.cmd.none(), subsystem)
60+
61+
assert subsystem in command.getRequirements()
62+
63+
def test_deferredNullCommand():
64+
command = commands2.DeferredCommand(lambda: None)
65+
66+
command.initialize()
67+
command.execute()
68+
command.isFinished()
69+
command.end(False)

tests/test_networkbutton.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
def test_networkbutton(
1212
scheduler: commands2.CommandScheduler, nt_instance: NetworkTableInstance
1313
):
14-
# command = commands2.Command()
1514
command = commands2.Command()
1615
start_spying_on(command)
1716

tests/test_perpetualcommand.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
import pytest
1010

1111

12-
def test_perpetualCommandSchedule(scheduler: commands2.CommandScheduler):
13-
command = commands2.PerpetualCommand(commands2.InstantCommand())
12+
# def test_perpetualCommandSchedule(scheduler: commands2.CommandScheduler):
13+
# command = commands2.PerpetualCommand(commands2.InstantCommand())
1414

15-
scheduler.schedule(command)
16-
scheduler.run()
15+
# scheduler.schedule(command)
16+
# scheduler.run()
1717

18-
assert scheduler.isScheduled(command)
18+
# assert scheduler.isScheduled(command)

0 commit comments

Comments
 (0)