From 2465d7fecdc6593b3d0864f1e952bc477d02c697 Mon Sep 17 00:00:00 2001 From: Ryan Blue Date: Mon, 28 Aug 2023 16:17:21 -0400 Subject: [PATCH] Add compose/schedule lock testing to composition tests --- .../command/CommandGroupErrorTest.java | 59 ------------------- .../command/SingleCompositionTestBase.java | 31 ++++++++++ 2 files changed, 31 insertions(+), 59 deletions(-) delete mode 100644 wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/CommandGroupErrorTest.java diff --git a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/CommandGroupErrorTest.java b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/CommandGroupErrorTest.java deleted file mode 100644 index 2a5deda3cb6..00000000000 --- a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/CommandGroupErrorTest.java +++ /dev/null @@ -1,59 +0,0 @@ -// Copyright (c) FIRST and other WPILib contributors. -// Open Source Software; you can modify and/or share it under the terms of -// the WPILib BSD license file in the root directory of this project. - -package edu.wpi.first.wpilibj2.command; - -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; -import static org.junit.jupiter.api.Assertions.assertThrows; - -import java.util.concurrent.atomic.AtomicReference; -import org.junit.jupiter.api.Test; - -class CommandGroupErrorTest extends CommandTestBase { - @Test - void commandInMultipleGroupsTest() { - MockCommandHolder command1Holder = new MockCommandHolder(true); - Command command1 = command1Holder.getMock(); - MockCommandHolder command2Holder = new MockCommandHolder(true); - Command command2 = command2Holder.getMock(); - - new ParallelCommandGroup(command1, command2); - assertThrows( - IllegalArgumentException.class, () -> new ParallelCommandGroup(command1, command2)); - } - - @Test - void commandInGroupExternallyScheduledTest() { - MockCommandHolder command1Holder = new MockCommandHolder(true); - Command command1 = command1Holder.getMock(); - MockCommandHolder command2Holder = new MockCommandHolder(true); - Command command2 = command2Holder.getMock(); - - new ParallelCommandGroup(command1, command2); - - assertThrows( - IllegalArgumentException.class, () -> CommandScheduler.getInstance().schedule(command1)); - } - - @Test - void redecoratedCommandErrorTest() { - Command command = new InstantCommand(); - - assertDoesNotThrow(() -> command.withTimeout(10).until(() -> false)); - assertThrows(IllegalArgumentException.class, () -> command.withTimeout(10)); - CommandScheduler.getInstance().removeComposedCommand(command); - assertDoesNotThrow(() -> command.withTimeout(10)); - } - - @Test - void scheduledThenComposedTest() { - Command command = new RunCommand(() -> {}); - CommandScheduler.getInstance().schedule(command); - CommandScheduler.getInstance().run(); - - AtomicReference composition = new AtomicReference<>(new InstantCommand()); - assertThrows( - IllegalArgumentException.class, () -> composition.set(new ParallelCommandGroup(command))); - } -} diff --git a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/SingleCompositionTestBase.java b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/SingleCompositionTestBase.java index 67c8568c17c..cc944c99e28 100644 --- a/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/SingleCompositionTestBase.java +++ b/wpilibNewCommands/src/test/java/edu/wpi/first/wpilibj2/command/SingleCompositionTestBase.java @@ -5,7 +5,9 @@ package edu.wpi.first.wpilibj2.command; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.EnumSource; import org.junit.jupiter.params.provider.ValueSource; @@ -29,4 +31,33 @@ void runWhenDisabled(boolean runsWhenDisabled) { composeSingle(new WaitUntilCommand(() -> false).ignoringDisable(runsWhenDisabled)); assertEquals(runsWhenDisabled, command.runsWhenDisabled()); } + + @Test + void commandInOtherCompositionTest() { + var command = new InstantCommand(); + var composition = new WrapperCommand(command) {}; + assertThrows(IllegalArgumentException.class, () -> composeSingle(command)); + } + + @Test + void commandInMultipleCompositionsTest() { + var command = new InstantCommand(); + var composition = composeSingle(command); + assertThrows(IllegalArgumentException.class, () -> composeSingle(command)); + } + + @Test + void composeThenScheduleTest() { + var command = new InstantCommand(); + var composition = composeSingle(command); + assertThrows( + IllegalArgumentException.class, () -> CommandScheduler.getInstance().schedule(command)); + } + + @Test + void scheduleThenComposeTest() { + var command = new RunCommand(() -> {}); + CommandScheduler.getInstance().schedule(command); + assertThrows(IllegalArgumentException.class, () -> composeSingle(command)); + } }