Skip to content

Commit 1b46350

Browse files
TheTripleVvirtuald
authored andcommitted
port latest test changes
1 parent f48b3fe commit 1b46350

11 files changed

+632
-32
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
@@ -127,14 +127,14 @@ def test_raceWith(scheduler: commands2.CommandScheduler):
127127
assert not group.isScheduled()
128128

129129

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

139139

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

tests/test_command_schedule.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
if TYPE_CHECKING:
77
from .util import *
88

9+
from ntcore import NetworkTableInstance
10+
from wpilib import SmartDashboard
11+
912
import pytest
1013

1114

@@ -88,3 +91,22 @@ def test_notScheduledCancel(scheduler: commands2.CommandScheduler):
8891
command = commands2.Command()
8992

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

tests/test_command_sendable_button.py

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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+
15+
def test_trueAndNotScheduledSchedules(
16+
scheduler: commands2.CommandScheduler, nt_instance: NetworkTableInstance
17+
):
18+
schedule = OOInteger(0)
19+
cancel = OOInteger(0)
20+
21+
command = commands2.cmd.startEnd(schedule.incrementAndGet, cancel.incrementAndGet)
22+
publish = nt_instance.getBooleanTopic("/SmartDashboard/command/running").publish()
23+
SmartDashboard.putData("command", command)
24+
SmartDashboard.updateValues()
25+
#
26+
commands2.CommandScheduler.getInstance().run()
27+
SmartDashboard.updateValues()
28+
assert not command.isScheduled()
29+
assert schedule == 0
30+
assert cancel == 0
31+
32+
publish.set(True)
33+
SmartDashboard.updateValues()
34+
commands2.CommandScheduler.getInstance().run()
35+
assert command.isScheduled()
36+
assert schedule == 1
37+
assert cancel == 0
38+
39+
40+
def test_trueAndScheduleNoOp(
41+
scheduler: commands2.CommandScheduler, nt_instance: NetworkTableInstance
42+
):
43+
schedule = OOInteger(0)
44+
cancel = OOInteger(0)
45+
46+
command = commands2.cmd.startEnd(schedule.incrementAndGet, cancel.incrementAndGet)
47+
publish = nt_instance.getBooleanTopic("/SmartDashboard/command/running").publish()
48+
SmartDashboard.putData("command", command)
49+
SmartDashboard.updateValues()
50+
#
51+
command.schedule()
52+
commands2.CommandScheduler.getInstance().run()
53+
SmartDashboard.updateValues()
54+
assert command.isScheduled()
55+
assert schedule == 1
56+
assert cancel == 0
57+
58+
publish.set(True)
59+
SmartDashboard.updateValues()
60+
commands2.CommandScheduler.getInstance().run()
61+
assert command.isScheduled()
62+
assert schedule == 1
63+
assert cancel == 0
64+
65+
66+
def test_falseAndNotScheduledNoOp(
67+
scheduler: commands2.CommandScheduler, nt_instance: NetworkTableInstance
68+
):
69+
schedule = OOInteger(0)
70+
cancel = OOInteger(0)
71+
72+
command = commands2.cmd.startEnd(schedule.incrementAndGet, cancel.incrementAndGet)
73+
publish = nt_instance.getBooleanTopic("/SmartDashboard/command/running").publish()
74+
SmartDashboard.putData("command", command)
75+
SmartDashboard.updateValues()
76+
#
77+
commands2.CommandScheduler.getInstance().run()
78+
SmartDashboard.updateValues()
79+
assert not command.isScheduled()
80+
assert schedule == 0
81+
assert cancel == 0
82+
83+
publish.set(False)
84+
SmartDashboard.updateValues()
85+
commands2.CommandScheduler.getInstance().run()
86+
assert not command.isScheduled()
87+
assert schedule == 0
88+
assert cancel == 0
89+
90+
91+
def test_falseAndScheduleCancel(
92+
scheduler: commands2.CommandScheduler, nt_instance: NetworkTableInstance
93+
):
94+
schedule = OOInteger(0)
95+
cancel = OOInteger(0)
96+
97+
command = commands2.cmd.startEnd(schedule.incrementAndGet, cancel.incrementAndGet)
98+
publish = nt_instance.getBooleanTopic("/SmartDashboard/command/running").publish()
99+
SmartDashboard.putData("command", command)
100+
SmartDashboard.updateValues()
101+
#
102+
command.schedule()
103+
commands2.CommandScheduler.getInstance().run()
104+
SmartDashboard.updateValues()
105+
assert command.isScheduled()
106+
assert schedule == 1
107+
assert cancel == 0
108+
109+
publish.set(False)
110+
SmartDashboard.updateValues()
111+
commands2.CommandScheduler.getInstance().run()
112+
assert not command.isScheduled()
113+
assert schedule == 1
114+
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: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
12+
# parameterized on true and false
13+
@pytest.mark.parametrize(
14+
"interrupted",
15+
[
16+
True,
17+
False,
18+
],
19+
)
20+
def test_deferredFunctions(interrupted):
21+
inner_command = commands2.Command()
22+
start_spying_on(inner_command)
23+
command = commands2.DeferredCommand(lambda: inner_command)
24+
25+
command.initialize()
26+
verify(inner_command).initialize()
27+
28+
command.execute()
29+
verify(inner_command).execute()
30+
31+
assert not command.isFinished()
32+
verify(inner_command).isFinished()
33+
34+
inner_command.isFinished = lambda: True
35+
assert command.isFinished()
36+
verify(inner_command, times(2)).isFinished()
37+
38+
command.end(interrupted)
39+
verify(inner_command).end(interrupted)
40+
41+
42+
def test_deferredSupplierOnlyCalledDuringInit(scheduler: commands2.CommandScheduler):
43+
class Supplier:
44+
def get(self):
45+
return commands2.cmd.none()
46+
47+
supplier = Supplier()
48+
start_spying_on(supplier)
49+
50+
command = commands2.DeferredCommand(supplier)
51+
verify(supplier, never()).get()
52+
53+
scheduler.schedule(command)
54+
verify(supplier, times(1)).get()
55+
scheduler.run()
56+
57+
scheduler.schedule(command)
58+
verify(supplier, times(2)).get()
59+
60+
61+
def test_deferredRequirements():
62+
subsystem = commands2.Subsystem()
63+
command = commands2.DeferredCommand(commands2.cmd.none(), subsystem)
64+
65+
assert subsystem in command.getRequirements()
66+
67+
68+
def test_deferredNullCommand():
69+
command = commands2.DeferredCommand(lambda: None)
70+
71+
command.initialize()
72+
command.execute()
73+
command.isFinished()
74+
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)