[commands] Add onlyWhile and onlyIf (#5291)

This commit is contained in:
Joseph Eng
2023-04-30 14:09:02 -07:00
committed by GitHub
parent 40ca094686
commit ee3b4621e5
7 changed files with 183 additions and 3 deletions

View File

@@ -54,6 +54,24 @@ TEST_F(CommandDecoratorTest, Until) {
EXPECT_FALSE(scheduler.IsScheduled(command));
}
TEST_F(CommandDecoratorTest, OnlyWhile) {
CommandScheduler scheduler = GetScheduler();
bool run = true;
auto command = RunCommand([] {}, {}).OnlyWhile([&run] { return run; });
scheduler.Schedule(command);
scheduler.Run();
EXPECT_TRUE(scheduler.IsScheduled(command));
run = false;
scheduler.Run();
EXPECT_FALSE(scheduler.IsScheduled(command));
}
TEST_F(CommandDecoratorTest, IgnoringDisable) {
CommandScheduler scheduler = GetScheduler();
@@ -140,6 +158,27 @@ TEST_F(CommandDecoratorTest, Unless) {
EXPECT_TRUE(hasRun);
}
TEST_F(CommandDecoratorTest, OnlyIf) {
CommandScheduler scheduler = GetScheduler();
bool hasRun = false;
bool onlyIfBool = false;
auto command =
InstantCommand([&hasRun] { hasRun = true; }, {}).OnlyIf([&onlyIfBool] {
return onlyIfBool;
});
scheduler.Schedule(command);
scheduler.Run();
EXPECT_FALSE(hasRun);
onlyIfBool = true;
scheduler.Schedule(command);
scheduler.Run();
EXPECT_TRUE(hasRun);
}
TEST_F(CommandDecoratorTest, FinallyDo) {
CommandScheduler scheduler = GetScheduler();
int first = 0;