mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
[commands] C++ unique_ptr migration (#4319)
Add a CommandPtr with an internal unique_ptr to enable not needing to move the underlying classes, which is error-prone due to the potential for lambda captures.
This commit is contained in:
@@ -23,15 +23,15 @@ TEST_F(CommandDecoratorTest, WithTimeout) {
|
||||
|
||||
auto command = RunCommand([] {}, {}).WithTimeout(100_ms);
|
||||
|
||||
scheduler.Schedule(&command);
|
||||
scheduler.Schedule(command);
|
||||
|
||||
scheduler.Run();
|
||||
EXPECT_TRUE(scheduler.IsScheduled(&command));
|
||||
EXPECT_TRUE(scheduler.IsScheduled(command));
|
||||
|
||||
frc::sim::StepTiming(150_ms);
|
||||
|
||||
scheduler.Run();
|
||||
EXPECT_FALSE(scheduler.IsScheduled(&command));
|
||||
EXPECT_FALSE(scheduler.IsScheduled(command));
|
||||
|
||||
frc::sim::ResumeTiming();
|
||||
}
|
||||
@@ -43,15 +43,15 @@ TEST_F(CommandDecoratorTest, Until) {
|
||||
|
||||
auto command = RunCommand([] {}, {}).Until([&finished] { return finished; });
|
||||
|
||||
scheduler.Schedule(&command);
|
||||
scheduler.Schedule(command);
|
||||
|
||||
scheduler.Run();
|
||||
EXPECT_TRUE(scheduler.IsScheduled(&command));
|
||||
EXPECT_TRUE(scheduler.IsScheduled(command));
|
||||
|
||||
finished = true;
|
||||
|
||||
scheduler.Run();
|
||||
EXPECT_FALSE(scheduler.IsScheduled(&command));
|
||||
EXPECT_FALSE(scheduler.IsScheduled(command));
|
||||
}
|
||||
|
||||
TEST_F(CommandDecoratorTest, IgnoringDisable) {
|
||||
@@ -61,10 +61,10 @@ TEST_F(CommandDecoratorTest, IgnoringDisable) {
|
||||
|
||||
SetDSEnabled(false);
|
||||
|
||||
scheduler.Schedule(command.get());
|
||||
scheduler.Schedule(command);
|
||||
|
||||
scheduler.Run();
|
||||
EXPECT_TRUE(scheduler.IsScheduled(command.get()));
|
||||
EXPECT_TRUE(scheduler.IsScheduled(command));
|
||||
}
|
||||
|
||||
TEST_F(CommandDecoratorTest, BeforeStarting) {
|
||||
@@ -75,14 +75,14 @@ TEST_F(CommandDecoratorTest, BeforeStarting) {
|
||||
auto command = InstantCommand([] {}, {}).BeforeStarting(
|
||||
[&finished] { finished = true; });
|
||||
|
||||
scheduler.Schedule(&command);
|
||||
scheduler.Schedule(command);
|
||||
|
||||
EXPECT_TRUE(finished);
|
||||
|
||||
scheduler.Run();
|
||||
scheduler.Run();
|
||||
|
||||
EXPECT_FALSE(scheduler.IsScheduled(&command));
|
||||
EXPECT_FALSE(scheduler.IsScheduled(command));
|
||||
}
|
||||
|
||||
TEST_F(CommandDecoratorTest, AndThen) {
|
||||
@@ -93,14 +93,14 @@ TEST_F(CommandDecoratorTest, AndThen) {
|
||||
auto command =
|
||||
InstantCommand([] {}, {}).AndThen([&finished] { finished = true; });
|
||||
|
||||
scheduler.Schedule(&command);
|
||||
scheduler.Schedule(command);
|
||||
|
||||
EXPECT_FALSE(finished);
|
||||
|
||||
scheduler.Run();
|
||||
scheduler.Run();
|
||||
|
||||
EXPECT_FALSE(scheduler.IsScheduled(&command));
|
||||
EXPECT_FALSE(scheduler.IsScheduled(command));
|
||||
EXPECT_TRUE(finished);
|
||||
}
|
||||
|
||||
@@ -124,12 +124,12 @@ TEST_F(CommandDecoratorTest, Endlessly) {
|
||||
|
||||
auto command = InstantCommand([] {}, {}).Endlessly();
|
||||
|
||||
scheduler.Schedule(&command);
|
||||
scheduler.Schedule(command);
|
||||
|
||||
scheduler.Run();
|
||||
scheduler.Run();
|
||||
|
||||
EXPECT_TRUE(scheduler.IsScheduled(&command));
|
||||
EXPECT_TRUE(scheduler.IsScheduled(command));
|
||||
}
|
||||
|
||||
TEST_F(CommandDecoratorTest, Unless) {
|
||||
@@ -143,12 +143,12 @@ TEST_F(CommandDecoratorTest, Unless) {
|
||||
return unlessBool;
|
||||
});
|
||||
|
||||
scheduler.Schedule(&command);
|
||||
scheduler.Schedule(command);
|
||||
scheduler.Run();
|
||||
EXPECT_FALSE(hasRun);
|
||||
|
||||
unlessBool = false;
|
||||
scheduler.Schedule(&command);
|
||||
scheduler.Schedule(command);
|
||||
scheduler.Run();
|
||||
EXPECT_TRUE(hasRun);
|
||||
}
|
||||
|
||||
@@ -54,7 +54,7 @@ TEST_F(CommandRequirementsTest, RequirementUninterruptible) {
|
||||
int exeCounter = 0;
|
||||
int endCounter = 0;
|
||||
|
||||
std::unique_ptr<Command> command1 =
|
||||
CommandPtr command1 =
|
||||
FunctionalCommand([&initCounter] { initCounter++; },
|
||||
[&exeCounter] { exeCounter++; },
|
||||
[&endCounter](bool interruptible) { endCounter++; },
|
||||
@@ -68,13 +68,13 @@ TEST_F(CommandRequirementsTest, RequirementUninterruptible) {
|
||||
EXPECT_CALL(command2, End(true)).Times(0);
|
||||
EXPECT_CALL(command2, End(false)).Times(0);
|
||||
|
||||
scheduler.Schedule(command1.get());
|
||||
scheduler.Schedule(command1);
|
||||
EXPECT_EQ(1, initCounter);
|
||||
scheduler.Run();
|
||||
EXPECT_EQ(1, exeCounter);
|
||||
EXPECT_TRUE(scheduler.IsScheduled(command1.get()));
|
||||
EXPECT_TRUE(scheduler.IsScheduled(command1));
|
||||
scheduler.Schedule(&command2);
|
||||
EXPECT_TRUE(scheduler.IsScheduled(command1.get()));
|
||||
EXPECT_TRUE(scheduler.IsScheduled(command1));
|
||||
EXPECT_FALSE(scheduler.IsScheduled(&command2));
|
||||
scheduler.Run();
|
||||
EXPECT_EQ(2, exeCounter);
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
#include "CommandTestBase.h"
|
||||
#include "frc2/command/EndlessCommand.h"
|
||||
#include "frc2/command/InstantCommand.h"
|
||||
|
||||
using namespace frc2;
|
||||
@@ -14,10 +13,10 @@ TEST_F(EndlessCommandTest, EndlessCommandSchedule) {
|
||||
|
||||
bool check = false;
|
||||
|
||||
EndlessCommand command{InstantCommand([&check] { check = true; }, {})};
|
||||
auto command = InstantCommand([&check] { check = true; }, {}).Endlessly();
|
||||
|
||||
scheduler.Schedule(&command);
|
||||
scheduler.Schedule(command);
|
||||
scheduler.Run();
|
||||
EXPECT_TRUE(scheduler.IsScheduled(&command));
|
||||
EXPECT_TRUE(scheduler.IsScheduled(command));
|
||||
EXPECT_TRUE(check);
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include <memory>
|
||||
|
||||
#include "CommandTestBase.h"
|
||||
#include "frc2/command/CommandPtr.h"
|
||||
#include "frc2/command/InstantCommand.h"
|
||||
#include "frc2/command/ProxyScheduleCommand.h"
|
||||
#include "frc2/command/WaitUntilCommand.h"
|
||||
@@ -51,10 +52,10 @@ TEST_F(ProxyScheduleCommandTest, OwningCommandSchedule) {
|
||||
|
||||
bool scheduled = false;
|
||||
|
||||
ProxyScheduleCommand command(
|
||||
std::make_unique<InstantCommand>([&scheduled] { scheduled = true; }));
|
||||
CommandPtr command =
|
||||
InstantCommand([&scheduled] { scheduled = true; }).AsProxy();
|
||||
|
||||
scheduler.Schedule(&command);
|
||||
scheduler.Schedule(command);
|
||||
scheduler.Run();
|
||||
|
||||
EXPECT_TRUE(scheduled);
|
||||
@@ -65,15 +66,15 @@ TEST_F(ProxyScheduleCommandTest, OwningCommandEnd) {
|
||||
|
||||
bool finished = false;
|
||||
|
||||
ProxyScheduleCommand command(
|
||||
std::make_unique<WaitUntilCommand>([&finished] { return finished; }));
|
||||
CommandPtr command =
|
||||
WaitUntilCommand([&finished] { return finished; }).AsProxy();
|
||||
|
||||
scheduler.Schedule(&command);
|
||||
scheduler.Schedule(command);
|
||||
scheduler.Run();
|
||||
|
||||
EXPECT_TRUE(scheduler.IsScheduled(&command));
|
||||
EXPECT_TRUE(scheduler.IsScheduled(command));
|
||||
finished = true;
|
||||
scheduler.Run();
|
||||
scheduler.Run();
|
||||
EXPECT_FALSE(scheduler.IsScheduled(&command));
|
||||
EXPECT_FALSE(scheduler.IsScheduled(command));
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
|
||||
#include "CommandTestBase.h"
|
||||
#include "frc2/command/FunctionalCommand.h"
|
||||
#include "frc2/command/RepeatCommand.h"
|
||||
|
||||
using namespace frc2;
|
||||
class RepeatCommandTest : public CommandTestBase {};
|
||||
@@ -33,7 +32,7 @@ TEST_F(RepeatCommandTest, CallsMethodsCorrectly) {
|
||||
EXPECT_EQ(0, isFinishedCounter);
|
||||
EXPECT_EQ(0, endCounter);
|
||||
|
||||
scheduler.Schedule(&command);
|
||||
scheduler.Schedule(command);
|
||||
EXPECT_EQ(1, initCounter);
|
||||
EXPECT_EQ(0, exeCounter);
|
||||
EXPECT_EQ(0, isFinishedCounter);
|
||||
|
||||
Reference in New Issue
Block a user