mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-04 03:11:43 +00:00
[commands] Add property tests for command compositions (#4715)
This commit is contained in:
@@ -19,6 +19,7 @@ public class ParallelCommandGroup extends CommandGroupBase {
|
||||
// maps commands in this group to whether they are still running
|
||||
private final Map<Command, Boolean> m_commands = new HashMap<>();
|
||||
private boolean m_runWhenDisabled = true;
|
||||
private InterruptionBehavior m_interruptBehavior = InterruptionBehavior.kCancelIncoming;
|
||||
|
||||
/**
|
||||
* Creates a new ParallelCommandGroup. The given commands will be executed simultaneously. The
|
||||
@@ -50,6 +51,9 @@ public class ParallelCommandGroup extends CommandGroupBase {
|
||||
m_commands.put(command, false);
|
||||
m_requirements.addAll(command.getRequirements());
|
||||
m_runWhenDisabled &= command.runsWhenDisabled();
|
||||
if (command.getInterruptionBehavior() == InterruptionBehavior.kCancelSelf) {
|
||||
m_interruptBehavior = InterruptionBehavior.kCancelSelf;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,4 +99,9 @@ public class ParallelCommandGroup extends CommandGroupBase {
|
||||
public boolean runsWhenDisabled() {
|
||||
return m_runWhenDisabled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public InterruptionBehavior getInterruptionBehavior() {
|
||||
return m_interruptBehavior;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ public class ParallelDeadlineGroup extends CommandGroupBase {
|
||||
private boolean m_runWhenDisabled = true;
|
||||
private boolean m_finished = true;
|
||||
private Command m_deadline;
|
||||
private InterruptionBehavior m_interruptBehavior = InterruptionBehavior.kCancelIncoming;
|
||||
|
||||
/**
|
||||
* Creates a new ParallelDeadlineGroup. The given commands (including the deadline) will be
|
||||
@@ -72,6 +73,9 @@ public class ParallelDeadlineGroup extends CommandGroupBase {
|
||||
m_commands.put(command, false);
|
||||
m_requirements.addAll(command.getRequirements());
|
||||
m_runWhenDisabled &= command.runsWhenDisabled();
|
||||
if (command.getInterruptionBehavior() == InterruptionBehavior.kCancelSelf) {
|
||||
m_interruptBehavior = InterruptionBehavior.kCancelSelf;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -119,4 +123,9 @@ public class ParallelDeadlineGroup extends CommandGroupBase {
|
||||
public boolean runsWhenDisabled() {
|
||||
return m_runWhenDisabled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public InterruptionBehavior getInterruptionBehavior() {
|
||||
return m_interruptBehavior;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ public class ParallelRaceGroup extends CommandGroupBase {
|
||||
private final Set<Command> m_commands = new HashSet<>();
|
||||
private boolean m_runWhenDisabled = true;
|
||||
private boolean m_finished = true;
|
||||
private InterruptionBehavior m_interruptBehavior = InterruptionBehavior.kCancelIncoming;
|
||||
|
||||
/**
|
||||
* Creates a new ParallelCommandRace. The given commands will be executed simultaneously, and will
|
||||
@@ -51,6 +52,9 @@ public class ParallelRaceGroup extends CommandGroupBase {
|
||||
m_commands.add(command);
|
||||
m_requirements.addAll(command.getRequirements());
|
||||
m_runWhenDisabled &= command.runsWhenDisabled();
|
||||
if (command.getInterruptionBehavior() == InterruptionBehavior.kCancelSelf) {
|
||||
m_interruptBehavior = InterruptionBehavior.kCancelSelf;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,4 +92,9 @@ public class ParallelRaceGroup extends CommandGroupBase {
|
||||
public boolean runsWhenDisabled() {
|
||||
return m_runWhenDisabled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public InterruptionBehavior getInterruptionBehavior() {
|
||||
return m_interruptBehavior;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,6 +31,8 @@ public class SelectCommand extends CommandBase {
|
||||
private final Supplier<Object> m_selector;
|
||||
private final Supplier<Command> m_toRun;
|
||||
private Command m_selectedCommand;
|
||||
private boolean m_runsWhenDisabled = true;
|
||||
private InterruptionBehavior m_interruptBehavior = InterruptionBehavior.kCancelIncoming;
|
||||
|
||||
/**
|
||||
* Creates a new selectcommand.
|
||||
@@ -50,6 +52,10 @@ public class SelectCommand extends CommandBase {
|
||||
|
||||
for (Command command : m_commands.values()) {
|
||||
m_requirements.addAll(command.getRequirements());
|
||||
m_runsWhenDisabled &= command.runsWhenDisabled();
|
||||
if (command.getInterruptionBehavior() == InterruptionBehavior.kCancelSelf) {
|
||||
m_interruptBehavior = InterruptionBehavior.kCancelSelf;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,6 +68,10 @@ public class SelectCommand extends CommandBase {
|
||||
m_commands = null;
|
||||
m_selector = null;
|
||||
m_toRun = requireNonNullParam(toRun, "toRun", "SelectCommand");
|
||||
|
||||
// we have no way of checking the underlying command, so default.
|
||||
m_runsWhenDisabled = false;
|
||||
m_interruptBehavior = InterruptionBehavior.kCancelSelf;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -97,14 +107,11 @@ public class SelectCommand extends CommandBase {
|
||||
|
||||
@Override
|
||||
public boolean runsWhenDisabled() {
|
||||
if (m_commands != null) {
|
||||
boolean runsWhenDisabled = true;
|
||||
for (Command command : m_commands.values()) {
|
||||
runsWhenDisabled &= command.runsWhenDisabled();
|
||||
}
|
||||
return runsWhenDisabled;
|
||||
} else {
|
||||
return m_toRun.get().runsWhenDisabled();
|
||||
}
|
||||
return m_runsWhenDisabled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public InterruptionBehavior getInterruptionBehavior() {
|
||||
return m_interruptBehavior;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ public class SequentialCommandGroup extends CommandGroupBase {
|
||||
private final List<Command> m_commands = new ArrayList<>();
|
||||
private int m_currentCommandIndex = -1;
|
||||
private boolean m_runWhenDisabled = true;
|
||||
private InterruptionBehavior m_interruptBehavior = InterruptionBehavior.kCancelIncoming;
|
||||
|
||||
/**
|
||||
* Creates a new SequentialCommandGroup. The given commands will be run sequentially, with the
|
||||
@@ -44,6 +45,9 @@ public class SequentialCommandGroup extends CommandGroupBase {
|
||||
m_commands.add(command);
|
||||
m_requirements.addAll(command.getRequirements());
|
||||
m_runWhenDisabled &= command.runsWhenDisabled();
|
||||
if (command.getInterruptionBehavior() == InterruptionBehavior.kCancelSelf) {
|
||||
m_interruptBehavior = InterruptionBehavior.kCancelSelf;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,4 +98,9 @@ public class SequentialCommandGroup extends CommandGroupBase {
|
||||
public boolean runsWhenDisabled() {
|
||||
return m_runWhenDisabled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public InterruptionBehavior getInterruptionBehavior() {
|
||||
return m_interruptBehavior;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,6 +56,11 @@ bool ParallelCommandGroup::RunsWhenDisabled() const {
|
||||
return m_runWhenDisabled;
|
||||
}
|
||||
|
||||
Command::InterruptionBehavior ParallelCommandGroup::GetInterruptionBehavior()
|
||||
const {
|
||||
return m_interruptBehavior;
|
||||
}
|
||||
|
||||
void ParallelCommandGroup::AddCommands(
|
||||
std::vector<std::unique_ptr<Command>>&& commands) {
|
||||
for (auto&& command : commands) {
|
||||
@@ -75,6 +80,10 @@ void ParallelCommandGroup::AddCommands(
|
||||
command->SetGrouped(true);
|
||||
AddRequirements(command->GetRequirements());
|
||||
m_runWhenDisabled &= command->RunsWhenDisabled();
|
||||
if (command->GetInterruptionBehavior() ==
|
||||
Command::InterruptionBehavior::kCancelSelf) {
|
||||
m_interruptBehavior = Command::InterruptionBehavior::kCancelSelf;
|
||||
}
|
||||
m_commands.emplace_back(std::move(command), false);
|
||||
} else {
|
||||
throw FRC_MakeError(frc::err::CommandIllegalUse,
|
||||
|
||||
@@ -53,6 +53,11 @@ bool ParallelDeadlineGroup::RunsWhenDisabled() const {
|
||||
return m_runWhenDisabled;
|
||||
}
|
||||
|
||||
Command::InterruptionBehavior ParallelDeadlineGroup::GetInterruptionBehavior()
|
||||
const {
|
||||
return m_interruptBehavior;
|
||||
}
|
||||
|
||||
void ParallelDeadlineGroup::AddCommands(
|
||||
std::vector<std::unique_ptr<Command>>&& commands) {
|
||||
if (!RequireUngrouped(commands)) {
|
||||
@@ -70,6 +75,10 @@ void ParallelDeadlineGroup::AddCommands(
|
||||
command->SetGrouped(true);
|
||||
AddRequirements(command->GetRequirements());
|
||||
m_runWhenDisabled &= command->RunsWhenDisabled();
|
||||
if (command->GetInterruptionBehavior() ==
|
||||
Command::InterruptionBehavior::kCancelSelf) {
|
||||
m_interruptBehavior = Command::InterruptionBehavior::kCancelSelf;
|
||||
}
|
||||
m_commands.emplace_back(std::move(command), false);
|
||||
} else {
|
||||
throw FRC_MakeError(frc::err::CommandIllegalUse,
|
||||
|
||||
@@ -43,6 +43,11 @@ bool ParallelRaceGroup::RunsWhenDisabled() const {
|
||||
return m_runWhenDisabled;
|
||||
}
|
||||
|
||||
Command::InterruptionBehavior ParallelRaceGroup::GetInterruptionBehavior()
|
||||
const {
|
||||
return m_interruptBehavior;
|
||||
}
|
||||
|
||||
void ParallelRaceGroup::AddCommands(
|
||||
std::vector<std::unique_ptr<Command>>&& commands) {
|
||||
if (!RequireUngrouped(commands)) {
|
||||
@@ -60,6 +65,10 @@ void ParallelRaceGroup::AddCommands(
|
||||
command->SetGrouped(true);
|
||||
AddRequirements(command->GetRequirements());
|
||||
m_runWhenDisabled &= command->RunsWhenDisabled();
|
||||
if (command->GetInterruptionBehavior() ==
|
||||
Command::InterruptionBehavior::kCancelSelf) {
|
||||
m_interruptBehavior = Command::InterruptionBehavior::kCancelSelf;
|
||||
}
|
||||
m_commands.emplace_back(std::move(command));
|
||||
} else {
|
||||
throw FRC_MakeError(frc::err::CommandIllegalUse,
|
||||
|
||||
@@ -53,6 +53,11 @@ bool SequentialCommandGroup::RunsWhenDisabled() const {
|
||||
return m_runWhenDisabled;
|
||||
}
|
||||
|
||||
Command::InterruptionBehavior SequentialCommandGroup::GetInterruptionBehavior()
|
||||
const {
|
||||
return m_interruptBehavior;
|
||||
}
|
||||
|
||||
void SequentialCommandGroup::AddCommands(
|
||||
std::vector<std::unique_ptr<Command>>&& commands) {
|
||||
if (!RequireUngrouped(commands)) {
|
||||
@@ -69,6 +74,10 @@ void SequentialCommandGroup::AddCommands(
|
||||
command->SetGrouped(true);
|
||||
AddRequirements(command->GetRequirements());
|
||||
m_runWhenDisabled &= command->RunsWhenDisabled();
|
||||
if (command->GetInterruptionBehavior() ==
|
||||
Command::InterruptionBehavior::kCancelSelf) {
|
||||
m_interruptBehavior = Command::InterruptionBehavior::kCancelSelf;
|
||||
}
|
||||
m_commands.emplace_back(std::move(command));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,11 +84,15 @@ class ParallelCommandGroup
|
||||
|
||||
bool RunsWhenDisabled() const override;
|
||||
|
||||
Command::InterruptionBehavior GetInterruptionBehavior() const override;
|
||||
|
||||
private:
|
||||
void AddCommands(std::vector<std::unique_ptr<Command>>&& commands) final;
|
||||
|
||||
std::vector<std::pair<std::unique_ptr<Command>, bool>> m_commands;
|
||||
bool m_runWhenDisabled{true};
|
||||
Command::InterruptionBehavior m_interruptBehavior{
|
||||
Command::InterruptionBehavior::kCancelIncoming};
|
||||
bool isRunning = false;
|
||||
};
|
||||
} // namespace frc2
|
||||
|
||||
@@ -92,6 +92,8 @@ class ParallelDeadlineGroup
|
||||
|
||||
bool RunsWhenDisabled() const override;
|
||||
|
||||
Command::InterruptionBehavior GetInterruptionBehavior() const override;
|
||||
|
||||
private:
|
||||
void AddCommands(std::vector<std::unique_ptr<Command>>&& commands) final;
|
||||
|
||||
@@ -100,6 +102,8 @@ class ParallelDeadlineGroup
|
||||
std::vector<std::pair<std::unique_ptr<Command>, bool>> m_commands;
|
||||
Command* m_deadline;
|
||||
bool m_runWhenDisabled{true};
|
||||
Command::InterruptionBehavior m_interruptBehavior{
|
||||
Command::InterruptionBehavior::kCancelIncoming};
|
||||
bool m_finished{true};
|
||||
};
|
||||
} // namespace frc2
|
||||
|
||||
@@ -72,11 +72,15 @@ class ParallelRaceGroup
|
||||
|
||||
bool RunsWhenDisabled() const override;
|
||||
|
||||
Command::InterruptionBehavior GetInterruptionBehavior() const override;
|
||||
|
||||
private:
|
||||
void AddCommands(std::vector<std::unique_ptr<Command>>&& commands) final;
|
||||
|
||||
std::vector<std::unique_ptr<Command>> m_commands;
|
||||
bool m_runWhenDisabled{true};
|
||||
Command::InterruptionBehavior m_interruptBehavior{
|
||||
Command::InterruptionBehavior::kCancelIncoming};
|
||||
bool m_finished{false};
|
||||
bool isRunning = false;
|
||||
};
|
||||
|
||||
@@ -69,6 +69,10 @@ class SelectCommand : public CommandHelper<CommandBase, SelectCommand<Key>> {
|
||||
for (auto&& command : foo) {
|
||||
this->AddRequirements(command.second->GetRequirements());
|
||||
m_runsWhenDisabled &= command.second->RunsWhenDisabled();
|
||||
if (command.second->GetInterruptionBehavior() ==
|
||||
Command::InterruptionBehavior::kCancelSelf) {
|
||||
m_interruptBehavior = Command::InterruptionBehavior::kCancelSelf;
|
||||
}
|
||||
m_commands.emplace(std::move(command.first), std::move(command.second));
|
||||
}
|
||||
}
|
||||
@@ -86,6 +90,10 @@ class SelectCommand : public CommandHelper<CommandBase, SelectCommand<Key>> {
|
||||
for (auto&& command : commands) {
|
||||
this->AddRequirements(command.second->GetRequirements());
|
||||
m_runsWhenDisabled &= command.second->RunsWhenDisabled();
|
||||
if (command.second->GetInterruptionBehavior() ==
|
||||
Command::InterruptionBehavior::kCancelSelf) {
|
||||
m_interruptBehavior = Command::InterruptionBehavior::kCancelSelf;
|
||||
}
|
||||
m_commands.emplace(std::move(command.first), std::move(command.second));
|
||||
}
|
||||
}
|
||||
@@ -118,6 +126,10 @@ class SelectCommand : public CommandHelper<CommandBase, SelectCommand<Key>> {
|
||||
|
||||
bool RunsWhenDisabled() const override { return m_runsWhenDisabled; }
|
||||
|
||||
Command::InterruptionBehavior GetInterruptionBehavior() const override {
|
||||
return m_interruptBehavior;
|
||||
}
|
||||
|
||||
protected:
|
||||
std::unique_ptr<Command> TransferOwnership() && override {
|
||||
return std::make_unique<SelectCommand>(std::move(*this));
|
||||
@@ -129,6 +141,8 @@ class SelectCommand : public CommandHelper<CommandBase, SelectCommand<Key>> {
|
||||
std::function<Command*()> m_toRun;
|
||||
Command* m_selectedCommand;
|
||||
bool m_runsWhenDisabled = true;
|
||||
Command::InterruptionBehavior m_interruptBehavior{
|
||||
Command::InterruptionBehavior::kCancelIncoming};
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
|
||||
@@ -87,12 +87,16 @@ class SequentialCommandGroup
|
||||
|
||||
bool RunsWhenDisabled() const override;
|
||||
|
||||
Command::InterruptionBehavior GetInterruptionBehavior() const override;
|
||||
|
||||
private:
|
||||
void AddCommands(std::vector<std::unique_ptr<Command>>&& commands) final;
|
||||
|
||||
wpi::SmallVector<std::unique_ptr<Command>, 4> m_commands;
|
||||
size_t m_currentCommandIndex{invalid_index};
|
||||
bool m_runWhenDisabled{true};
|
||||
Command::InterruptionBehavior m_interruptBehavior{
|
||||
Command::InterruptionBehavior::kCancelIncoming};
|
||||
};
|
||||
} // namespace frc2
|
||||
|
||||
|
||||
Reference in New Issue
Block a user