mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-25 01:41:43 +00:00
[commands] Add missing C++ decorators (#6599)
This commit is contained in:
@@ -22,17 +22,14 @@ class CommandDecoratorTest extends CommandTestBase {
|
||||
HAL.initialize(500, 0);
|
||||
SimHooks.pauseTiming();
|
||||
try (CommandScheduler scheduler = new CommandScheduler()) {
|
||||
Command command1 = new WaitCommand(10);
|
||||
|
||||
Command timeout = command1.withTimeout(2);
|
||||
Command timeout = new RunCommand(() -> {}).withTimeout(0.1);
|
||||
|
||||
scheduler.schedule(timeout);
|
||||
scheduler.run();
|
||||
|
||||
assertFalse(scheduler.isScheduled(command1));
|
||||
assertTrue(scheduler.isScheduled(timeout));
|
||||
|
||||
SimHooks.stepTiming(3);
|
||||
SimHooks.stepTiming(0.15);
|
||||
scheduler.run();
|
||||
|
||||
assertFalse(scheduler.isScheduled(timeout));
|
||||
@@ -44,15 +41,18 @@ class CommandDecoratorTest extends CommandTestBase {
|
||||
@Test
|
||||
void untilTest() {
|
||||
try (CommandScheduler scheduler = new CommandScheduler()) {
|
||||
AtomicBoolean condition = new AtomicBoolean();
|
||||
AtomicBoolean finish = new AtomicBoolean();
|
||||
|
||||
Command command = new WaitCommand(10).until(condition::get);
|
||||
Command command = new RunCommand(() -> {}).until(finish::get);
|
||||
|
||||
scheduler.schedule(command);
|
||||
scheduler.run();
|
||||
|
||||
assertTrue(scheduler.isScheduled(command));
|
||||
condition.set(true);
|
||||
|
||||
finish.set(true);
|
||||
scheduler.run();
|
||||
|
||||
assertFalse(scheduler.isScheduled(command));
|
||||
}
|
||||
}
|
||||
@@ -60,15 +60,18 @@ class CommandDecoratorTest extends CommandTestBase {
|
||||
@Test
|
||||
void onlyWhileTest() {
|
||||
try (CommandScheduler scheduler = new CommandScheduler()) {
|
||||
AtomicBoolean condition = new AtomicBoolean(true);
|
||||
AtomicBoolean run = new AtomicBoolean(true);
|
||||
|
||||
Command command = new WaitCommand(10).onlyWhile(condition::get);
|
||||
Command command = new RunCommand(() -> {}).onlyWhile(run::get);
|
||||
|
||||
scheduler.schedule(command);
|
||||
scheduler.run();
|
||||
|
||||
assertTrue(scheduler.isScheduled(command));
|
||||
condition.set(false);
|
||||
|
||||
run.set(false);
|
||||
scheduler.run();
|
||||
|
||||
assertFalse(scheduler.isScheduled(command));
|
||||
}
|
||||
}
|
||||
@@ -90,61 +93,75 @@ class CommandDecoratorTest extends CommandTestBase {
|
||||
@Test
|
||||
void beforeStartingTest() {
|
||||
try (CommandScheduler scheduler = new CommandScheduler()) {
|
||||
AtomicBoolean condition = new AtomicBoolean();
|
||||
condition.set(false);
|
||||
AtomicBoolean finished = new AtomicBoolean();
|
||||
finished.set(false);
|
||||
|
||||
Command command = new InstantCommand();
|
||||
Command command = new InstantCommand().beforeStarting(() -> finished.set(true));
|
||||
|
||||
scheduler.schedule(command.beforeStarting(() -> condition.set(true)));
|
||||
scheduler.schedule(command);
|
||||
|
||||
assertTrue(condition.get());
|
||||
assertTrue(finished.get());
|
||||
|
||||
scheduler.run();
|
||||
|
||||
assertTrue(scheduler.isScheduled(command));
|
||||
|
||||
scheduler.run();
|
||||
|
||||
assertFalse(scheduler.isScheduled(command));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void andThenLambdaTest() {
|
||||
try (CommandScheduler scheduler = new CommandScheduler()) {
|
||||
AtomicBoolean condition = new AtomicBoolean();
|
||||
condition.set(false);
|
||||
AtomicBoolean finished = new AtomicBoolean(false);
|
||||
|
||||
Command command = new InstantCommand();
|
||||
Command command = new InstantCommand().andThen(() -> finished.set(true));
|
||||
|
||||
scheduler.schedule(command.andThen(() -> condition.set(true)));
|
||||
scheduler.schedule(command);
|
||||
|
||||
assertFalse(condition.get());
|
||||
assertFalse(finished.get());
|
||||
|
||||
scheduler.run();
|
||||
|
||||
assertTrue(condition.get());
|
||||
assertTrue(finished.get());
|
||||
|
||||
scheduler.run();
|
||||
|
||||
assertFalse(scheduler.isScheduled(command));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void andThenTest() {
|
||||
try (CommandScheduler scheduler = new CommandScheduler()) {
|
||||
AtomicBoolean condition = new AtomicBoolean();
|
||||
condition.set(false);
|
||||
AtomicBoolean condition = new AtomicBoolean(false);
|
||||
|
||||
Command command1 = new InstantCommand();
|
||||
Command command2 = new InstantCommand(() -> condition.set(true));
|
||||
Command group = command1.andThen(command2);
|
||||
|
||||
scheduler.schedule(command1.andThen(command2));
|
||||
scheduler.schedule(group);
|
||||
|
||||
assertFalse(condition.get());
|
||||
|
||||
scheduler.run();
|
||||
|
||||
assertTrue(condition.get());
|
||||
|
||||
scheduler.run();
|
||||
|
||||
assertFalse(scheduler.isScheduled(group));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void deadlineWithTest() {
|
||||
void deadlineForTest() {
|
||||
try (CommandScheduler scheduler = new CommandScheduler()) {
|
||||
AtomicBoolean condition = new AtomicBoolean();
|
||||
condition.set(false);
|
||||
AtomicBoolean finish = new AtomicBoolean(false);
|
||||
|
||||
Command dictator = new WaitUntilCommand(condition::get);
|
||||
Command dictator = new WaitUntilCommand(finish::get);
|
||||
Command endsBefore = new InstantCommand();
|
||||
Command endsAfter = new WaitUntilCommand(() -> false);
|
||||
|
||||
@@ -155,7 +172,7 @@ class CommandDecoratorTest extends CommandTestBase {
|
||||
|
||||
assertTrue(scheduler.isScheduled(group));
|
||||
|
||||
condition.set(true);
|
||||
finish.set(true);
|
||||
scheduler.run();
|
||||
|
||||
assertFalse(scheduler.isScheduled(group));
|
||||
@@ -165,10 +182,9 @@ class CommandDecoratorTest extends CommandTestBase {
|
||||
@Test
|
||||
void alongWithTest() {
|
||||
try (CommandScheduler scheduler = new CommandScheduler()) {
|
||||
AtomicBoolean condition = new AtomicBoolean();
|
||||
condition.set(false);
|
||||
AtomicBoolean finish = new AtomicBoolean(false);
|
||||
|
||||
Command command1 = new WaitUntilCommand(condition::get);
|
||||
Command command1 = new WaitUntilCommand(finish::get);
|
||||
Command command2 = new InstantCommand();
|
||||
|
||||
Command group = command1.alongWith(command2);
|
||||
@@ -178,7 +194,7 @@ class CommandDecoratorTest extends CommandTestBase {
|
||||
|
||||
assertTrue(scheduler.isScheduled(group));
|
||||
|
||||
condition.set(true);
|
||||
finish.set(true);
|
||||
scheduler.run();
|
||||
|
||||
assertFalse(scheduler.isScheduled(group));
|
||||
@@ -203,40 +219,38 @@ class CommandDecoratorTest extends CommandTestBase {
|
||||
@Test
|
||||
void unlessTest() {
|
||||
try (CommandScheduler scheduler = new CommandScheduler()) {
|
||||
AtomicBoolean hasRun = new AtomicBoolean(false);
|
||||
AtomicBoolean unlessCondition = new AtomicBoolean(true);
|
||||
AtomicBoolean hasRunCondition = new AtomicBoolean(false);
|
||||
|
||||
Command command =
|
||||
new InstantCommand(() -> hasRunCondition.set(true)).unless(unlessCondition::get);
|
||||
Command command = new InstantCommand(() -> hasRun.set(true)).unless(unlessCondition::get);
|
||||
|
||||
scheduler.schedule(command);
|
||||
scheduler.run();
|
||||
assertFalse(hasRunCondition.get());
|
||||
assertFalse(hasRun.get());
|
||||
|
||||
unlessCondition.set(false);
|
||||
scheduler.schedule(command);
|
||||
scheduler.run();
|
||||
assertTrue(hasRunCondition.get());
|
||||
assertTrue(hasRun.get());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void onlyIfTest() {
|
||||
try (CommandScheduler scheduler = new CommandScheduler()) {
|
||||
AtomicBoolean hasRun = new AtomicBoolean(false);
|
||||
AtomicBoolean onlyIfCondition = new AtomicBoolean(false);
|
||||
AtomicBoolean hasRunCondition = new AtomicBoolean(false);
|
||||
|
||||
Command command =
|
||||
new InstantCommand(() -> hasRunCondition.set(true)).onlyIf(onlyIfCondition::get);
|
||||
Command command = new InstantCommand(() -> hasRun.set(true)).onlyIf(onlyIfCondition::get);
|
||||
|
||||
scheduler.schedule(command);
|
||||
scheduler.run();
|
||||
assertFalse(hasRunCondition.get());
|
||||
assertFalse(hasRun.get());
|
||||
|
||||
onlyIfCondition.set(true);
|
||||
scheduler.schedule(command);
|
||||
scheduler.run();
|
||||
assertTrue(hasRunCondition.get());
|
||||
assertTrue(hasRun.get());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user