Skip to content

Commit

Permalink
port latest test changes
Browse files Browse the repository at this point in the history
  • Loading branch information
TheTripleV authored and virtuald committed Nov 5, 2023
1 parent f48b3fe commit 1b46350
Show file tree
Hide file tree
Showing 11 changed files with 632 additions and 32 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
description="WPILib command framework v2",
url="https://github.com/robotpy/robotpy-commands-v2",
packages=["commands2"],
install_requires=["wpilib<2025,>=2024.0.0b2", "typing_extensions>=4.1.0,<5"],
license="BSD-3-Clause",
install_requires=["wpilib<2025,>=2024.0.0b2", "typing_extensions>=4.1.0,<5"],
python_requires=">=3.8",
include_package_data=True,
)
16 changes: 8 additions & 8 deletions tests/test_command_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,14 @@ def test_raceWith(scheduler: commands2.CommandScheduler):
assert not group.isScheduled()


def test_perpetually(scheduler: commands2.CommandScheduler):
command = commands2.InstantCommand()
perpetual = command.perpetually()
scheduler.schedule(perpetual)
scheduler.run()
scheduler.run()
scheduler.run()
assert perpetual.isScheduled()
# def test_perpetually(scheduler: commands2.CommandScheduler):
# command = commands2.InstantCommand()
# perpetual = command.perpetually()
# scheduler.schedule(perpetual)
# scheduler.run()
# scheduler.run()
# scheduler.run()
# assert perpetual.isScheduled()


@pytest.mark.skipif(IS_OLD_COMMANDS, reason="not in old commands")
Expand Down
22 changes: 22 additions & 0 deletions tests/test_command_schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
if TYPE_CHECKING:
from .util import *

from ntcore import NetworkTableInstance
from wpilib import SmartDashboard

import pytest


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

scheduler.cancel(command)


def test_smartDashboardCancelTest(
scheduler: commands2.CommandScheduler, nt_instance: NetworkTableInstance
):
SmartDashboard.putData("Scheduler", scheduler)
SmartDashboard.updateValues()

command = commands2.Command()
scheduler.schedule(command)
scheduler.run()
SmartDashboard.updateValues()
assert scheduler.isScheduled(command)

table = nt_instance.getTable("SmartDashboard")
table.getEntry("Scheduler/Cancel").setIntegerArray([id(command)])
SmartDashboard.updateValues()
scheduler.run()
assert not scheduler.isScheduled(command)
114 changes: 114 additions & 0 deletions tests/test_command_sendable_button.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
from typing import TYPE_CHECKING

import commands2
from util import * # type: ignore

if TYPE_CHECKING:
from .util import *

from ntcore import NetworkTableInstance
from wpilib import SmartDashboard

import pytest


def test_trueAndNotScheduledSchedules(
scheduler: commands2.CommandScheduler, nt_instance: NetworkTableInstance
):
schedule = OOInteger(0)
cancel = OOInteger(0)

command = commands2.cmd.startEnd(schedule.incrementAndGet, cancel.incrementAndGet)
publish = nt_instance.getBooleanTopic("/SmartDashboard/command/running").publish()
SmartDashboard.putData("command", command)
SmartDashboard.updateValues()
#
commands2.CommandScheduler.getInstance().run()
SmartDashboard.updateValues()
assert not command.isScheduled()
assert schedule == 0
assert cancel == 0

publish.set(True)
SmartDashboard.updateValues()
commands2.CommandScheduler.getInstance().run()
assert command.isScheduled()
assert schedule == 1
assert cancel == 0


def test_trueAndScheduleNoOp(
scheduler: commands2.CommandScheduler, nt_instance: NetworkTableInstance
):
schedule = OOInteger(0)
cancel = OOInteger(0)

command = commands2.cmd.startEnd(schedule.incrementAndGet, cancel.incrementAndGet)
publish = nt_instance.getBooleanTopic("/SmartDashboard/command/running").publish()
SmartDashboard.putData("command", command)
SmartDashboard.updateValues()
#
command.schedule()
commands2.CommandScheduler.getInstance().run()
SmartDashboard.updateValues()
assert command.isScheduled()
assert schedule == 1
assert cancel == 0

publish.set(True)
SmartDashboard.updateValues()
commands2.CommandScheduler.getInstance().run()
assert command.isScheduled()
assert schedule == 1
assert cancel == 0


def test_falseAndNotScheduledNoOp(
scheduler: commands2.CommandScheduler, nt_instance: NetworkTableInstance
):
schedule = OOInteger(0)
cancel = OOInteger(0)

command = commands2.cmd.startEnd(schedule.incrementAndGet, cancel.incrementAndGet)
publish = nt_instance.getBooleanTopic("/SmartDashboard/command/running").publish()
SmartDashboard.putData("command", command)
SmartDashboard.updateValues()
#
commands2.CommandScheduler.getInstance().run()
SmartDashboard.updateValues()
assert not command.isScheduled()
assert schedule == 0
assert cancel == 0

publish.set(False)
SmartDashboard.updateValues()
commands2.CommandScheduler.getInstance().run()
assert not command.isScheduled()
assert schedule == 0
assert cancel == 0


def test_falseAndScheduleCancel(
scheduler: commands2.CommandScheduler, nt_instance: NetworkTableInstance
):
schedule = OOInteger(0)
cancel = OOInteger(0)

command = commands2.cmd.startEnd(schedule.incrementAndGet, cancel.incrementAndGet)
publish = nt_instance.getBooleanTopic("/SmartDashboard/command/running").publish()
SmartDashboard.putData("command", command)
SmartDashboard.updateValues()
#
command.schedule()
commands2.CommandScheduler.getInstance().run()
SmartDashboard.updateValues()
assert command.isScheduled()
assert schedule == 1
assert cancel == 0

publish.set(False)
SmartDashboard.updateValues()
commands2.CommandScheduler.getInstance().run()
assert not command.isScheduled()
assert schedule == 1
assert cancel == 1
104 changes: 104 additions & 0 deletions tests/test_conditional_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,107 @@ def test_conditionalCommandRequirement(scheduler: commands2.CommandScheduler):

assert command1.end.called_with(True)
assert not command2.end.called_with(True)


@pytest.mark.parametrize(
"name,expected,command1,command2,selector",
[
(
"AllCancelSelf",
commands2.InterruptionBehavior.kCancelSelf,
commands2.WaitUntilCommand(lambda: False).withInterruptBehavior(
commands2.InterruptionBehavior.kCancelSelf
),
commands2.WaitUntilCommand(lambda: False).withInterruptBehavior(
commands2.InterruptionBehavior.kCancelSelf
),
lambda: True,
),
(
"AllCancelIncoming",
commands2.InterruptionBehavior.kCancelIncoming,
commands2.WaitUntilCommand(lambda: False).withInterruptBehavior(
commands2.InterruptionBehavior.kCancelIncoming
),
commands2.WaitUntilCommand(lambda: False).withInterruptBehavior(
commands2.InterruptionBehavior.kCancelIncoming
),
lambda: True,
),
(
"OneCancelSelfOneIncoming",
commands2.InterruptionBehavior.kCancelSelf,
commands2.WaitUntilCommand(lambda: False).withInterruptBehavior(
commands2.InterruptionBehavior.kCancelSelf
),
commands2.WaitUntilCommand(lambda: False).withInterruptBehavior(
commands2.InterruptionBehavior.kCancelIncoming
),
lambda: True,
),
(
"OneCancelIncomingOneSelf",
commands2.InterruptionBehavior.kCancelSelf,
commands2.WaitUntilCommand(lambda: False).withInterruptBehavior(
commands2.InterruptionBehavior.kCancelIncoming
),
commands2.WaitUntilCommand(lambda: False).withInterruptBehavior(
commands2.InterruptionBehavior.kCancelSelf
),
lambda: True,
),
],
)
def test_interruptible(
name,
expected,
command1,
command2,
selector,
):
command = commands2.ConditionalCommand(command1, command2, selector)
assert command.getInterruptionBehavior() == expected


@pytest.mark.parametrize(
"name,expected,command1,command2,selector",
[
(
"AllFalse",
False,
commands2.WaitUntilCommand(lambda: False).ignoringDisable(False),
commands2.WaitUntilCommand(lambda: False).ignoringDisable(False),
lambda: True,
),
(
"AllTrue",
True,
commands2.WaitUntilCommand(lambda: False).ignoringDisable(True),
commands2.WaitUntilCommand(lambda: False).ignoringDisable(True),
lambda: True,
),
(
"OneTrueOneFalse",
False,
commands2.WaitUntilCommand(lambda: False).ignoringDisable(True),
commands2.WaitUntilCommand(lambda: False).ignoringDisable(False),
lambda: True,
),
(
"OneFalseOneTrue",
False,
commands2.WaitUntilCommand(lambda: False).ignoringDisable(False),
commands2.WaitUntilCommand(lambda: False).ignoringDisable(True),
lambda: True,
),
],
)
def test_runsWhenDisabled(
name,
expected,
command1,
command2,
selector,
):
command = commands2.ConditionalCommand(command1, command2, selector)
assert command.runsWhenDisabled() == expected
74 changes: 74 additions & 0 deletions tests/test_deferred_command.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
from typing import TYPE_CHECKING

import commands2
from util import * # type: ignore

if TYPE_CHECKING:
from .util import *

import pytest


# parameterized on true and false
@pytest.mark.parametrize(
"interrupted",
[
True,
False,
],
)
def test_deferredFunctions(interrupted):
inner_command = commands2.Command()
start_spying_on(inner_command)
command = commands2.DeferredCommand(lambda: inner_command)

command.initialize()
verify(inner_command).initialize()

command.execute()
verify(inner_command).execute()

assert not command.isFinished()
verify(inner_command).isFinished()

inner_command.isFinished = lambda: True
assert command.isFinished()
verify(inner_command, times(2)).isFinished()

command.end(interrupted)
verify(inner_command).end(interrupted)


def test_deferredSupplierOnlyCalledDuringInit(scheduler: commands2.CommandScheduler):
class Supplier:
def get(self):
return commands2.cmd.none()

supplier = Supplier()
start_spying_on(supplier)

command = commands2.DeferredCommand(supplier)
verify(supplier, never()).get()

scheduler.schedule(command)
verify(supplier, times(1)).get()
scheduler.run()

scheduler.schedule(command)
verify(supplier, times(2)).get()


def test_deferredRequirements():
subsystem = commands2.Subsystem()
command = commands2.DeferredCommand(commands2.cmd.none(), subsystem)

assert subsystem in command.getRequirements()


def test_deferredNullCommand():
command = commands2.DeferredCommand(lambda: None)

command.initialize()
command.execute()
command.isFinished()
command.end(False)
1 change: 0 additions & 1 deletion tests/test_networkbutton.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
def test_networkbutton(
scheduler: commands2.CommandScheduler, nt_instance: NetworkTableInstance
):
# command = commands2.Command()
command = commands2.Command()
start_spying_on(command)

Expand Down
10 changes: 5 additions & 5 deletions tests/test_perpetualcommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
import pytest


def test_perpetualCommandSchedule(scheduler: commands2.CommandScheduler):
command = commands2.PerpetualCommand(commands2.InstantCommand())
# def test_perpetualCommandSchedule(scheduler: commands2.CommandScheduler):
# command = commands2.PerpetualCommand(commands2.InstantCommand())

scheduler.schedule(command)
scheduler.run()
# scheduler.schedule(command)
# scheduler.run()

assert scheduler.isScheduled(command)
# assert scheduler.isScheduled(command)
Loading

0 comments on commit 1b46350

Please sign in to comment.