[commands] Add property tests for command compositions (#4715)

This commit is contained in:
Starlight220
2022-11-28 02:23:56 +02:00
committed by GitHub
parent e4ac09077c
commit 8958b2a4da
31 changed files with 579 additions and 59 deletions

View File

@@ -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,

View File

@@ -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,

View File

@@ -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,

View File

@@ -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));
}
}

View File

@@ -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

View File

@@ -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

View File

@@ -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;
};

View File

@@ -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>

View File

@@ -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