[commands] Add unless() decorator (#4244)

This commit is contained in:
ohowe
2022-05-24 10:22:19 -06:00
committed by GitHub
parent ef3714223b
commit b193b318c1
5 changed files with 77 additions and 2 deletions

View File

@@ -9,6 +9,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import edu.wpi.first.hal.HAL;
import edu.wpi.first.wpilibj.simulation.SimHooks;
import java.util.concurrent.atomic.AtomicBoolean;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.ResourceLock;
@@ -182,4 +183,24 @@ class CommandDecoratorTest extends CommandTestBase {
assertTrue(scheduler.isScheduled(perpetual));
}
}
@Test
void unlessTest() {
try (CommandScheduler scheduler = new CommandScheduler()) {
AtomicBoolean unlessCondition = new AtomicBoolean(true);
AtomicBoolean hasRunCondition = new AtomicBoolean(false);
Command command =
new InstantCommand(() -> hasRunCondition.set(true)).unless(unlessCondition::get);
scheduler.schedule(command);
scheduler.run();
assertFalse(hasRunCondition.get());
unlessCondition.set(false);
scheduler.schedule(command);
scheduler.run();
assertTrue(hasRunCondition.get());
}
}
}

View File

@@ -5,6 +5,7 @@
#include <frc/simulation/SimHooks.h>
#include "CommandTestBase.h"
#include "frc2/command/ConditionalCommand.h"
#include "frc2/command/InstantCommand.h"
#include "frc2/command/ParallelRaceGroup.h"
#include "frc2/command/PerpetualCommand.h"
@@ -101,3 +102,24 @@ TEST_F(CommandDecoratorTest, Perpetually) {
EXPECT_TRUE(scheduler.IsScheduled(&command));
}
TEST_F(CommandDecoratorTest, Unless) {
CommandScheduler scheduler = GetScheduler();
bool hasRun = false;
bool unlessBool = true;
auto command =
InstantCommand([&hasRun] { hasRun = true; }, {}).Unless([&unlessBool] {
return unlessBool;
});
scheduler.Schedule(&command);
scheduler.Run();
EXPECT_FALSE(hasRun);
unlessBool = false;
scheduler.Schedule(&command);
scheduler.Run();
EXPECT_TRUE(hasRun);
}