[commands] Improve error message when composing commands twice in same composition (#6091)

Also disallow deadline command from also appearing in other commands.
This commit is contained in:
Joseph Eng
2023-12-26 18:07:16 -08:00
committed by GitHub
parent 55508706ff
commit 8aeee03626
5 changed files with 64 additions and 16 deletions

View File

@@ -5,6 +5,7 @@
package edu.wpi.first.wpilibj2.command;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.params.provider.Arguments.arguments;
import edu.wpi.first.wpilibj2.command.Command.InterruptionBehavior;
@@ -112,4 +113,19 @@ abstract class MultiCompositionTestBase<T extends Command> extends SingleComposi
var command = compose(command1, command2, command3);
assertEquals(expected, command.runsWhenDisabled());
}
static Stream<Arguments> composeDuplicates() {
Command a = new InstantCommand(() -> {});
Command b = new InstantCommand(() -> {});
return Stream.of(
arguments("AA", new Command[] {a, a}),
arguments("ABA", new Command[] {a, b, a}),
arguments("BAA", new Command[] {b, a, a}));
}
@MethodSource
@ParameterizedTest(name = "composeDuplicates[{index}]: {0}")
void composeDuplicates(@SuppressWarnings("unused") String name, Command[] commands) {
assertThrows(IllegalArgumentException.class, () -> compose(commands));
}
}

View File

@@ -4,6 +4,7 @@
package edu.wpi.first.wpilibj2.command;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -124,6 +125,21 @@ class ParallelDeadlineGroupTest extends MultiCompositionTestBase<ParallelDeadlin
IllegalArgumentException.class, () -> new ParallelDeadlineGroup(command1, command2));
}
@Test
void parallelDeadlineSetDeadlineToDeadlineTest() {
Command a = new InstantCommand(() -> {});
ParallelDeadlineGroup group = new ParallelDeadlineGroup(a);
assertDoesNotThrow(() -> group.setDeadline(a));
}
@Test
void parallelDeadlineSetDeadlineDuplicateTest() {
Command a = new InstantCommand(() -> {});
Command b = new InstantCommand(() -> {});
ParallelDeadlineGroup group = new ParallelDeadlineGroup(a, b);
assertThrows(IllegalArgumentException.class, () -> group.setDeadline(b));
}
@Override
public ParallelDeadlineGroup compose(Command... members) {
return new ParallelDeadlineGroup(members[0], Arrays.copyOfRange(members, 1, members.length));